|
| 1 | +use futures::SinkExt as _; |
| 2 | +use test_programs::p3::wasi::http::types::{ErrorCode, Headers, Method, Request, Scheme, Trailers}; |
| 3 | +use test_programs::p3::{wit_future, wit_stream}; |
| 4 | +use wit_bindgen_rt::async_support::{FutureWriter, StreamWriter}; |
| 5 | + |
1 | 6 | struct Component; |
2 | 7 |
|
3 | 8 | test_programs::p3::export!(Component); |
4 | 9 |
|
| 10 | +fn make_request() -> ( |
| 11 | + Request, |
| 12 | + StreamWriter<u8>, |
| 13 | + FutureWriter<Result<Option<Trailers>, ErrorCode>>, |
| 14 | +) { |
| 15 | + let (contents_tx, contents_rx) = wit_stream::new(); |
| 16 | + let (trailers_tx, trailers_rx) = wit_future::new(); |
| 17 | + let (request, _) = Request::new( |
| 18 | + Headers::from_list(&[("Content-Length".to_string(), b"11".to_vec())]).unwrap(), |
| 19 | + Some(contents_rx), |
| 20 | + trailers_rx, |
| 21 | + None, |
| 22 | + ); |
| 23 | + |
| 24 | + request.set_method(&Method::Post).expect("setting method"); |
| 25 | + request |
| 26 | + .set_scheme(Some(&Scheme::Http)) |
| 27 | + .expect("setting scheme"); |
| 28 | + let addr = test_programs::p3::wasi::cli::environment::get_environment() |
| 29 | + .into_iter() |
| 30 | + .find_map(|(k, v)| k.eq("HTTP_SERVER").then_some(v)) |
| 31 | + .unwrap(); |
| 32 | + request |
| 33 | + .set_authority(Some(&addr)) |
| 34 | + .expect("setting authority"); |
| 35 | + request |
| 36 | + .set_path_with_query(Some("/")) |
| 37 | + .expect("setting path with query"); |
| 38 | + |
| 39 | + (request, contents_tx, trailers_tx) |
| 40 | +} |
| 41 | + |
5 | 42 | impl test_programs::p3::exports::wasi::cli::run::Guest for Component { |
6 | 43 | async fn run() -> Result<(), ()> { |
7 | | - // TODO: adapt |
| 44 | + { |
| 45 | + println!("writing enough"); |
| 46 | + let (_, mut contents_tx, trailers_tx) = make_request(); |
| 47 | + contents_tx.send(b"long enough".to_vec()).await.unwrap(); |
| 48 | + drop(contents_tx); |
| 49 | + trailers_tx.write(Ok(None)).await; |
| 50 | + } |
| 51 | + |
| 52 | + { |
| 53 | + println!("writing too little"); |
| 54 | + let (_, mut contents_tx, trailers_tx) = make_request(); |
| 55 | + contents_tx.send(b"msg".to_vec()).await.unwrap(); |
| 56 | + drop(contents_tx); |
| 57 | + trailers_tx.write(Ok(None)).await; |
| 58 | + |
| 59 | + // TODO: Figure out how/if to represent this in wasip3 |
| 60 | + //let e = OutgoingBody::finish(outgoing_body, None) |
| 61 | + // .expect_err("finish should fail"); |
| 62 | + |
| 63 | + //assert!( |
| 64 | + // matches!(&e, ErrorCode::HttpRequestBodySize(Some(3))), |
| 65 | + // "unexpected error: {e:#?}" |
| 66 | + //); |
| 67 | + } |
| 68 | + |
| 69 | + { |
| 70 | + println!("writing too much"); |
| 71 | + let (_, mut contents_tx, trailers_tx) = make_request(); |
| 72 | + contents_tx |
| 73 | + .send(b"more than 11 bytes".to_vec()) |
| 74 | + .await |
| 75 | + .unwrap(); |
| 76 | + drop(contents_tx); |
| 77 | + trailers_tx.write(Ok(None)).await; |
| 78 | + |
| 79 | + // TODO: Figure out how/if to represent this in wasip3 |
| 80 | + //let e = request_body |
| 81 | + // .blocking_write_and_flush("more than 11 bytes".as_bytes()) |
| 82 | + // .expect_err("write should fail"); |
| 83 | + //let e = match e { |
| 84 | + // test_programs::wasi::io::streams::StreamError::LastOperationFailed(e) => { |
| 85 | + // http_error_code(&e) |
| 86 | + // } |
| 87 | + // test_programs::wasi::io::streams::StreamError::Closed => panic!("request closed"), |
| 88 | + //}; |
| 89 | + //assert!( |
| 90 | + // matches!( |
| 91 | + // e, |
| 92 | + // Some(ErrorCode::HttpRequestBodySize(Some(18))) |
| 93 | + // ), |
| 94 | + // "unexpected error {e:?}" |
| 95 | + //); |
| 96 | + //let e = OutgoingBody::finish(outgoing_body, None) |
| 97 | + // .expect_err("finish should fail"); |
| 98 | + |
| 99 | + //assert!( |
| 100 | + // matches!(&e, ErrorCode::HttpRequestBodySize(Some(18))), |
| 101 | + // "unexpected error: {e:#?}" |
| 102 | + //); |
| 103 | + } |
8 | 104 | Ok(()) |
9 | 105 | } |
10 | 106 | } |
11 | 107 |
|
12 | 108 | fn main() {} |
13 | | - |
14 | | -//use test_programs::wasi::http::types as http_types; |
15 | | -// |
16 | | -//fn make_request() -> http_types::OutgoingRequest { |
17 | | -// let request = http_types::OutgoingRequest::new( |
18 | | -// http_types::Headers::from_list(&[("Content-Length".to_string(), b"11".to_vec())]).unwrap(), |
19 | | -// ); |
20 | | -// |
21 | | -// request |
22 | | -// .set_method(&http_types::Method::Post) |
23 | | -// .expect("setting method"); |
24 | | -// request |
25 | | -// .set_scheme(Some(&http_types::Scheme::Http)) |
26 | | -// .expect("setting scheme"); |
27 | | -// let addr = std::env::var("HTTP_SERVER").unwrap(); |
28 | | -// request |
29 | | -// .set_authority(Some(&addr)) |
30 | | -// .expect("setting authority"); |
31 | | -// request |
32 | | -// .set_path_with_query(Some("/")) |
33 | | -// .expect("setting path with query"); |
34 | | -// |
35 | | -// request |
36 | | -//} |
37 | | -// |
38 | | -//fn main() { |
39 | | -// { |
40 | | -// println!("writing enough"); |
41 | | -// let request = make_request(); |
42 | | -// let outgoing_body = request.body().unwrap(); |
43 | | -// |
44 | | -// { |
45 | | -// let request_body = outgoing_body.write().unwrap(); |
46 | | -// request_body |
47 | | -// .blocking_write_and_flush("long enough".as_bytes()) |
48 | | -// .unwrap(); |
49 | | -// } |
50 | | -// |
51 | | -// http_types::OutgoingBody::finish(outgoing_body, None).expect("enough written") |
52 | | -// } |
53 | | -// |
54 | | -// { |
55 | | -// println!("writing too little"); |
56 | | -// let request = make_request(); |
57 | | -// let outgoing_body = request.body().unwrap(); |
58 | | -// |
59 | | -// { |
60 | | -// let request_body = outgoing_body.write().unwrap(); |
61 | | -// request_body |
62 | | -// .blocking_write_and_flush("msg".as_bytes()) |
63 | | -// .unwrap(); |
64 | | -// } |
65 | | -// |
66 | | -// let e = |
67 | | -// http_types::OutgoingBody::finish(outgoing_body, None).expect_err("finish should fail"); |
68 | | -// |
69 | | -// assert!( |
70 | | -// matches!(&e, http_types::ErrorCode::HttpRequestBodySize(Some(3))), |
71 | | -// "unexpected error: {e:#?}" |
72 | | -// ); |
73 | | -// } |
74 | | -// |
75 | | -// { |
76 | | -// println!("writing too much"); |
77 | | -// let request = make_request(); |
78 | | -// let outgoing_body = request.body().unwrap(); |
79 | | -// |
80 | | -// { |
81 | | -// let request_body = outgoing_body.write().unwrap(); |
82 | | -// let e = request_body |
83 | | -// .blocking_write_and_flush("more than 11 bytes".as_bytes()) |
84 | | -// .expect_err("write should fail"); |
85 | | -// |
86 | | -// let e = match e { |
87 | | -// test_programs::wasi::io::streams::StreamError::LastOperationFailed(e) => { |
88 | | -// http_types::http_error_code(&e) |
89 | | -// } |
90 | | -// test_programs::wasi::io::streams::StreamError::Closed => panic!("request closed"), |
91 | | -// }; |
92 | | -// |
93 | | -// assert!( |
94 | | -// matches!( |
95 | | -// e, |
96 | | -// Some(http_types::ErrorCode::HttpRequestBodySize(Some(18))) |
97 | | -// ), |
98 | | -// "unexpected error {e:?}" |
99 | | -// ); |
100 | | -// } |
101 | | -// |
102 | | -// let e = |
103 | | -// http_types::OutgoingBody::finish(outgoing_body, None).expect_err("finish should fail"); |
104 | | -// |
105 | | -// assert!( |
106 | | -// matches!(&e, http_types::ErrorCode::HttpRequestBodySize(Some(18))), |
107 | | -// "unexpected error: {e:#?}" |
108 | | -// ); |
109 | | -// } |
110 | | -//} |
0 commit comments