Skip to content

Commit

Permalink
send requests that are redirected to devtools
Browse files Browse the repository at this point in the history
add unit test
  • Loading branch information
mrmiywj committed Jul 27, 2016
1 parent d899149 commit a2b6c3a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 17 deletions.
36 changes: 19 additions & 17 deletions components/net/http_loader.rs
Expand Up @@ -622,18 +622,16 @@ fn send_request_to_devtools(msg: ChromeToDevtoolsControlMsg,
devtools_chan.send(DevtoolsControlMsg::FromChrome(msg)).unwrap();
}

fn send_response_to_devtools(devtools_chan: Option<Sender<DevtoolsControlMsg>>,
fn send_response_to_devtools(devtools_chan: &Sender<DevtoolsControlMsg>,
request_id: String,
headers: Option<Headers>,
status: Option<RawStatus>,
pipeline_id: PipelineId) {
if let Some(ref chan) = devtools_chan {
let response = DevtoolsHttpResponse { headers: headers, status: status, body: None, pipeline_id: pipeline_id };
let net_event_response = NetworkEvent::HttpResponse(response);
let response = DevtoolsHttpResponse { headers: headers, status: status, body: None, pipeline_id: pipeline_id };
let net_event_response = NetworkEvent::HttpResponse(response);

let msg = ChromeToDevtoolsControlMsg::NetworkEvent(request_id, net_event_response);
chan.send(DevtoolsControlMsg::FromChrome(msg)).unwrap();
}
let msg = ChromeToDevtoolsControlMsg::NetworkEvent(request_id, net_event_response);
let _ = devtools_chan.send(DevtoolsControlMsg::FromChrome(msg));
}

fn request_must_be_secured(url: &Url, hsts_list: &Arc<RwLock<HstsList>>) -> bool {
Expand Down Expand Up @@ -1056,10 +1054,13 @@ pub fn load<A, B>(load_data: &LoadData,
doc_url = new_doc_url;

redirected_to.insert(doc_url.clone());
continue;
}
}

// Only notify the devtools about the final request that received a response.
if let Some(m) = msg {
send_request_to_devtools(m, devtools_chan.as_ref().unwrap());
}
let mut adjusted_headers = response.headers().clone();

if viewing_source {
Expand All @@ -1078,23 +1079,24 @@ pub fn load<A, B>(load_data: &LoadData,
} else {
HttpsState::None
};
metadata.referrer = referrer_url;

// Only notify the devtools about the final request that received a response.
if let Some(msg) = msg {
send_request_to_devtools(msg, devtools_chan.as_ref().unwrap());
}
metadata.referrer = referrer_url.clone();

// --- Tell devtools that we got a response
// Send an HttpResponse message to devtools with the corresponding request_id
// TODO: Send this message even when the load fails?
if let Some(pipeline_id) = load_data.pipeline_id {
if let Some(ref chan) = devtools_chan {
send_response_to_devtools(
devtools_chan, request_id,
chan, request_id,
metadata.headers.clone(), metadata.status.clone(),
pipeline_id);
}
return StreamedResponse::from_http_response(box response, metadata)
}
}
if response.status().class() == StatusClass::Redirection {
continue;
} else {
return StreamedResponse::from_http_response(box response, metadata);
}
}
}

Expand Down
46 changes: 46 additions & 0 deletions tests/unit/net/http_loader.rs
Expand Up @@ -586,6 +586,52 @@ fn test_request_and_response_message_from_devtool_without_pipeline_id() {
assert!(devtools_port.try_recv().is_err());
}

#[test]
fn test_redirected_request_to_devtools() {
struct Factory;

impl HttpRequestFactory for Factory {
type R = MockRequest;

fn create(&self, url: Url, method: Method, _: Headers) -> Result<MockRequest, LoadError> {
if url.domain().unwrap() == "mozilla.com" {
assert_eq!(Method::Post, method);
Ok(MockRequest::new(ResponseType::Redirect("http://mozilla.org".to_owned())))
} else {
assert_eq!(Method::Get, method);
Ok(MockRequest::new(ResponseType::Text(<[_]>::to_vec("Yay!".as_bytes()))))
}
}
}

let url = Url::parse("http://mozilla.com").unwrap();
let mut load_data = LoadData::new(LoadContext::Browsing, url.clone(), &HttpTest);

load_data.method = Method::Post;

let http_state = HttpState::new();
let ui_provider = TestProvider::new();
let (devtools_chan, devtools_port) = mpsc::channel::<DevtoolsControlMsg>();

let _ = load(&load_data, &ui_provider, &http_state, Some(devtools_chan), &Factory,
DEFAULT_USER_AGENT.to_owned(), &CancellationListener::new(None), None);

let devhttprequest = expect_devtools_http_request(&devtools_port);
let devhttpresponse = expect_devtools_http_response(&devtools_port);

assert!(devhttprequest.method == Method::Post);
assert!(devhttprequest.url == url);
assert!(devhttpresponse.status == Some(RawStatus(301, Cow::Borrowed("Moved Permanently"))));

let devhttprequest = expect_devtools_http_request(&devtools_port);
let devhttpresponse = expect_devtools_http_response(&devtools_port);
let url = Url::parse("http://mozilla.org").unwrap();

assert!(devhttprequest.method == Method::Get);
assert!(devhttprequest.url == url);
assert!(devhttpresponse.status == Some(RawStatus(200, Cow::Borrowed("Ok"))));
}



#[test]
Expand Down

0 comments on commit a2b6c3a

Please sign in to comment.