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

Initial prep for [email protected]. #10

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
license = "Apache-2.0"

[dependencies]
axum = { version = "0.6.16", features = ["headers", "http2"] }
axum = { git="https://github.com/tokio-rs/axum.git", branch="david/hyper-1.0-rc.x", features = ["headers", "http2"] }
mime = "0.3"
minijinja = "0.32.0"
rand = "0.8.5"
Expand All @@ -17,4 +17,5 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] }

[dev-dependencies]
hyper = { version = "0.14", features = ["full"] }
http-body-util = "0.1.0-rc.2"
tower = { version = "0.4", features = ["util"] }
11 changes: 7 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ async fn main() {
let addr = SocketAddr::from(([127, 0, 0, 1], port));

tracing::info!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(server::app().into_make_service_with_connect_info::<SocketAddr>())
.await
.unwrap();
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
axum::serve(
listener,
server::app().into_make_service_with_connect_info::<SocketAddr>(),
)
.await
.unwrap();
}
7 changes: 4 additions & 3 deletions src/routes/request_inspection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ mod tests {
extract::connect_info::MockConnectInfo,
http::{header, HeaderValue, Request, StatusCode},
};
use http_body_util::BodyExt;
use std::net::SocketAddr;
use tower::ServiceExt;

Expand All @@ -68,7 +69,7 @@ mod tests {
Some(&HeaderValue::from_static(mime::APPLICATION_JSON.as_ref()))
);

let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
let body = response.collect().await.unwrap().to_bytes();
let response_json = serde_json::from_slice::<Value>(&body.to_vec()).unwrap();
let headers = Value::as_object(&response_json["headers"]).unwrap();
assert_eq!(headers["foo"], "value-foo");
Expand All @@ -90,7 +91,7 @@ mod tests {
Some(&HeaderValue::from_static(mime::APPLICATION_JSON.as_ref()))
);

let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
let body = response.collect().await.unwrap().to_bytes();
let response_json = serde_json::from_slice::<Value>(&body.to_vec()).unwrap();
assert_eq!(&response_json["origin"], "10.10.32.1");
}
Expand All @@ -116,7 +117,7 @@ mod tests {
Some(&HeaderValue::from_static(mime::APPLICATION_JSON.as_ref()))
);

let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
let body = response.collect().await.unwrap().to_bytes();
let response_json = serde_json::from_slice::<Value>(&body.to_vec()).unwrap();
assert_eq!(&response_json["user_agent"], "foo-bar");
}
Expand Down
3 changes: 2 additions & 1 deletion src/routes/response_formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod tests {
body::Body,
http::{header, HeaderValue, Request, StatusCode},
};
use http_body_util::BodyExt;
use tower::ServiceExt;

#[tokio::test]
Expand All @@ -39,7 +40,7 @@ mod tests {
Some(&HeaderValue::from_static(mime::TEXT_HTML_UTF_8.as_ref()))
);

let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
let body = response.collect().await.unwrap().to_bytes();
assert!(std::str::from_utf8(&body).is_ok())
}
}
29 changes: 12 additions & 17 deletions src/routes/status_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ mod tests {
body::Body,
http::{header, HeaderValue, Method, Request, StatusCode},
};
use tower::{Service, ServiceExt};
use http_body_util::BodyExt;
use tower::ServiceExt;

#[tokio::test]
async fn selects_a_single_status_code() {
Expand All @@ -156,7 +157,7 @@ mod tests {

#[tokio::test]
async fn supports_multiple_http_methods() {
let mut app = routes();
let app = routes();

let methods = vec![
Method::GET,
Expand All @@ -170,10 +171,8 @@ mod tests {

for method in methods {
let response = app
.ready()
.await
.unwrap()
.call(
.clone()
.oneshot(
Request::builder()
.method(method)
.uri("/status/200")
Expand Down Expand Up @@ -237,18 +236,16 @@ mod tests {

#[tokio::test]
async fn chooses_a_higher_weighted_random_status_code_more_often() {
let mut app = routes();
let app = routes();
let mut ok_returns: u16 = 0;
let mut created_returns: u16 = 0;
let mut accepted_returns: u16 = 0;
let mut no_content_returns: u16 = 0;

for _num in 0..1000 {
let response = app
.ready()
.await
.unwrap()
.call(
.clone()
.oneshot(
Request::builder()
.uri("/status/200:0.1,201:0.25,202:0.75,204:1")
.body(Body::empty())
Expand Down Expand Up @@ -318,7 +315,7 @@ mod tests {

#[tokio::test]
async fn redirects_have_location_header() {
let mut app = routes();
let app = routes();

let redirects = vec![
StatusCode::MOVED_PERMANENTLY,
Expand All @@ -330,10 +327,8 @@ mod tests {

for redirect in redirects {
let response = app
.ready()
.await
.unwrap()
.call(
.clone()
.oneshot(
Request::builder()
.uri(format!("/status/{}", redirect.as_str()))
.body(Body::empty())
Expand Down Expand Up @@ -444,7 +439,7 @@ mod tests {
Some(&HeaderValue::from_static(mime::TEXT_PLAIN.as_ref()))
);

let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
let body = response.collect().await.unwrap().to_bytes();
assert!(std::str::from_utf8(&body).is_ok())
}
}
7 changes: 4 additions & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::routes::{request_inspection, response_formats, root, status_codes};
use axum::{
http::{header, HeaderValue, Method, Request, StatusCode},
extract::Request,
http::{header, HeaderValue, Method, StatusCode},
middleware::{from_fn, Next},
response::Response,
Router,
Expand All @@ -17,7 +18,7 @@ pub fn app() -> Router {
.layer(from_fn(inject_cors_headers))
}

async fn inject_server_header<B>(request: Request<B>, next: Next<B>) -> Response {
async fn inject_server_header(request: Request, next: Next) -> Response {
let mut response = next.run(request).await;

let headers = response.headers_mut();
Expand All @@ -28,7 +29,7 @@ async fn inject_server_header<B>(request: Request<B>, next: Next<B>) -> Response
response
}

async fn inject_cors_headers<B>(request: Request<B>, next: Next<B>) -> Response {
async fn inject_cors_headers(request: Request, next: Next) -> Response {
let method = request.method().clone();
let request_headers = request.headers().clone();
let mut response = next.run(request).await;
Expand Down