16f6a514bSRoman Volosatovs use futures::join;
26f6a514bSRoman Volosatovs use test_programs::p3::wasi::http::types::{ErrorCode, Headers, Request, Response};
36f6a514bSRoman Volosatovs use test_programs::p3::{wit_future, wit_stream};
41047b511SAlex Crichton use wit_bindgen::spawn;
56f6a514bSRoman Volosatovs 
66f6a514bSRoman Volosatovs struct T;
76f6a514bSRoman Volosatovs 
8*1cc0bcffSBailey Hayes test_programs::p3::service::export!(T);
96f6a514bSRoman Volosatovs 
10*1cc0bcffSBailey Hayes impl test_programs::p3::service::exports::wasi::http::handler::Guest for T {
handle(request: Request) -> Result<Response, ErrorCode>116f6a514bSRoman Volosatovs     async fn handle(request: Request) -> Result<Response, ErrorCode> {
126f6a514bSRoman Volosatovs         assert!(request.get_scheme().is_some());
136f6a514bSRoman Volosatovs         assert!(request.get_authority().is_some());
146f6a514bSRoman Volosatovs         assert!(request.get_path_with_query().is_some());
156f6a514bSRoman Volosatovs 
166f6a514bSRoman Volosatovs         // TODO: adapt below
176f6a514bSRoman Volosatovs         //test_filesystem();
186f6a514bSRoman Volosatovs 
196f6a514bSRoman Volosatovs         let header = String::from("custom-forbidden-header");
206f6a514bSRoman Volosatovs         let req_hdrs = request.get_headers();
216f6a514bSRoman Volosatovs 
226f6a514bSRoman Volosatovs         assert!(
236f6a514bSRoman Volosatovs             !req_hdrs.has(&header),
246f6a514bSRoman Volosatovs             "forbidden `custom-forbidden-header` found in request"
256f6a514bSRoman Volosatovs         );
266f6a514bSRoman Volosatovs 
276f6a514bSRoman Volosatovs         assert!(req_hdrs.delete(&header).is_err());
286f6a514bSRoman Volosatovs         assert!(req_hdrs.append(&header, b"no".as_ref()).is_err());
296f6a514bSRoman Volosatovs 
306f6a514bSRoman Volosatovs         assert!(
316f6a514bSRoman Volosatovs             !req_hdrs.has(&header),
326f6a514bSRoman Volosatovs             "append of forbidden header succeeded"
336f6a514bSRoman Volosatovs         );
346f6a514bSRoman Volosatovs 
356f6a514bSRoman Volosatovs         let hdrs = Headers::new();
366f6a514bSRoman Volosatovs         let (mut contents_tx, contents_rx) = wit_stream::new();
376f6a514bSRoman Volosatovs         let (trailers_tx, trailers_rx) = wit_future::new(|| todo!());
386f6a514bSRoman Volosatovs         let (resp, transmit) = Response::new(hdrs, Some(contents_rx), trailers_rx);
396f6a514bSRoman Volosatovs         spawn(async {
406f6a514bSRoman Volosatovs             join!(
416f6a514bSRoman Volosatovs                 async {
426f6a514bSRoman Volosatovs                     let remaining = contents_tx.write_all(b"hello, world!".to_vec()).await;
436f6a514bSRoman Volosatovs                     assert!(remaining.is_empty());
446f6a514bSRoman Volosatovs                     drop(contents_tx);
456f6a514bSRoman Volosatovs                     trailers_tx
466f6a514bSRoman Volosatovs                         .write(Ok(None))
476f6a514bSRoman Volosatovs                         .await
486f6a514bSRoman Volosatovs                         .expect("failed to write trailers");
496f6a514bSRoman Volosatovs                 },
506f6a514bSRoman Volosatovs                 async { transmit.await.unwrap() }
516f6a514bSRoman Volosatovs             );
526f6a514bSRoman Volosatovs         });
536f6a514bSRoman Volosatovs         Ok(resp)
546f6a514bSRoman Volosatovs     }
556f6a514bSRoman Volosatovs }
566f6a514bSRoman Volosatovs 
57*1cc0bcffSBailey Hayes // Technically this should not be here for a service, but given the current
586f6a514bSRoman Volosatovs // framework for tests it's required since this file is built as a `bin`
main()596f6a514bSRoman Volosatovs fn main() {}
606f6a514bSRoman Volosatovs 
616f6a514bSRoman Volosatovs // TODO: adapt below
626f6a514bSRoman Volosatovs //fn test_filesystem() {
636f6a514bSRoman Volosatovs //    assert!(std::fs::File::open(".").is_err());
646f6a514bSRoman Volosatovs //}
65