tests for following redirects

This commit is contained in:
Thiago Arrais 2020-12-09 13:58:15 -03:00
parent 0af013a972
commit d1727104c7
2 changed files with 55 additions and 5 deletions

View File

@ -142,13 +142,10 @@ where
Ok((resphead, payload)) => { Ok((resphead, payload)) => {
if_chain! { if_chain! {
if resphead.status.is_redirection(); if resphead.status.is_redirection();
if redirect_count < max_redirects;
if let Some(location_value) = resphead.headers.get(actix_http::http::header::LOCATION); if let Some(location_value) = resphead.headers.get(actix_http::http::header::LOCATION);
if let Ok(location_str) = location_value.to_str(); if let Ok(location_str) = location_value.to_str();
then { then {
if redirect_count >= max_redirects {
// TODO: need a better error
return Err(SendRequestError::Timeout);
}
if resphead.status == actix_http::http::StatusCode::SEE_OTHER { if resphead.status == actix_http::http::StatusCode::SEE_OTHER {
reqhead.method = actix_http::http::Method::GET; reqhead.method = actix_http::http::Method::GET;
reqbody = Body::None; reqbody = Body::None;

View File

@ -3,7 +3,9 @@ use actix_http::HttpService;
use actix_http_test::test_server; use actix_http_test::test_server;
use actix_service::{map_config, ServiceFactory}; use actix_service::{map_config, ServiceFactory};
use actix_web::http::Version; use actix_web::http::Version;
use actix_web::{dev::AppConfig, web, App, HttpResponse}; use actix_web::{dev::AppConfig, test, web, App, HttpResponse};
use awc::http::StatusCode;
use bytes::Bytes;
use open_ssl::ssl::{SslAcceptor, SslConnector, SslFiletype, SslMethod, SslVerifyMode}; use open_ssl::ssl::{SslAcceptor, SslConnector, SslFiletype, SslMethod, SslVerifyMode};
fn ssl_acceptor() -> SslAcceptor { fn ssl_acceptor() -> SslAcceptor {
@ -58,3 +60,54 @@ async fn test_connection_window_size() {
assert!(response.status().is_success()); assert!(response.status().is_success());
assert_eq!(response.version(), Version::HTTP_2); assert_eq!(response.version(), Version::HTTP_2);
} }
#[actix_rt::test]
async fn test_follow_redirects() {
let srv = test::start(|| {
App::new()
.service(web::resource("/do/redirect").route(web::to(|| {
HttpResponse::TemporaryRedirect()
.header("Location", "get")
.finish()
})))
.service(web::resource("/do/get").route(web::to(HttpResponse::Ok)))
});
let client = awc::Client::builder().finish();
let request = client.get(srv.url("/do/redirect")).send();
let response = request.await.unwrap();
assert!(response.status().is_success());
assert_eq!(response.status(), StatusCode::OK);
}
#[actix_rt::test]
async fn test_max_redirects() {
let srv = test::start(|| {
App::new()
.service(web::resource("/first-redirect").route(web::to(|| {
HttpResponse::TemporaryRedirect()
.header("Location", "/second-redirect")
.body("first")
})))
.service(web::resource("/second-redirect").route(web::to(|| {
HttpResponse::TemporaryRedirect()
.header("Location", "/third-redirect")
.body("second")
})))
.service(web::resource("/third-redirect").route(web::to(|| {
HttpResponse::TemporaryRedirect()
.header("Location", "/the-content")
.body("third")
})))
.service(web::resource("/the-content").route(web::to(HttpResponse::Ok)))
});
let client = awc::Client::builder().max_redirects(2).finish();
let request = client.get(srv.url("/first-redirect")).send();
let mut response = request.await.unwrap();
assert!(response.status().is_redirection());
assert_eq!(response.status(), StatusCode::TEMPORARY_REDIRECT);
assert_eq!(response.body().await.unwrap(), Bytes::from("third"));
}