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 the data_loader test with fetch. #14042

Merged
merged 6 commits into from Nov 3, 2016

Rewrite the data_loader test with fetch.

  • Loading branch information
Ms2ger committed Nov 3, 2016
commit f304151fe2e0c2ca6538753aa4156459ffd37d99
@@ -2,65 +2,54 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use fetch_sync;
use hyper::header::ContentType;
use hyper::mime::{Attr, Mime, SubLevel, TopLevel, Value};
use hyper_serde::Serde;
use ipc_channel::ipc;
use msg::constellation_msg::{PipelineId, ReferrerPolicy};
use net_traits::{LoadContext, LoadData, LoadOrigin, NetworkError};
use net_traits::LoadConsumer::Channel;
use net_traits::ProgressMsg::{Done, Payload};
use net_traits::{FetchMetadata, FilteredMetadata, NetworkError};
use net_traits::request::{Origin, Request};
use net_traits::response::ResponseBody;
use std::ops::Deref;
use url::Url;

struct DataLoadTest;

impl LoadOrigin for DataLoadTest {
fn referrer_url(&self) -> Option<Url> {
None
}
fn referrer_policy(&self) -> Option<ReferrerPolicy> {
None
}
fn pipeline_id(&self) -> Option<PipelineId> {
None
}
}

#[cfg(test)]
fn assert_parse(url: &'static str,
content_type: Option<ContentType>,
charset: Option<&str>,
data: Option<&[u8]>) {
use net::data_loader::load;
use net::mime_classifier::MimeClassifier;
use net::resource_thread::CancellationListener;
use std::sync::Arc;

let (start_chan, start_port) = ipc::channel().unwrap();
let classifier = Arc::new(MimeClassifier::new());
load(LoadData::new(LoadContext::Browsing, Url::parse(url).unwrap(), &DataLoadTest),
Channel(start_chan),
classifier, CancellationListener::new(None));

let response = start_port.recv().unwrap();
assert_eq!(&response.metadata.content_type.map(Serde::into_inner),
&content_type);
assert_eq!(response.metadata.charset.as_ref().map(String::deref), charset);
let url = Url::parse(url).unwrap();
let origin = Origin::Origin(url.origin());
let request = Request::new(url, Some(origin), false, None);

let progress = response.progress_port.recv().unwrap();
let response = fetch_sync(request, None);

match data {
None => {
assert_eq!(progress, Done(Err(NetworkError::Internal("invalid data uri".to_owned()))));
}
Some(dat) => {
match progress {
Payload(d) => assert_eq!(d, dat),
Some(data) => {
assert!(!response.is_network_error());
assert_eq!(response.headers.len(), 1);

let header_content_type = response.headers.get::<ContentType>();
assert_eq!(header_content_type, content_type.as_ref());

let metadata = match response.metadata() {
Ok(FetchMetadata::Filtered { filtered: FilteredMetadata::Transparent(m), .. }) => m,
result => panic!(result),
};
assert_eq!(metadata.content_type.map(Serde::into_inner), content_type);
assert_eq!(metadata.charset.as_ref().map(String::deref), charset);

let resp_body = response.body.lock().unwrap();
match *resp_body {
ResponseBody::Done(ref val) => {
assert_eq!(val, &data);
},
_ => panic!(),
}
assert_eq!(response.progress_port.recv().unwrap(), Done(Ok(())));
}
},
None => {
assert!(response.is_network_error());
assert_eq!(response.metadata().err(), Some(NetworkError::Internal("Decoding data URL failed".to_owned())));
},
}
}

@@ -74,7 +63,7 @@ fn plain() {
assert_parse(
"data:,hello%20world",
Some(ContentType(Mime(TopLevel::Text, SubLevel::Plain,
vec!((Attr::Charset, Value::Ext("us-ascii".to_owned())))))),
vec!((Attr::Charset, Value::Ext("US-ASCII".to_owned())))))),
Some("US-ASCII"),
Some(b"hello world"));
}
@@ -116,7 +105,7 @@ fn base64() {
"data:;base64,C62+7w==",
Some(ContentType(Mime(TopLevel::Text,
SubLevel::Plain,
vec!((Attr::Charset, Value::Ext("us-ascii".to_owned())))))),
vec!((Attr::Charset, Value::Ext("US-ASCII".to_owned())))))),
Some("US-ASCII"),
Some(&[0x0B, 0xAD, 0xBE, 0xEF]));
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.