mirror of
https://github.com/tokio-rs/axum.git
synced 2025-10-02 15:24:54 +00:00
Add testing example with real HTTP server
This commit is contained in:
parent
9a419de290
commit
6d483a623a
@ -36,6 +36,7 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
use http::StatusCode;
|
use http::StatusCode;
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
|
use std::net::{SocketAddr, TcpListener};
|
||||||
use tower::ServiceExt; // for `app.oneshot()`
|
use tower::ServiceExt; // for `app.oneshot()`
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
@ -98,4 +99,34 @@ mod tests {
|
|||||||
let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
|
let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
|
||||||
assert!(body.is_empty());
|
assert!(body.is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// You can also spawn a server and talk to it like any other HTTP server:
|
||||||
|
#[tokio::test]
|
||||||
|
async fn the_real_deal() {
|
||||||
|
let listener = TcpListener::bind("0.0.0.0:0".parse::<SocketAddr>().unwrap()).unwrap();
|
||||||
|
let addr = listener.local_addr().unwrap();
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
hyper::Server::from_tcp(listener)
|
||||||
|
.unwrap()
|
||||||
|
.serve(app().into_make_service())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
});
|
||||||
|
|
||||||
|
let client = hyper::Client::new();
|
||||||
|
|
||||||
|
let response = client
|
||||||
|
.request(
|
||||||
|
Request::builder()
|
||||||
|
.uri(format!("http://{}", addr))
|
||||||
|
.body(Body::empty())
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
|
||||||
|
assert_eq!(&body[..], b"Hello, World!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user