From 900fd5a98e7bd1988dd7d8a504ccc31cc0fd4354 Mon Sep 17 00:00:00 2001 From: Douman Date: Sat, 4 Aug 2018 01:34:23 +0300 Subject: [PATCH] Correct settings headers for HTTP2 Add test to verify number of Set-Cookies --- src/server/h2writer.rs | 6 +++--- tests/test_server.rs | 44 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/server/h2writer.rs b/src/server/h2writer.rs index ff87b693e..511929fa8 100644 --- a/src/server/h2writer.rs +++ b/src/server/h2writer.rs @@ -112,7 +112,7 @@ impl Writer for H2Writer { DATE => has_date = true, _ => (), } - resp.headers_mut().insert(key, value.clone()); + resp.headers_mut().append(key, value.clone()); } // set date header @@ -151,6 +151,8 @@ impl Writer for H2Writer { .insert(CONTENT_ENCODING, HeaderValue::try_from(ce).unwrap()); } + trace!("Response: {:?}", resp); + match self .respond .send_response(resp, self.flags.contains(Flags::EOF)) @@ -159,8 +161,6 @@ impl Writer for H2Writer { Err(_) => return Err(io::Error::new(io::ErrorKind::Other, "err")), } - trace!("Response: {:?}", msg); - let body = msg.replace_body(Body::Empty); if let Body::Binary(bytes) = body { if bytes.is_empty() { diff --git a/tests/test_server.rs b/tests/test_server.rs index 4db73a3be..5c4385680 100644 --- a/tests/test_server.rs +++ b/tests/test_server.rs @@ -931,3 +931,47 @@ fn test_application() { let response = srv.execute(request.send()).unwrap(); assert!(response.status().is_success()); } + +#[test] +fn test_server_cookies() { + use actix_web::http; + + let mut srv = test::TestServer::with_factory(|| { + App::new().resource("/", |r| r.f(|_| HttpResponse::Ok().cookie(http::CookieBuilder::new("first", "first_value").http_only(true).finish()) + .cookie(http::Cookie::new("second", "first_value")) + .cookie(http::Cookie::new("second", "second_value")) + .finish()) + ) + }); + + let first_cookie = http::CookieBuilder::new("first", "first_value").http_only(true).finish(); + let second_cookie = http::Cookie::new("second", "second_value"); + + let request = srv.get().finish().unwrap(); + let response = srv.execute(request.send()).unwrap(); + assert!(response.status().is_success()); + + let cookies = response.cookies().expect("To have cookies"); + assert_eq!(cookies.len(), 2); + if cookies[0] == first_cookie { + assert_eq!(cookies[1], second_cookie); + } else { + assert_eq!(cookies[0], second_cookie); + assert_eq!(cookies[1], first_cookie); + } + + let first_cookie = first_cookie.to_string(); + let second_cookie = second_cookie.to_string(); + //Check that we have exactly two instances of raw cookie headers + let cookies = response.headers().get_all(http::header::SET_COOKIE) + .iter() + .map(|header| header.to_str().expect("To str").to_string()) + .collect::>(); + assert_eq!(cookies.len(), 2); + if cookies[0] == first_cookie { + assert_eq!(cookies[1], second_cookie); + } else { + assert_eq!(cookies[0], second_cookie); + assert_eq!(cookies[1], first_cookie); + } +}