Path: blob/main/crates/test-programs/src/bin/api_proxy.rs
1693 views
use test_programs::wasi::http::types::{1Headers, IncomingRequest, Method, OutgoingBody, OutgoingResponse, ResponseOutparam,2};34struct T;56test_programs::proxy::export!(T);78impl test_programs::proxy::exports::wasi::http::incoming_handler::Guest for T {9fn handle(request: IncomingRequest, outparam: ResponseOutparam) {10assert!(request.scheme().is_some());11assert!(request.authority().is_some());12assert!(request.path_with_query().is_some());1314test_filesystem();1516match (request.method(), request.path_with_query().as_deref()) {17(Method::Get, Some("/early_drop")) => {18// Ignore all the errors for this endpoint.19let resp = OutgoingResponse::new(Headers::new());20let body = resp.body().expect("outgoing response");21ResponseOutparam::set(outparam, Ok(resp));22let _ = body.write().and_then(|out| {23let _ = out.blocking_write_and_flush(b"hello, world!");24drop(out);25Ok(())26});27let _ = OutgoingBody::finish(body, None);2829return;30}3132_ => {}33}3435let header = String::from("custom-forbidden-header");36let req_hdrs = request.headers();3738assert!(39!req_hdrs.has(&header),40"forbidden `custom-forbidden-header` found in request"41);4243assert!(req_hdrs.delete(&header).is_err());44assert!(req_hdrs.append(&header, b"no".as_ref()).is_err());4546assert!(47!req_hdrs.has(&header),48"append of forbidden header succeeded"49);5051assert!(52!req_hdrs.has("host"),53"forbidden host header present in incoming request"54);5556let hdrs = Headers::new();57let resp = OutgoingResponse::new(hdrs);58let body = resp.body().expect("outgoing response");5960ResponseOutparam::set(outparam, Ok(resp));6162let out = body.write().expect("outgoing stream");63out.blocking_write_and_flush(b"hello, world!")64.expect("writing response");6566drop(out);67OutgoingBody::finish(body, None).expect("outgoing-body.finish");68}69}7071// Technically this should not be here for a proxy, but given the current72// framework for tests it's required since this file is built as a `bin`73fn main() {}7475fn test_filesystem() {76assert!(std::fs::File::open(".").is_err());77}787980