From 300535b1c92020a62885a3aa4d18f13f440c8d55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20S=C3=B6derberg?= Date: Wed, 30 Oct 2024 10:59:25 -0600 Subject: [PATCH 1/2] Adding authority to Requests for both client and server. --- test-programs/src/bin/client_get_authority.rs | 14 ++++++++++++++ test-programs/src/bin/server_authority.rs | 16 ++++++++++++++++ waki/src/request.rs | 7 ++++++- waki/tests/all/client.rs | 7 +++++++ waki/tests/all/server.rs | 15 +++++++++++++++ 5 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 test-programs/src/bin/client_get_authority.rs create mode 100644 test-programs/src/bin/server_authority.rs diff --git a/test-programs/src/bin/client_get_authority.rs b/test-programs/src/bin/client_get_authority.rs new file mode 100644 index 0000000..d0bb8b5 --- /dev/null +++ b/test-programs/src/bin/client_get_authority.rs @@ -0,0 +1,14 @@ +use waki::Client; + +fn main() { + let req = Client::new() + .get("https://httpbin.org/get?a=b") + .query(&[("c", "d")]) + .build() + .unwrap(); + + match req.authority() { + Some(authority) => assert_eq!(authority.as_str(), "httpbin.org"), + None => assert!(false, "Authority isn't set on client-request"), + } +} diff --git a/test-programs/src/bin/server_authority.rs b/test-programs/src/bin/server_authority.rs new file mode 100644 index 0000000..abb0966 --- /dev/null +++ b/test-programs/src/bin/server_authority.rs @@ -0,0 +1,16 @@ +use waki::{handler, ErrorCode, Request, Response}; + +#[handler] +fn hello(req: Request) -> Result { + let authority = req.authority(); + + match authority { + Some(authority) => Response::builder() + .body(format!("Hello, {}!", authority.as_str())) + .build(), + None => Response::builder().body("Hello!").build(), + } +} + +// required since this file is built as a `bin` +fn main() {} diff --git a/waki/src/request.rs b/waki/src/request.rs index 98f79c8..63c8e5c 100644 --- a/waki/src/request.rs +++ b/waki/src/request.rs @@ -10,7 +10,7 @@ use crate::{ use anyhow::{anyhow, Error, Result}; use http::{ - uri::{Parts, PathAndQuery}, + uri::{Authority, Parts, PathAndQuery}, Uri, }; use std::borrow::Borrow; @@ -201,6 +201,11 @@ impl Request { } } + /// Get the authority of the request. + pub fn authority(&self) -> &Option { + &self.uri.authority + } + fn send(self) -> Result { let req = OutgoingRequest::new(self.headers.try_into()?); req.set_method(&self.method) diff --git a/waki/tests/all/client.rs b/waki/tests/all/client.rs index 00eec12..14e2b39 100644 --- a/waki/tests/all/client.rs +++ b/waki/tests/all/client.rs @@ -1,5 +1,12 @@ use super::run_wasi; +#[tokio::test(flavor = "multi_thread")] +async fn get_authority() { + run_wasi(test_programs_artifacts::CLIENT_GET_AUTHORITY_COMPONENT) + .await + .unwrap(); +} + #[tokio::test(flavor = "multi_thread")] async fn get_chunk() { run_wasi(test_programs_artifacts::CLIENT_GET_CHUNK_COMPONENT) diff --git a/waki/tests/all/server.rs b/waki/tests/all/server.rs index 6a427e0..83081e5 100644 --- a/waki/tests/all/server.rs +++ b/waki/tests/all/server.rs @@ -88,6 +88,21 @@ async fn status_code() -> Result<()> { Ok(()) } +#[tokio::test(flavor = "multi_thread")] +async fn authority() -> Result<()> { + let req = hyper::Request::builder() + .uri("http://127.0.0.1:3000") + .body(body::empty())?; + + let resp: http::Response> = + run_wasi_http(test_programs_artifacts::SERVER_AUTHORITY_COMPONENT, req).await??; + let body = resp.into_body().to_bytes(); + let body = std::str::from_utf8(&body)?; + assert_eq!(body, "Hello, 127.0.0.1:3000!"); + + Ok(()) +} + mod body { use http_body_util::{combinators::BoxBody, BodyExt, Empty, Full}; use hyper::{body::Bytes, Error}; From f44b4353dc52d8473df3c97e5edbc5de8e1c3ad2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20S=C3=B6derberg?= Date: Thu, 31 Oct 2024 07:37:43 -0600 Subject: [PATCH 2/2] Removed unnecessary client test for authority --- test-programs/src/bin/client_get_authority.rs | 14 -------------- waki/tests/all/client.rs | 7 ------- 2 files changed, 21 deletions(-) delete mode 100644 test-programs/src/bin/client_get_authority.rs diff --git a/test-programs/src/bin/client_get_authority.rs b/test-programs/src/bin/client_get_authority.rs deleted file mode 100644 index d0bb8b5..0000000 --- a/test-programs/src/bin/client_get_authority.rs +++ /dev/null @@ -1,14 +0,0 @@ -use waki::Client; - -fn main() { - let req = Client::new() - .get("https://httpbin.org/get?a=b") - .query(&[("c", "d")]) - .build() - .unwrap(); - - match req.authority() { - Some(authority) => assert_eq!(authority.as_str(), "httpbin.org"), - None => assert!(false, "Authority isn't set on client-request"), - } -} diff --git a/waki/tests/all/client.rs b/waki/tests/all/client.rs index 14e2b39..00eec12 100644 --- a/waki/tests/all/client.rs +++ b/waki/tests/all/client.rs @@ -1,12 +1,5 @@ use super::run_wasi; -#[tokio::test(flavor = "multi_thread")] -async fn get_authority() { - run_wasi(test_programs_artifacts::CLIENT_GET_AUTHORITY_COMPONENT) - .await - .unwrap(); -} - #[tokio::test(flavor = "multi_thread")] async fn get_chunk() { run_wasi(test_programs_artifacts::CLIENT_GET_CHUNK_COMPONENT)