Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite some http unit tests with fetch. #14149

Merged
merged 5 commits into from Nov 9, 2016

Rewrite test_load_doesnt_send_request_body_on_any_redirect.

Note that it is necessary to use POST here; using GET will cause an error in
hyper, which enforces the rule that GET requests don't have a body.
  • Loading branch information
Ms2ger committed Nov 9, 2016
commit f03a6001de6dc92bd503370d0f4e7a06d9f2f34b
@@ -309,27 +309,6 @@ impl HttpRequestFactory for AssertMustNotIncludeHeadersRequestFactory {
}
}

struct AssertMustHaveBodyRequest {
expected_body: Option<Vec<u8>>,
t: ResponseType
}

impl AssertMustHaveBodyRequest {
fn new(t: ResponseType, expected_body: Option<Vec<u8>>) -> Self {
AssertMustHaveBodyRequest { expected_body: expected_body, t: t }
}
}

impl HttpRequest for AssertMustHaveBodyRequest {
type R = MockResponse;

fn send(self, body: &Option<Vec<u8>>) -> Result<MockResponse, LoadError> {
assert_eq!(self.expected_body, *body);

response_for_request_type(self.t)
}
}

pub fn expect_devtools_http_request(devtools_port: &Receiver<DevtoolsControlMsg>) -> DevtoolsHttpRequest {
match devtools_port.recv().unwrap() {
DevtoolsControlMsg::FromChrome(
@@ -715,45 +694,39 @@ fn test_load_should_decode_the_response_as_gzip_when_response_headers_have_conte

#[test]
fn test_load_doesnt_send_request_body_on_any_redirect() {
struct Factory;

impl HttpRequestFactory for Factory {
type R = AssertMustHaveBodyRequest;

fn create(&self, url: Url, _: Method, _: Headers) -> Result<AssertMustHaveBodyRequest, LoadError> {
if url.domain().unwrap() == "mozilla.com" {
Ok(
AssertMustHaveBodyRequest::new(
ResponseType::Redirect("http://mozilla.org".to_owned()),
Some(<[_]>::to_vec("Body on POST!".as_bytes()))
)
)
} else {
Ok(
AssertMustHaveBodyRequest::new(
ResponseType::Text(<[_]>::to_vec("Yay!".as_bytes())),
None
)
)
}
}
}
let post_handler = move |mut request: HyperRequest, response: HyperResponse| {
assert_eq!(request.method, Method::Get);

This comment has been minimized.

@jdm

jdm Nov 9, 2016

Member

Let's verify that there's no body for consistency.

let data = read_response(&mut request);
assert_eq!(data, "");
response.send(b"Yay!").unwrap();
};
let (mut post_server, post_url) = make_server(post_handler);

let url = Url::parse("http://mozilla.com").unwrap();
let mut load_data = LoadData::new(LoadContext::Browsing, url.clone(), &HttpTest);
let post_redirect_url = post_url.clone();
let pre_handler = move |mut request: HyperRequest, mut response: HyperResponse| {
let data = read_response(&mut request);
assert_eq!(data, "Body on POST!");
response.headers_mut().set(Location(post_redirect_url.to_string()));
*response.status_mut() = StatusCode::MovedPermanently;
response.send(b"").unwrap();
};
let (mut pre_server, pre_url) = make_server(pre_handler);

load_data.data = Some(<[_]>::to_vec("Body on POST!".as_bytes()));
let request = Request::from_init(RequestInit {
url: pre_url.clone(),
body: Some(b"Body on POST!".to_vec()),
method: Method::Post,
destination: Destination::Document,
origin: pre_url.clone(),
pipeline_id: Some(TEST_PIPELINE_ID),
.. RequestInit::default()
});
let response = fetch_sync(request, None);

let http_state = HttpState::new();
let ui_provider = TestProvider::new();
let _ = pre_server.close();
let _ = post_server.close();

let _ = load(
&load_data, &ui_provider, &http_state,
None,
&Factory,
DEFAULT_USER_AGENT.into(),
&CancellationListener::new(None),
None);
assert!(response.to_actual().status.unwrap().is_success());
}

#[test]
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.