1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-25 06:39:22 +02:00

fix h1 client for handling expect header request (#2049)

This commit is contained in:
fakeshadow
2021-03-07 10:33:16 -08:00
committed by GitHub
parent 78384c3ff5
commit 880b863f95
2 changed files with 121 additions and 23 deletions

View File

@ -1,8 +1,13 @@
use actix_http::{http, HttpService, Request, Response};
use actix_http::{
error, http, http::StatusCode, HttpMessage, HttpService, Request, Response,
};
use actix_http_test::test_server;
use actix_service::ServiceFactoryExt;
use bytes::Bytes;
use futures_util::future::{self, ok};
use futures_util::{
future::{self, ok},
StreamExt,
};
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
@ -88,3 +93,55 @@ async fn test_with_query_parameter() {
let response = request.send().await.unwrap();
assert!(response.status().is_success());
}
#[actix_rt::test]
async fn test_h1_expect() {
let srv = test_server(move || {
HttpService::build()
.expect(|req: Request| async {
if req.headers().contains_key("AUTH") {
Ok(req)
} else {
Err(error::ErrorExpectationFailed("expect failed"))
}
})
.h1(|req: Request| async move {
let (_, mut body) = req.into_parts();
let mut buf = Vec::new();
while let Some(Ok(chunk)) = body.next().await {
buf.extend_from_slice(&chunk);
}
let str = std::str::from_utf8(&buf).unwrap();
assert_eq!(str, "expect body");
Ok::<_, ()>(Response::Ok().finish())
})
.tcp()
})
.await;
// test expect without payload.
let request = srv
.request(http::Method::GET, srv.url("/"))
.insert_header(("Expect", "100-continue"));
let response = request.send().await;
assert!(response.is_err());
// test expect would fail to continue
let request = srv
.request(http::Method::GET, srv.url("/"))
.insert_header(("Expect", "100-continue"));
let response = request.send_body("expect body").await.unwrap();
assert_eq!(response.status(), StatusCode::EXPECTATION_FAILED);
// test exepct would continue
let request = srv
.request(http::Method::GET, srv.url("/"))
.insert_header(("Expect", "100-continue"))
.insert_header(("AUTH", "996"));
let response = request.send_body("expect body").await.unwrap();
assert!(response.status().is_success());
}