aboutsummaryrefslogtreecommitdiffstats
path: root/components/net/tests
diff options
context:
space:
mode:
authorwebbeef <me@webbeef.org>2024-09-29 11:23:48 -0700
committerGitHub <noreply@github.com>2024-09-29 18:23:48 +0000
commit58f34ad7a3db8c633871d568bfcef8b094217e3e (patch)
tree43c3f128c54f5da5d80d5feeaca495950e2de144 /components/net/tests
parent013473f1d5a18f7d99183593ef370045dc58c978 (diff)
downloadservo-58f34ad7a3db8c633871d568bfcef8b094217e3e.tar.gz
servo-58f34ad7a3db8c633871d568bfcef8b094217e3e.zip
Create HttpStatus to safely deal with HTTP responses status. (#33581)
Signed-off-by: webbeef <me@webbeef.org>
Diffstat (limited to 'components/net/tests')
-rw-r--r--components/net/tests/fetch.rs12
-rw-r--r--components/net/tests/http_cache.rs2
-rw-r--r--components/net/tests/http_loader.rs84
3 files changed, 36 insertions, 62 deletions
diff --git a/components/net/tests/fetch.rs b/components/net/tests/fetch.rs
index 9ffe6817d81..03d2826fc01 100644
--- a/components/net/tests/fetch.rs
+++ b/components/net/tests/fetch.rs
@@ -31,6 +31,7 @@ use net::protocols::ProtocolRegistry;
use net::resource_thread::CoreResourceThreadPool;
use net::test::HttpState;
use net_traits::filemanager_thread::FileTokenCheck;
+use net_traits::http_status::HttpStatus;
use net_traits::request::{
Destination, Origin, RedirectMode, Referrer, Request, RequestBuilder, RequestMode,
};
@@ -642,7 +643,7 @@ fn test_fetch_response_is_opaque_filtered() {
assert!(fetch_response.url().is_none());
assert!(fetch_response.url_list.is_empty());
// this also asserts that status message is "the empty byte sequence"
- assert!(fetch_response.status.is_none());
+ assert!(fetch_response.status.is_error());
assert_eq!(fetch_response.headers, HeaderMap::new());
match *fetch_response.body.lock().unwrap() {
ResponseBody::Empty => {},
@@ -694,7 +695,7 @@ fn test_fetch_response_is_opaque_redirect_filtered() {
assert_eq!(fetch_response.response_type, ResponseType::OpaqueRedirect);
// this also asserts that status message is "the empty byte sequence"
- assert!(fetch_response.status.is_none());
+ assert!(fetch_response.status.is_error());
assert_eq!(fetch_response.headers, HeaderMap::new());
match *fetch_response.body.lock().unwrap() {
ResponseBody::Empty => {},
@@ -855,8 +856,7 @@ fn test_load_adds_host_to_hsts_list_when_url_is_https() {
.internal_response
.unwrap()
.status
- .unwrap()
- .0
+ .code()
.is_success());
assert!(context
.state
@@ -922,7 +922,7 @@ fn test_fetch_self_signed() {
let response = fetch_with_context(&mut request, &mut context);
- assert!(response.status.unwrap().0.is_success());
+ assert!(response.status.code().is_success());
let _ = server.close();
}
@@ -1392,7 +1392,7 @@ fn test_fetch_with_devtools() {
let httpresponse = DevtoolsHttpResponse {
headers: Some(response_headers),
- status: Some((200, b"OK".to_vec())),
+ status: HttpStatus::default(),
body: None,
pipeline_id: TEST_PIPELINE_ID,
};
diff --git a/components/net/tests/http_cache.rs b/components/net/tests/http_cache.rs
index 6d7848fad5e..9900295c891 100644
--- a/components/net/tests/http_cache.rs
+++ b/components/net/tests/http_cache.rs
@@ -39,7 +39,7 @@ fn test_refreshing_resource_sets_done_chan_the_appropriate_value() {
// First, store the 'normal' response.
cache.store(&request, &response);
// Second, mutate the response into a 304 response, and refresh the stored one.
- response.status = Some((StatusCode::NOT_MODIFIED, String::from("304")));
+ response.status = StatusCode::NOT_MODIFIED.into();
let (send, recv) = unbounded();
let mut done_chan = Some((send, recv));
let refreshed_response = cache.refresh(&request, response.clone(), &mut done_chan);
diff --git a/components/net/tests/http_loader.rs b/components/net/tests/http_loader.rs
index e514eabebf3..c56618833aa 100644
--- a/components/net/tests/http_loader.rs
+++ b/components/net/tests/http_loader.rs
@@ -35,6 +35,7 @@ use net::cookie_storage::CookieStorage;
use net::http_loader::determine_requests_referrer;
use net::resource_thread::AuthCacheEntry;
use net::test::replace_host_table;
+use net_traits::http_status::HttpStatus;
use net_traits::request::{
BodyChunkRequest, BodyChunkResponse, BodySource, CredentialsMode, Destination, Referrer,
RequestBody, RequestBuilder,
@@ -161,8 +162,7 @@ fn test_check_default_headers_loaded_in_every_request() {
.internal_response
.unwrap()
.status
- .unwrap()
- .0
+ .code()
.is_success());
// Testing for method.POST
@@ -188,8 +188,7 @@ fn test_check_default_headers_loaded_in_every_request() {
.internal_response
.unwrap()
.status
- .unwrap()
- .0
+ .code()
.is_success());
let _ = server.close();
@@ -219,8 +218,7 @@ fn test_load_when_request_is_not_get_or_head_and_there_is_no_body_content_length
.internal_response
.unwrap()
.status
- .unwrap()
- .0
+ .code()
.is_success());
let _ = server.close();
@@ -253,8 +251,7 @@ fn test_request_and_response_data_with_network_messages() {
.internal_response
.unwrap()
.status
- .unwrap()
- .0
+ .code()
.is_success());
let _ = server.close();
@@ -312,7 +309,7 @@ fn test_request_and_response_data_with_network_messages() {
let httpresponse = DevtoolsHttpResponse {
headers: Some(response_headers),
- status: Some((200, b"OK".to_vec())),
+ status: HttpStatus::default(),
body: None,
pipeline_id: TEST_PIPELINE_ID,
};
@@ -341,13 +338,7 @@ fn test_request_and_response_message_from_devtool_without_pipeline_id() {
let (devtools_chan, devtools_port) = unbounded();
let response = fetch(&mut request, Some(devtools_chan));
- assert!(response
- .actual_response()
- .status
- .as_ref()
- .unwrap()
- .0
- .is_success());
+ assert!(response.actual_response().status.code().is_success());
let _ = server.close();
@@ -393,7 +384,7 @@ fn test_redirected_request_to_devtools() {
assert_eq!(devhttprequest.url, pre_url);
assert_eq!(
devhttpresponse.status,
- Some((301, b"Moved Permanently".to_vec()))
+ HttpStatus::from(StatusCode::MOVED_PERMANENTLY)
);
let devhttprequest = expect_devtools_http_request(&devtools_port);
@@ -401,7 +392,7 @@ fn test_redirected_request_to_devtools() {
assert_eq!(devhttprequest.method, Method::GET);
assert_eq!(devhttprequest.url, post_url);
- assert_eq!(devhttpresponse.status, Some((200, b"OK".to_vec())));
+ assert_eq!(devhttpresponse.status, HttpStatus::default());
}
#[test]
@@ -435,7 +426,7 @@ fn test_load_when_redirecting_from_a_post_should_rewrite_next_request_as_get() {
let _ = pre_server.close();
let _ = post_server.close();
- assert!(response.to_actual().status.unwrap().0.is_success());
+ assert!(response.to_actual().status.code().is_success());
}
#[test]
@@ -466,7 +457,7 @@ fn test_load_should_decode_the_response_as_deflate_when_response_headers_have_co
let _ = server.close();
let internal_response = response.internal_response.unwrap();
- assert!(internal_response.status.clone().unwrap().0.is_success());
+ assert!(internal_response.status.clone().code().is_success());
assert_eq!(
*internal_response.body.lock().unwrap(),
ResponseBody::Done(b"Yay!".to_vec())
@@ -499,7 +490,7 @@ fn test_load_should_decode_the_response_as_gzip_when_response_headers_have_conte
let _ = server.close();
let internal_response = response.internal_response.unwrap();
- assert!(internal_response.status.clone().unwrap().0.is_success());
+ assert!(internal_response.status.clone().code().is_success());
assert_eq!(
*internal_response.body.lock().unwrap(),
ResponseBody::Done(b"Yay!".to_vec())
@@ -544,7 +535,7 @@ fn test_load_doesnt_send_request_body_on_any_redirect() {
let _ = pre_server.close();
let _ = post_server.close();
- assert!(response.to_actual().status.unwrap().0.is_success());
+ assert!(response.to_actual().status.code().is_success());
}
#[test]
@@ -576,8 +567,7 @@ fn test_load_doesnt_add_host_to_hsts_list_when_url_is_http_even_if_hsts_headers_
.internal_response
.unwrap()
.status
- .unwrap()
- .0
+ .code()
.is_success());
assert_eq!(
context
@@ -622,8 +612,7 @@ fn test_load_sets_cookies_in_the_resource_manager_when_it_get_set_cookie_header_
.internal_response
.unwrap()
.status
- .unwrap()
- .0
+ .code()
.is_success());
assert_cookie_for_domain(
@@ -674,8 +663,7 @@ fn test_load_sets_requests_cookies_header_for_url_by_getting_cookies_from_the_re
.internal_response
.unwrap()
.status
- .unwrap()
- .0
+ .code()
.is_success());
}
@@ -720,8 +708,7 @@ fn test_load_sends_cookie_if_nonhttp() {
.internal_response
.unwrap()
.status
- .unwrap()
- .0
+ .code()
.is_success());
}
@@ -758,8 +745,7 @@ fn test_cookie_set_with_httponly_should_not_be_available_using_getcookiesforurl(
.internal_response
.unwrap()
.status
- .unwrap()
- .0
+ .code()
.is_success());
assert_cookie_for_domain(
@@ -802,13 +788,7 @@ fn test_when_cookie_received_marked_secure_is_ignored_for_http() {
let _ = server.close();
- assert!(response
- .actual_response()
- .status
- .as_ref()
- .unwrap()
- .0
- .is_success());
+ assert!(response.actual_response().status.code().is_success());
assert_cookie_for_domain(&context.state.cookie_jar, url.as_str(), None);
}
@@ -844,8 +824,7 @@ fn test_load_sets_content_length_to_length_of_request_body() {
.internal_response
.unwrap()
.status
- .unwrap()
- .0
+ .code()
.is_success());
}
@@ -883,8 +862,7 @@ fn test_load_uses_explicit_accept_from_headers_in_load_data() {
.internal_response
.unwrap()
.status
- .unwrap()
- .0
+ .code()
.is_success());
}
@@ -919,8 +897,7 @@ fn test_load_sets_default_accept_to_html_xhtml_xml_and_then_anything_else() {
.internal_response
.unwrap()
.status
- .unwrap()
- .0
+ .code()
.is_success());
}
@@ -958,8 +935,7 @@ fn test_load_uses_explicit_accept_encoding_from_load_data_headers() {
.internal_response
.unwrap()
.status
- .unwrap()
- .0
+ .code()
.is_success());
}
@@ -994,8 +970,7 @@ fn test_load_sets_default_accept_encoding_to_gzip_and_deflate() {
.internal_response
.unwrap()
.status
- .unwrap()
- .0
+ .code()
.is_success());
}
@@ -1140,7 +1115,7 @@ fn test_load_follows_a_redirect() {
let _ = post_server.close();
let internal_response = response.internal_response.unwrap();
- assert!(internal_response.status.clone().unwrap().0.is_success());
+ assert!(internal_response.status.clone().code().is_success());
assert_eq!(
*internal_response.body.lock().unwrap(),
ResponseBody::Done(b"Yay!".to_vec())
@@ -1223,7 +1198,7 @@ fn test_redirect_from_x_to_y_provides_y_cookies_from_y() {
let _ = server.close();
let internal_response = response.internal_response.unwrap();
- assert!(internal_response.status.clone().unwrap().0.is_success());
+ assert!(internal_response.status.clone().code().is_success());
assert_eq!(
*internal_response.body.lock().unwrap(),
ResponseBody::Done(b"Yay!".to_vec())
@@ -1272,7 +1247,7 @@ fn test_redirect_from_x_to_x_provides_x_with_cookie_from_first_response() {
let _ = server.close();
let internal_response = response.internal_response.unwrap();
- assert!(internal_response.status.clone().unwrap().0.is_success());
+ assert!(internal_response.status.clone().code().is_success());
assert_eq!(
*internal_response.body.lock().unwrap(),
ResponseBody::Done(b"Yay!".to_vec())
@@ -1322,8 +1297,7 @@ fn test_if_auth_creds_not_in_url_but_in_cache_it_sets_it() {
.internal_response
.unwrap()
.status
- .unwrap()
- .0
+ .code()
.is_success());
}
@@ -1348,7 +1322,7 @@ fn test_auth_ui_needs_www_auth() {
let _ = server.close();
assert_eq!(
- response.internal_response.unwrap().status.unwrap().0,
+ response.internal_response.unwrap().status,
StatusCode::UNAUTHORIZED
);
}