mirror of
https://github.com/servo/servo.git
synced 2025-07-22 23:03:42 +01:00
Move some helper functions to the root of the net tests crate.
This commit is contained in:
parent
c6e15f8ef2
commit
a8f655caf3
2 changed files with 49 additions and 41 deletions
|
@ -2,10 +2,10 @@
|
|||
* 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 {DEFAULT_USER_AGENT, FetchResponseCollector, new_fetch_context, fetch_async, fetch_sync};
|
||||
use devtools_traits::DevtoolsControlMsg;
|
||||
use devtools_traits::HttpRequest as DevtoolsHttpRequest;
|
||||
use devtools_traits::HttpResponse as DevtoolsHttpResponse;
|
||||
use filemanager_thread::{TestProvider, TEST_PROVIDER};
|
||||
use http_loader::{expect_devtools_http_request, expect_devtools_http_response};
|
||||
use hyper::LanguageTag;
|
||||
use hyper::header::{Accept, AccessControlAllowCredentials, AccessControlAllowHeaders, AccessControlAllowOrigin};
|
||||
|
@ -22,10 +22,7 @@ use hyper::status::StatusCode;
|
|||
use hyper::uri::RequestUri;
|
||||
use msg::constellation_msg::{ReferrerPolicy, TEST_PIPELINE_ID};
|
||||
use net::fetch::cors_cache::CORSCache;
|
||||
use net::fetch::methods::{FetchContext, fetch, fetch_with_cors_cache};
|
||||
use net::filemanager_thread::FileManager;
|
||||
use net::http_loader::HttpState;
|
||||
use net_traits::FetchTaskTarget;
|
||||
use net::fetch::methods::{fetch, fetch_with_cors_cache};
|
||||
use net_traits::request::{Origin, RedirectMode, Referrer, Request, RequestMode};
|
||||
use net_traits::response::{CacheState, Response, ResponseBody, ResponseType};
|
||||
use std::fs::File;
|
||||
|
@ -34,49 +31,13 @@ use std::rc::Rc;
|
|||
use std::sync::{Arc, Mutex};
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::mpsc::{Sender, channel};
|
||||
use std::thread;
|
||||
use time::{self, Duration};
|
||||
use unicase::UniCase;
|
||||
use url::{Origin as UrlOrigin, Url};
|
||||
use util::resource_files::resources_dir_path;
|
||||
|
||||
const DEFAULT_USER_AGENT: &'static str = "Such Browser. Very Layout. Wow.";
|
||||
|
||||
// TODO write a struct that impls Handler for storing test values
|
||||
|
||||
struct FetchResponseCollector {
|
||||
sender: Sender<Response>,
|
||||
}
|
||||
|
||||
fn new_fetch_context(dc: Option<Sender<DevtoolsControlMsg>>) -> FetchContext<TestProvider> {
|
||||
FetchContext {
|
||||
state: HttpState::new(),
|
||||
user_agent: DEFAULT_USER_AGENT.into(),
|
||||
devtools_chan: dc,
|
||||
filemanager: FileManager::new(TEST_PROVIDER),
|
||||
}
|
||||
}
|
||||
impl FetchTaskTarget for FetchResponseCollector {
|
||||
fn process_request_body(&mut self, _: &Request) {}
|
||||
fn process_request_eof(&mut self, _: &Request) {}
|
||||
fn process_response(&mut self, _: &Response) {}
|
||||
fn process_response_chunk(&mut self, _: Vec<u8>) {}
|
||||
/// Fired when the response is fully fetched
|
||||
fn process_response_eof(&mut self, response: &Response) {
|
||||
let _ = self.sender.send(response.clone());
|
||||
}
|
||||
}
|
||||
|
||||
fn fetch_async(request: Request, target: Box<FetchTaskTarget + Send>, dc: Option<Sender<DevtoolsControlMsg>>) {
|
||||
thread::spawn(move || {
|
||||
fetch(Rc::new(request), &mut Some(target), new_fetch_context(dc));
|
||||
});
|
||||
}
|
||||
|
||||
fn fetch_sync(request: Request, dc: Option<Sender<DevtoolsControlMsg>>) -> Response {
|
||||
fetch(Rc::new(request), &mut None, new_fetch_context(dc))
|
||||
}
|
||||
|
||||
fn make_server<H: Handler + 'static>(handler: H) -> (Listening, Url) {
|
||||
// this is a Listening server because of handle_threads()
|
||||
let server = Server::http("0.0.0.0:0").unwrap().handle_threads(handler, 1).unwrap();
|
||||
|
|
|
@ -32,3 +32,50 @@ extern crate util;
|
|||
#[cfg(test)] mod hsts;
|
||||
#[cfg(test)] mod http_loader;
|
||||
#[cfg(test)] mod filemanager_thread;
|
||||
|
||||
use devtools_traits::DevtoolsControlMsg;
|
||||
use filemanager_thread::{TestProvider, TEST_PROVIDER};
|
||||
use net::fetch::methods::{FetchContext, fetch};
|
||||
use net::filemanager_thread::FileManager;
|
||||
use net::http_loader::HttpState;
|
||||
use net_traits::FetchTaskTarget;
|
||||
use net_traits::request::Request;
|
||||
use net_traits::response::Response;
|
||||
use std::rc::Rc;
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::thread;
|
||||
|
||||
const DEFAULT_USER_AGENT: &'static str = "Such Browser. Very Layout. Wow.";
|
||||
|
||||
struct FetchResponseCollector {
|
||||
sender: Sender<Response>,
|
||||
}
|
||||
|
||||
fn new_fetch_context(dc: Option<Sender<DevtoolsControlMsg>>) -> FetchContext<TestProvider> {
|
||||
FetchContext {
|
||||
state: HttpState::new(),
|
||||
user_agent: DEFAULT_USER_AGENT.into(),
|
||||
devtools_chan: dc,
|
||||
filemanager: FileManager::new(TEST_PROVIDER),
|
||||
}
|
||||
}
|
||||
impl FetchTaskTarget for FetchResponseCollector {
|
||||
fn process_request_body(&mut self, _: &Request) {}
|
||||
fn process_request_eof(&mut self, _: &Request) {}
|
||||
fn process_response(&mut self, _: &Response) {}
|
||||
fn process_response_chunk(&mut self, _: Vec<u8>) {}
|
||||
/// Fired when the response is fully fetched
|
||||
fn process_response_eof(&mut self, response: &Response) {
|
||||
let _ = self.sender.send(response.clone());
|
||||
}
|
||||
}
|
||||
|
||||
fn fetch_async(request: Request, target: Box<FetchTaskTarget + Send>, dc: Option<Sender<DevtoolsControlMsg>>) {
|
||||
thread::spawn(move || {
|
||||
fetch(Rc::new(request), &mut Some(target), new_fetch_context(dc));
|
||||
});
|
||||
}
|
||||
|
||||
fn fetch_sync(request: Request, dc: Option<Sender<DevtoolsControlMsg>>) -> Response {
|
||||
fetch(Rc::new(request), &mut None, new_fetch_context(dc))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue