1+ use anyhow:: Context as _;
2+ use futures:: join;
3+ use test_programs:: p3:: wasi:: http:: handler;
14use test_programs:: p3:: wasi:: http:: types:: { ErrorCode , Headers , Method , Request , Scheme , Trailers } ;
25use test_programs:: p3:: { wit_future, wit_stream} ;
6+ use wit_bindgen:: FutureReader ;
37use wit_bindgen_rt:: async_support:: { FutureWriter , StreamWriter } ;
48
59struct Component ;
@@ -10,10 +14,11 @@ fn make_request() -> (
1014 Request ,
1115 StreamWriter < u8 > ,
1216 FutureWriter < Result < Option < Trailers > , ErrorCode > > ,
17+ FutureReader < Result < ( ) , ErrorCode > > ,
1318) {
1419 let ( contents_tx, contents_rx) = wit_stream:: new ( ) ;
1520 let ( trailers_tx, trailers_rx) = wit_future:: new ( ) ;
16- let ( request, _ ) = Request :: new (
21+ let ( request, transmit ) = Request :: new (
1722 Headers :: from_list ( & [ ( "Content-Length" . to_string ( ) , b"11" . to_vec ( ) ) ] ) . unwrap ( ) ,
1823 Some ( contents_rx) ,
1924 trailers_rx,
@@ -35,72 +40,95 @@ fn make_request() -> (
3540 . set_path_with_query ( Some ( "/" ) )
3641 . expect ( "setting path with query" ) ;
3742
38- ( request, contents_tx, trailers_tx)
43+ ( request, contents_tx, trailers_tx, transmit )
3944}
4045
4146impl test_programs:: p3:: exports:: wasi:: cli:: run:: Guest for Component {
4247 async fn run ( ) -> Result < ( ) , ( ) > {
4348 {
44- println ! ( "writing enough" ) ;
45- let ( _, mut contents_tx, trailers_tx) = make_request ( ) ;
46- let remaining = contents_tx. write_all ( b"long enough" . to_vec ( ) ) . await ;
47- assert ! ( remaining. is_empty( ) ) ;
48- drop ( contents_tx) ;
49- trailers_tx. write ( Ok ( None ) ) . await . unwrap ( ) ;
49+ let ( request, mut contents_tx, trailers_tx, transmit) = make_request ( ) ;
50+ let ( transmit, handle) = join ! ( async { transmit. await } , async {
51+ let res = handler:: handle( request)
52+ . await
53+ . context( "failed to send request" ) ?;
54+ println!( "writing enough" ) ;
55+ let remaining = contents_tx. write_all( b"long enough" . to_vec( ) ) . await ;
56+ assert!(
57+ remaining. is_empty( ) ,
58+ "{}" ,
59+ String :: from_utf8_lossy( & remaining)
60+ ) ;
61+ drop( contents_tx) ;
62+ trailers_tx
63+ . write( Ok ( None ) )
64+ . await
65+ . context( "failed to finish body" ) ?;
66+ anyhow:: Ok ( res)
67+ } ) ;
68+ let res = handle. unwrap ( ) ;
69+ drop ( res) ;
70+ transmit
71+ . expect ( "transmit sender dropped" )
72+ . expect ( "failed to transmit request" ) ;
5073 }
5174
5275 {
53- println ! ( "writing too little" ) ;
54- let ( _, mut contents_tx, trailers_tx) = make_request ( ) ;
55- let remaining = contents_tx. write_all ( b"msg" . to_vec ( ) ) . await ;
56- assert ! ( remaining. is_empty( ) ) ;
57- drop ( contents_tx) ;
58- trailers_tx. write ( Ok ( None ) ) . await . unwrap ( ) ;
59-
60- // handle()
61-
62- // TODO: Figure out how/if to represent this in wasip3
63- //let e = OutgoingBody::finish(outgoing_body, None)
64- // .expect_err("finish should fail");
65-
66- //assert!(
67- // matches!(&e, ErrorCode::HttpRequestBodySize(Some(3))),
68- // "unexpected error: {e:#?}"
69- //);
76+ let ( request, mut contents_tx, trailers_tx, transmit) = make_request ( ) ;
77+ let ( transmit, handle) = join ! ( async { transmit. await } , async {
78+ let res = handler:: handle( request)
79+ . await
80+ . context( "failed to send request" ) ?;
81+ println!( "writing too little" ) ;
82+ let remaining = contents_tx. write_all( b"msg" . to_vec( ) ) . await ;
83+ assert!(
84+ remaining. is_empty( ) ,
85+ "{}" ,
86+ String :: from_utf8_lossy( & remaining)
87+ ) ;
88+ drop( contents_tx) ;
89+ trailers_tx
90+ . write( Ok ( None ) )
91+ . await
92+ . context( "failed to finish body" ) ?;
93+ anyhow:: Ok ( res)
94+ } ) ;
95+ let res = handle. unwrap ( ) ;
96+ drop ( res) ;
97+ let err = transmit
98+ . expect ( "transmit sender dropped" )
99+ . expect_err ( "request transmission should have failed" ) ;
100+ assert ! (
101+ matches!( err, ErrorCode :: HttpRequestBodySize ( Some ( 3 ) ) ) ,
102+ "unexpected error: {err:#?}"
103+ ) ;
70104 }
71105
72106 {
73- println ! ( "writing too much" ) ;
74- let ( _, mut contents_tx, trailers_tx) = make_request ( ) ;
75- let remaining = contents_tx. write_all ( b"more than 11 bytes" . to_vec ( ) ) . await ;
76- assert ! ( remaining. is_empty( ) ) ;
77- drop ( contents_tx) ;
78- trailers_tx. write ( Ok ( None ) ) . await . unwrap ( ) ;
79-
80- // TODO: Figure out how/if to represent this in wasip3
81- //let e = request_body
82- // .blocking_write_and_flush("more than 11 bytes".as_bytes())
83- // .expect_err("write should fail");
84- //let e = match e {
85- // test_programs::wasi::io::streams::StreamError::LastOperationFailed(e) => {
86- // http_error_code(&e)
87- // }
88- // test_programs::wasi::io::streams::StreamError::Closed => panic!("request closed"),
89- //};
90- //assert!(
91- // matches!(
92- // e,
93- // Some(ErrorCode::HttpRequestBodySize(Some(18)))
94- // ),
95- // "unexpected error {e:?}"
96- //);
97- //let e = OutgoingBody::finish(outgoing_body, None)
98- // .expect_err("finish should fail");
99-
100- //assert!(
101- // matches!(&e, ErrorCode::HttpRequestBodySize(Some(18))),
102- // "unexpected error: {e:#?}"
103- //);
107+ let ( request, mut contents_tx, trailers_tx, transmit) = make_request ( ) ;
108+ let ( transmit, handle) = join ! ( async { transmit. await } , async {
109+ let res = handler:: handle( request)
110+ . await
111+ . context( "failed to send request" ) ?;
112+ println!( "writing too much" ) ;
113+ let remaining = contents_tx. write_all( b"more than 11 bytes" . to_vec( ) ) . await ;
114+ assert!(
115+ remaining. is_empty( ) ,
116+ "{}" ,
117+ String :: from_utf8_lossy( & remaining)
118+ ) ;
119+ drop( contents_tx) ;
120+ _ = trailers_tx. write( Ok ( None ) ) . await ;
121+ anyhow:: Ok ( res)
122+ } ) ;
123+ let res = handle. unwrap ( ) ;
124+ drop ( res) ;
125+ let err = transmit
126+ . expect ( "transmit sender dropped" )
127+ . expect_err ( "request transmission should have failed" ) ;
128+ assert ! (
129+ matches!( err, ErrorCode :: HttpRequestBodySize ( Some ( 18 ) ) ) ,
130+ "unexpected error: {err:#?}"
131+ ) ;
104132 }
105133 Ok ( ( ) )
106134 }
0 commit comments