Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding authority to Requests for both client and server. #30

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions test-programs/src/bin/client_get_authority.rs
Original file line number Diff line number Diff line change
@@ -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"),
}
iawia002 marked this conversation as resolved.
Show resolved Hide resolved
}
16 changes: 16 additions & 0 deletions test-programs/src/bin/server_authority.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use waki::{handler, ErrorCode, Request, Response};

#[handler]
fn hello(req: Request) -> Result<Response, ErrorCode> {
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() {}
7 changes: 6 additions & 1 deletion waki/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -201,6 +201,11 @@ impl Request {
}
}

/// Get the authority of the request.
pub fn authority(&self) -> &Option<Authority> {
&self.uri.authority
}

fn send(self) -> Result<Response> {
let req = OutgoingRequest::new(self.headers.try_into()?);
req.set_method(&self.method)
Expand Down
7 changes: 7 additions & 0 deletions waki/tests/all/client.rs
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
15 changes: 15 additions & 0 deletions waki/tests/all/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<http_body_util::Collected<bytes::Bytes>> =
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};
Expand Down