This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathlib.rs
60 lines (53 loc) · 1.97 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use bytes::Bytes;
#[no_mangle]
pub extern "C" fn get() {
let url = "https://some-random-api.ml/facts/dog".to_string();
let req = http::request::Builder::new().uri(&url).body(None).unwrap();
let mut res = wasi_experimental_http::request(req).expect("cannot make get request");
let str = std::str::from_utf8(&res.body_read_all().unwrap())
.unwrap()
.to_string();
assert_eq!(str.is_empty(), false);
assert_eq!(res.status_code, 200);
assert!(!res
.header_get("content-type".to_string())
.unwrap()
.is_empty());
let header_map = res.headers_get_all().unwrap();
assert_ne!(header_map.len(), 0);
}
#[no_mangle]
pub extern "C" fn post() {
let url = "https://postman-echo.com/post".to_string();
let req = http::request::Builder::new()
.method(http::Method::POST)
.uri(&url)
.header("Content-Type", "text/plain")
.header("abc", "def");
let b = Bytes::from("Testing with a request body. Does this actually work?");
let req = req.body(Some(b)).unwrap();
let mut res = wasi_experimental_http::request(req).expect("cannot make post request");
let _ = std::str::from_utf8(&res.body_read_all().unwrap())
.unwrap()
.to_string();
assert_eq!(res.status_code, 200);
assert!(!res
.header_get("content-type".to_string())
.unwrap()
.is_empty());
let header_map = res.headers_get_all().unwrap();
assert_ne!(header_map.len(), 0);
}
#[allow(unused_variables)]
#[no_mangle]
pub extern "C" fn concurrent() {
let url = "https://some-random-api.ml/facts/dog".to_string();
// the responses are unused to avoid dropping them.
let req1 = make_req(url.clone());
let req2 = make_req(url.clone());
let req3 = make_req(url);
}
fn make_req(url: String) -> wasi_experimental_http::Response {
let req = http::request::Builder::new().uri(&url).body(None).unwrap();
wasi_experimental_http::request(req).expect("cannot make get request")
}