Skip to content

Commit

Permalink
Adding authority to Requests for both client and server. (#30)
Browse files Browse the repository at this point in the history
* Adding authority to Requests for both client and server.

* Removed unnecessary client test for authority

---------

Co-authored-by: Andreas Söderberg <andreas.soderber@nor2.io>
  • Loading branch information
usergood and Andreas Söderberg authored Oct 31, 2024
1 parent 07d17c7 commit 7fe3922
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
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
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

0 comments on commit 7fe3922

Please sign in to comment.