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_should_decode_the_response_as_deflate_when_response…

…_headers_have_content_encoding_deflate.
  • Loading branch information
Ms2ger committed Nov 9, 2016
commit 4fe105b8e1ff319ecc0d4f0759bd26b08c14e7a1
@@ -31,6 +31,7 @@ use net::test::{HttpResponse, LoadErrorType};
use net_traits::{CookieSource, IncludeSubdomains, LoadContext, LoadData};
use net_traits::{CustomResponse, LoadOrigin, Metadata, ReferrerPolicy};
use net_traits::request::{Request, RequestInit, Destination};
use net_traits::response::ResponseBody;
use std::borrow::Cow;
use std::io::{self, Cursor, Read, Write};
use std::sync::{Arc, Mutex, RwLock, mpsc};
@@ -656,37 +657,31 @@ fn test_load_when_redirecting_from_a_post_should_rewrite_next_request_as_get() {

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

impl HttpRequestFactory for Factory {
type R = MockRequest;

fn create(&self, _: Url, _: Method, _: Headers) -> Result<MockRequest, LoadError> {
let mut e = DeflateEncoder::new(Vec::new(), Compression::Default);
e.write(b"Yay!").unwrap();
let encoded_content = e.finish().unwrap();

let mut headers = Headers::new();
headers.set(ContentEncoding(vec![Encoding::Deflate]));
Ok(MockRequest::new(ResponseType::WithHeaders(encoded_content, headers)))
}
}

let url = Url::parse("http://mozilla.com").unwrap();
let load_data = LoadData::new(LoadContext::Browsing, url.clone(), &HttpTest);
let handler = move |_: HyperRequest, mut response: HyperResponse| {
response.headers_mut().set(ContentEncoding(vec![Encoding::Deflate]));
let mut e = DeflateEncoder::new(Vec::new(), Compression::Default);
e.write(b"Yay!").unwrap();
let encoded_content = e.finish().unwrap();
response.send(&encoded_content).unwrap();
};
let (mut server, url) = make_server(handler);

let http_state = HttpState::new();
let ui_provider = TestProvider::new();
let request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::Get,
body: None,
destination: Destination::Document,
origin: url.clone(),
pipeline_id: Some(TEST_PIPELINE_ID),
.. RequestInit::default()
});
let response = fetch_sync(request, None);

let mut response = load(
&load_data, &ui_provider, &http_state, None,
&Factory,
DEFAULT_USER_AGENT.into(),
&CancellationListener::new(None),
None)
.unwrap();
let _ = server.close();

assert_eq!(read_response(&mut response), "Yay!");
assert!(response.status.unwrap().is_success());
assert_eq!(*response.body.lock().unwrap(),
ResponseBody::Done(b"Yay!".to_vec()));
}

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