Auto merge of #9298 - nikkisquared:test_fetch, r=KiChjang

Implement a basic test for Fetch

As per @jdm's suggestion that I start minimally testing the Fetch protocol to catch any errors, I wrote a very simple test that just calls Fetch and checks that the response isn't a network error. I've made changes as necessary for every failure I encountered, although this doesn't mean the implementation is faultless yet.

As always, I look forward to any feedback for improvements regarding the test itself, the changes to the fetch files I've made, and anything that I missed and should update.

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/9298)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-01-14 08:20:05 +05:30
commit 9c713cb468
5 changed files with 89 additions and 30 deletions

44
tests/unit/net/fetch.rs Normal file
View file

@ -0,0 +1,44 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 hyper::server::{Listening, Server};
use hyper::server::{Request as HyperRequest, Response as HyperResponse};
use net::fetch::request::{Context, fetch, Referer, Request};
use net_traits::response::{Response};
use std::rc::Rc;
use url::Url;
fn make_server(message: &'static [u8]) -> (Listening, Url) {
let handler = move |_: HyperRequest, response: HyperResponse| {
response.send(message).unwrap();
};
// this is a Listening server because of handle_threads()
let server = Server::http("0.0.0.0:0").unwrap().handle_threads(handler, 1).unwrap();
let port = server.socket.port().to_string();
let mut url_string = "http://localhost:".to_owned();
url_string.push_str(&port);
let url = Url::parse(&url_string).unwrap();
(server, url)
}
#[test]
fn test_fetch_response_is_not_network_error() {
static MESSAGE: &'static [u8] = b"";
let (mut server, url) = make_server(MESSAGE);
let mut request = Request::new(url, Context::Fetch, false);
request.referer = Referer::NoReferer;
let wrapped_request = Rc::new(request);
let fetch_response = fetch(wrapped_request, false);
let _ = server.close();
if Response::is_network_error(&fetch_response) {
panic!("fetch response shouldn't be a network error");
}
}

View file

@ -19,6 +19,7 @@ extern crate util;
#[cfg(test)] mod cookie;
#[cfg(test)] mod data_loader;
#[cfg(test)] mod fetch;
#[cfg(test)] mod mime_classifier;
#[cfg(test)] mod resource_thread;
#[cfg(test)] mod hsts;