From 945a2c66e1090e592d1a344bad941c9fed433375 Mon Sep 17 00:00:00 2001 From: Ravi Shankar Date: Sat, 20 Feb 2016 01:11:41 +0530 Subject: [PATCH] Updated the unit tests to include NetworkError --- components/net/http_loader.rs | 10 +++++----- tests/unit/net/data_loader.rs | 4 ++-- tests/unit/net/http_loader.rs | 23 ++++++++++++----------- tests/unit/net/resource_thread.rs | 8 +++----- 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/components/net/http_loader.rs b/components/net/http_loader.rs index 97a1eea3968..b9caf61446a 100644 --- a/components/net/http_loader.rs +++ b/components/net/http_loader.rs @@ -309,13 +309,13 @@ impl HttpRequest for WrappedHttpRequest { #[derive(Debug)] pub struct LoadError { - url: Url, - error: LoadErrorType, - reason: String, + pub url: Url, + pub error: LoadErrorType, + pub reason: String, } impl LoadError { - fn new(url: Url, error: LoadErrorType, reason: String) -> LoadError { + pub fn new(url: Url, error: LoadErrorType, reason: String) -> LoadError { LoadError { url: url, error: error, @@ -324,7 +324,7 @@ impl LoadError { } } -#[derive(Debug)] +#[derive(Eq, PartialEq, Debug)] pub enum LoadErrorType { Cancelled, Connection, diff --git a/tests/unit/net/data_loader.rs b/tests/unit/net/data_loader.rs index 50ee8771490..2f887f5a2bc 100644 --- a/tests/unit/net/data_loader.rs +++ b/tests/unit/net/data_loader.rs @@ -7,7 +7,7 @@ extern crate hyper; use ipc_channel::ipc; use net_traits::LoadConsumer::Channel; use net_traits::ProgressMsg::{Payload, Done}; -use net_traits::{LoadData, LoadContext}; +use net_traits::{LoadData, LoadContext, NetworkError}; use self::hyper::header::ContentType; use self::hyper::mime::{Mime, TopLevel, SubLevel, Attr, Value}; @@ -36,7 +36,7 @@ fn assert_parse(url: &'static str, match data { None => { - assert_eq!(progress, Done(Err("invalid data uri".to_owned()))); + assert_eq!(progress, Done(Err(NetworkError::Internal("invalid data uri".to_owned())))); } Some(dat) => { assert_eq!(progress, Payload(dat)); diff --git a/tests/unit/net/http_loader.rs b/tests/unit/net/http_loader.rs index 3b9da166a45..a9932ded163 100644 --- a/tests/unit/net/http_loader.rs +++ b/tests/unit/net/http_loader.rs @@ -20,9 +20,10 @@ use msg::constellation_msg::PipelineId; use net::cookie::Cookie; use net::cookie_storage::CookieStorage; use net::hsts::HstsEntry; +use net::http_loader::LoadErrorType; use net::http_loader::{load, LoadError, HttpRequestFactory, HttpRequest, HttpResponse, UIProvider, HttpState}; use net::resource_thread::{AuthCacheEntry, CancellationListener}; -use net_traits::{LoadData, CookieSource, LoadContext, IncludeSubdomains}; +use net_traits::{LoadData, CookieSource, LoadContext, NetworkError, IncludeSubdomains}; use std::borrow::Cow; use std::io::{self, Write, Read, Cursor}; use std::sync::mpsc::Receiver; @@ -1075,9 +1076,8 @@ fn test_load_errors_when_there_a_redirect_loop() { match load(&load_data, &ui_provider, &http_state, None, &Factory, DEFAULT_USER_AGENT.to_owned(), &CancellationListener::new(None)) { - Err(LoadError::InvalidRedirect(_, msg)) => { - assert_eq!(msg, "redirect loop"); - }, + Err(ref load_err) if load_err.error == LoadErrorType::InvalidRedirect => + assert_eq!(&load_err.reason, "redirect loop"), _ => panic!("expected max redirects to fail") } } @@ -1110,10 +1110,11 @@ fn test_load_errors_when_there_is_too_many_redirects() { match load(&load_data, &ui_provider, &http_state, None, &Factory, DEFAULT_USER_AGENT.to_owned(), &CancellationListener::new(None)) { - Err(LoadError::MaxRedirects(url, num_redirects)) => { + Err(LoadError { error: LoadErrorType::MaxRedirects(num_redirects), + url, .. }) => { assert_eq!(num_redirects, redirect_limit as u32); - assert_eq!(url.domain().unwrap(), "mozilla.com") - }, + assert_eq!(url.domain().unwrap(), "mozilla.com"); + } _ => panic!("expected max redirects to fail") } @@ -1166,7 +1167,7 @@ impl HttpRequestFactory for DontConnectFactory { type R = MockRequest; fn create(&self, url: Url, _: Method, _: Headers) -> Result { - Err(LoadError::Connection(url, "should not have connected".to_owned())) + Err(LoadError::new(url, LoadErrorType::Connection, "should not have connected".to_owned())) } } @@ -1184,7 +1185,7 @@ fn test_load_errors_when_scheme_is_not_http_or_https() { &DontConnectFactory, DEFAULT_USER_AGENT.to_owned(), &CancellationListener::new(None)) { - Err(LoadError::UnsupportedScheme(_)) => {} + Err(ref load_err) if load_err.error == LoadErrorType::UnsupportedScheme => (), _ => panic!("expected ftp scheme to be unsupported") } } @@ -1203,7 +1204,7 @@ fn test_load_errors_when_viewing_source_and_inner_url_scheme_is_not_http_or_http &DontConnectFactory, DEFAULT_USER_AGENT.to_owned(), &CancellationListener::new(None)) { - Err(LoadError::UnsupportedScheme(_)) => {} + Err(ref load_err) if load_err.error == LoadErrorType::UnsupportedScheme => (), _ => panic!("expected ftp scheme to be unsupported") } } @@ -1245,7 +1246,7 @@ fn test_load_errors_when_cancelled() { &Factory, DEFAULT_USER_AGENT.to_owned(), &cancel_listener) { - Err(LoadError::Cancelled(_, _)) => (), + Err(ref load_err) if load_err.error == LoadErrorType::Cancelled => (), _ => panic!("expected load cancelled error!") } } diff --git a/tests/unit/net/resource_thread.rs b/tests/unit/net/resource_thread.rs index fdaa0cd6bdb..ecba392d4fb 100644 --- a/tests/unit/net/resource_thread.rs +++ b/tests/unit/net/resource_thread.rs @@ -5,7 +5,7 @@ use ipc_channel::ipc; use net::resource_thread::new_resource_thread; use net_traits::hosts::{parse_hostsfile, host_replacement}; -use net_traits::{ControlMsg, LoadData, LoadConsumer, ProgressMsg, LoadContext}; +use net_traits::{ControlMsg, LoadData, LoadConsumer, LoadContext, NetworkError, ProgressMsg}; use std::borrow::ToOwned; use std::collections::HashMap; use std::sync::mpsc::channel; @@ -213,9 +213,7 @@ fn test_cancelled_listener() { // (but, the loading has been cancelled) let _ = body_sender.send(body); let response = receiver.recv().unwrap(); - match response.progress_port.recv().unwrap() { - ProgressMsg::Done(result) => assert_eq!(result.unwrap_err(), "load cancelled".to_owned()), - _ => panic!("baaaah!"), - } + assert_eq!(response.progress_port.recv().unwrap(), + ProgressMsg::Done(Err(NetworkError::Internal("load cancelled".to_owned())))); resource_thread.send(ControlMsg::Exit).unwrap(); }