1 use test_programs::wasi::http::types as http_types;
2 
main()3 fn main() {
4     println!("Called _start");
5     {
6         let headers = http_types::Headers::from_list(&[(
7             "Content-Type".to_string(),
8             "application/json".to_string().into_bytes(),
9         )])
10         .unwrap();
11         let request = http_types::OutgoingRequest::new(headers);
12 
13         request
14             .set_method(&http_types::Method::Get)
15             .expect("setting method");
16         request
17             .set_scheme(Some(&http_types::Scheme::Https))
18             .expect("setting scheme");
19         request
20             .set_authority(Some("www.example.com"))
21             .expect("setting authority");
22 
23         let outgoing_body = request.body().unwrap();
24         let request_body = outgoing_body.write().unwrap();
25         request_body
26             .blocking_write_and_flush("request-body".as_bytes())
27             .unwrap();
28     }
29     {
30         let headers = http_types::Headers::from_list(&[(
31             "Content-Type".to_string(),
32             "application/text".to_string().into_bytes(),
33         )])
34         .unwrap();
35         let response = http_types::OutgoingResponse::new(headers);
36         let outgoing_body = response.body().unwrap();
37         let response_body = outgoing_body.write().unwrap();
38         response_body
39             .blocking_write_and_flush("response-body".as_bytes())
40             .unwrap();
41     }
42 
43     {
44         let req = http_types::OutgoingRequest::new(http_types::Fields::new());
45 
46         assert!(
47             req.set_method(&http_types::Method::Other("invalid method".to_string()))
48                 .is_err()
49         );
50 
51         assert!(req.set_authority(Some("bad-\nhost")).is_err());
52         // IPv6 addresses with and without a port should be allowed
53         assert!(req.set_authority(Some("[::]:443")).is_ok());
54         assert!(req.set_authority(Some("[::]")).is_ok());
55 
56         assert!(
57             req.set_scheme(Some(&http_types::Scheme::Other("bad\nscheme".to_string())))
58                 .is_err()
59         );
60 
61         assert!(req.set_path_with_query(Some("/bad\npath")).is_err());
62     }
63 
64     println!("Done");
65 }
66