mirror of
https://github.com/tokio-rs/axum.git
synced 2025-01-11 12:31:25 +01:00
Add testing example with real HTTP server
This commit is contained in:
parent
9a419de290
commit
6d483a623a
1 changed files with 31 additions and 0 deletions
|
@ -36,6 +36,7 @@ mod tests {
|
|||
use super::*;
|
||||
use http::StatusCode;
|
||||
use serde_json::{json, Value};
|
||||
use std::net::{SocketAddr, TcpListener};
|
||||
use tower::ServiceExt; // for `app.oneshot()`
|
||||
|
||||
#[tokio::test]
|
||||
|
@ -98,4 +99,34 @@ mod tests {
|
|||
let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
|
||||
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…
Reference in a new issue