mirror of
https://github.com/tokio-rs/axum.git
synced 2026-09-08 00:00:24 +02:00
examples: Simplify Request and Response construction (#3653)
This commit is contained in:
@@ -70,7 +70,7 @@ mod tests {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_main_page() {
|
async fn test_main_page() {
|
||||||
let response = app()
|
let response = app()
|
||||||
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
|
.oneshot(Request::get("/").body(Body::empty()).unwrap())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ mod tests {
|
|||||||
let app = app();
|
let app = app();
|
||||||
|
|
||||||
let response = app
|
let response = app
|
||||||
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
|
.oneshot(Request::get("/").body(Body::empty()).unwrap())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@@ -107,9 +107,7 @@ mod tests {
|
|||||||
|
|
||||||
let response = app
|
let response = app
|
||||||
.oneshot(
|
.oneshot(
|
||||||
Request::builder()
|
Request::post("/")
|
||||||
.method(http::Method::POST)
|
|
||||||
.uri("/")
|
|
||||||
.header(
|
.header(
|
||||||
http::header::CONTENT_TYPE,
|
http::header::CONTENT_TYPE,
|
||||||
mime::APPLICATION_WWW_FORM_URLENCODED.as_ref(),
|
mime::APPLICATION_WWW_FORM_URLENCODED.as_ref(),
|
||||||
|
|||||||
@@ -29,8 +29,7 @@ use http::Request;
|
|||||||
use tower_service::Service;
|
use tower_service::Service;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let request: Request<String> = Request::builder()
|
let request: Request<String> = Request::get("https://serverless.example/api/")
|
||||||
.uri("https://serverless.example/api/")
|
|
||||||
.body("Some Body Data".into())
|
.body("Some Body Data".into())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|||||||
@@ -81,12 +81,7 @@ mod tests {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_main() {
|
async fn test_main() {
|
||||||
let response = app()
|
let response = app()
|
||||||
.oneshot(
|
.oneshot(Request::get("/greet/Foo").body(Body::empty()).unwrap())
|
||||||
Request::builder()
|
|
||||||
.uri("/greet/Foo")
|
|
||||||
.body(Body::empty())
|
|
||||||
.unwrap(),
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(response.status(), StatusCode::OK);
|
assert_eq!(response.status(), StatusCode::OK);
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ mod tests {
|
|||||||
// `Router` implements `tower::Service<Request<Body>>` so we can
|
// `Router` implements `tower::Service<Request<Body>>` so we can
|
||||||
// call it like any tower service, no need to run an HTTP server.
|
// call it like any tower service, no need to run an HTTP server.
|
||||||
let response = app
|
let response = app
|
||||||
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
|
.oneshot(Request::get("/").body(Body::empty()).unwrap())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@@ -87,9 +87,7 @@ mod tests {
|
|||||||
|
|
||||||
let response = app
|
let response = app
|
||||||
.oneshot(
|
.oneshot(
|
||||||
Request::builder()
|
Request::post("/json")
|
||||||
.method(http::Method::POST)
|
|
||||||
.uri("/json")
|
|
||||||
.header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
|
.header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
|
||||||
.body(Body::from(
|
.body(Body::from(
|
||||||
serde_json::to_vec(&json!([1, 2, 3, 4])).unwrap(),
|
serde_json::to_vec(&json!([1, 2, 3, 4])).unwrap(),
|
||||||
@@ -111,12 +109,7 @@ mod tests {
|
|||||||
let app = app();
|
let app = app();
|
||||||
|
|
||||||
let response = app
|
let response = app
|
||||||
.oneshot(
|
.oneshot(Request::get("/does-not-exist").body(Body::empty()).unwrap())
|
||||||
Request::builder()
|
|
||||||
.uri("/does-not-exist")
|
|
||||||
.body(Body::empty())
|
|
||||||
.unwrap(),
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@@ -141,8 +134,7 @@ mod tests {
|
|||||||
|
|
||||||
let response = client
|
let response = client
|
||||||
.request(
|
.request(
|
||||||
Request::builder()
|
Request::get(format!("http://{addr}"))
|
||||||
.uri(format!("http://{addr}"))
|
|
||||||
.header("Host", "localhost")
|
.header("Host", "localhost")
|
||||||
.body(Body::empty())
|
.body(Body::empty())
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
@@ -160,7 +152,7 @@ mod tests {
|
|||||||
async fn multiple_request() {
|
async fn multiple_request() {
|
||||||
let mut app = app().into_service();
|
let mut app = app().into_service();
|
||||||
|
|
||||||
let request = Request::builder().uri("/").body(Body::empty()).unwrap();
|
let request = Request::get("/").body(Body::empty()).unwrap();
|
||||||
let response = ServiceExt::<Request<Body>>::ready(&mut app)
|
let response = ServiceExt::<Request<Body>>::ready(&mut app)
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@@ -169,7 +161,7 @@ mod tests {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(response.status(), StatusCode::OK);
|
assert_eq!(response.status(), StatusCode::OK);
|
||||||
|
|
||||||
let request = Request::builder().uri("/").body(Body::empty()).unwrap();
|
let request = Request::get("/").body(Body::empty()).unwrap();
|
||||||
let response = ServiceExt::<Request<Body>>::ready(&mut app)
|
let response = ServiceExt::<Request<Body>>::ready(&mut app)
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@@ -190,8 +182,7 @@ mod tests {
|
|||||||
.layer(MockConnectInfo(SocketAddr::from(([0, 0, 0, 0], 3000))))
|
.layer(MockConnectInfo(SocketAddr::from(([0, 0, 0, 0], 3000))))
|
||||||
.into_service();
|
.into_service();
|
||||||
|
|
||||||
let request = Request::builder()
|
let request = Request::get("/requires-connect-info")
|
||||||
.uri("/requires-connect-info")
|
|
||||||
.body(Body::empty())
|
.body(Body::empty())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let response = app.ready().await.unwrap().call(request).await.unwrap();
|
let response = app.ready().await.unwrap().call(request).await.unwrap();
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ mod unix {
|
|||||||
use axum::{
|
use axum::{
|
||||||
body::Body,
|
body::Body,
|
||||||
extract::connect_info::{self, ConnectInfo},
|
extract::connect_info::{self, ConnectInfo},
|
||||||
http::{Method, Request, StatusCode},
|
http::{Request, StatusCode},
|
||||||
routing::get,
|
routing::get,
|
||||||
serve::IncomingStream,
|
serve::IncomingStream,
|
||||||
Router,
|
Router,
|
||||||
@@ -63,9 +63,7 @@ mod unix {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let request = Request::builder()
|
let request = Request::get("http://uri-doesnt-matter.com")
|
||||||
.method(Method::GET)
|
|
||||||
.uri("http://uri-doesnt-matter.com")
|
|
||||||
.body(Body::empty())
|
.body(Body::empty())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|||||||
@@ -115,7 +115,7 @@ mod tests {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_no_param() {
|
async fn test_no_param() {
|
||||||
let response = app()
|
let response = app()
|
||||||
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
|
.oneshot(Request::get("/").body(Body::empty()).unwrap())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@@ -127,12 +127,7 @@ mod tests {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_with_param_without_value() {
|
async fn test_with_param_without_value() {
|
||||||
let response = app()
|
let response = app()
|
||||||
.oneshot(
|
.oneshot(Request::get("/?name=").body(Body::empty()).unwrap())
|
||||||
Request::builder()
|
|
||||||
.uri("/?name=")
|
|
||||||
.body(Body::empty())
|
|
||||||
.unwrap(),
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@@ -144,12 +139,7 @@ mod tests {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_with_param_with_short_value() {
|
async fn test_with_param_with_short_value() {
|
||||||
let response = app()
|
let response = app()
|
||||||
.oneshot(
|
.oneshot(Request::get("/?name=X").body(Body::empty()).unwrap())
|
||||||
Request::builder()
|
|
||||||
.uri("/?name=X")
|
|
||||||
.body(Body::empty())
|
|
||||||
.unwrap(),
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@@ -161,12 +151,7 @@ mod tests {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_with_param_and_value() {
|
async fn test_with_param_and_value() {
|
||||||
let response = app()
|
let response = app()
|
||||||
.oneshot(
|
.oneshot(Request::get("/?name=LT").body(Body::empty()).unwrap())
|
||||||
Request::builder()
|
|
||||||
.uri("/?name=LT")
|
|
||||||
.body(Body::empty())
|
|
||||||
.unwrap(),
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|||||||
@@ -83,12 +83,7 @@ mod tests {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_v1() {
|
async fn test_v1() {
|
||||||
let response = app()
|
let response = app()
|
||||||
.oneshot(
|
.oneshot(Request::get("/v1/foo").body(Body::empty()).unwrap())
|
||||||
Request::builder()
|
|
||||||
.uri("/v1/foo")
|
|
||||||
.body(Body::empty())
|
|
||||||
.unwrap(),
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@@ -103,12 +98,7 @@ mod tests {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_v4() {
|
async fn test_v4() {
|
||||||
let response = app()
|
let response = app()
|
||||||
.oneshot(
|
.oneshot(Request::get("/v4/foo").body(Body::empty()).unwrap())
|
||||||
Request::builder()
|
|
||||||
.uri("/v4/foo")
|
|
||||||
.body(Body::empty())
|
|
||||||
.unwrap(),
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user