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 and remove most unit tests for the legacy fetch stack. #14315

Merged
merged 14 commits into from Nov 24, 2016

Remove test for CustomResponseMediator.

The fetch stack does not yet support this at all (#14306).
  • Loading branch information
Ms2ger committed Nov 23, 2016
commit 3da075ca736df30e2fa2bcfec84ff845364e8212
@@ -31,7 +31,7 @@ use net::resource_thread::{AuthCacheEntry, CancellationListener};
use net::test::{HttpRequest, HttpRequestFactory, HttpState, LoadError, UIProvider, load};
use net::test::{HttpResponse, LoadErrorType};
use net_traits::{CookieSource, IncludeSubdomains, LoadContext, LoadData};
use net_traits::{CustomResponse, LoadOrigin, Metadata, NetworkError, ReferrerPolicy};
use net_traits::{LoadOrigin, NetworkError, ReferrerPolicy};
use net_traits::hosts::replace_host_table;
use net_traits::request::{Request, RequestInit, CredentialsMode, Destination};
use net_traits::response::ResponseBody;
@@ -44,7 +44,6 @@ use std::rc::Rc;
use std::sync::{Arc, Mutex, RwLock, mpsc};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::Receiver;
use std::thread;

const DEFAULT_USER_AGENT: &'static str = "Test-agent";

@@ -143,20 +142,10 @@ fn redirect_with_headers(host: String, mut headers: Headers) -> MockResponse {
)
}

fn respond_404() -> MockResponse {
MockResponse::new(
Headers::new(),
StatusCode::NotFound,
RawStatus(404, Cow::Borrowed("Not Found")),
b"".to_vec()
)
}

enum ResponseType {
RedirectWithHeaders(String, Headers),
Text(Vec<u8>),
WithHeaders(Vec<u8>, Headers),
Dummy404
}

struct MockRequest {
@@ -180,9 +169,6 @@ fn response_for_request_type(t: ResponseType) -> Result<MockResponse, LoadError>
ResponseType::WithHeaders(b, h) => {
Ok(respond_with_headers(b, h))
},
ResponseType::Dummy404 => {
Ok(respond_404())
}
}
}

@@ -1503,55 +1489,6 @@ fn test_auth_ui_needs_www_auth() {
assert_eq!(response.status.unwrap(), StatusCode::Unauthorized);
}

fn load_request_for_custom_response(expected_body: Vec<u8>) -> (Metadata, String) {
use ipc_channel::ipc;
let (sender, receiver) = ipc::channel().unwrap();

struct Factory;
impl HttpRequestFactory for Factory {
type R = MockRequest;
fn create(&self, _: ServoUrl, _: Method, _: Headers) -> Result<MockRequest, LoadError> {
Ok(MockRequest::new(ResponseType::Dummy404))
}
}

let mock_response = CustomResponse::new(
Headers::new(),
RawStatus(200, Cow::Borrowed("OK")),
expected_body
);
let url = ServoUrl::parse("http://mozilla.com").unwrap();
let http_state = HttpState::new();
let ui_provider = TestProvider::new();
let load_data = LoadData::new(LoadContext::Browsing, url.clone(), &HttpTest);

let join_handle = thread::spawn(move || {
let response = load(&load_data.clone(), &ui_provider, &http_state,
None, &Factory, DEFAULT_USER_AGENT.into(), &CancellationListener::new(None), Some(sender));
match response {
Ok(mut response) => {
let metadata = response.metadata.clone();
let body = read_response(&mut response);
(metadata, body)
}
Err(e) => panic!("Error Getting Response: {:?}", e)
}
});

let mediator = receiver.recv().unwrap();
mediator.response_chan.send(Some(mock_response)).unwrap();
let (metadata, body) = join_handle.join().unwrap();
(metadata, body)
}

#[test]
fn test_custom_response() {
let expected_body = b"Yay!".to_vec();
let (metadata, body) = load_request_for_custom_response(expected_body.clone());
assert_eq!(metadata.status, Some((200, b"OK".to_vec())));
assert_eq!(body, String::from_utf8(expected_body).unwrap());
}

#[test]
fn test_content_blocked() {
struct Factory;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.