diff options
Diffstat (limited to 'src/http_util.rs')
-rw-r--r-- | src/http_util.rs | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/src/http_util.rs b/src/http_util.rs index 1c6079c08..9c5ff9529 100644 --- a/src/http_util.rs +++ b/src/http_util.rs @@ -4,9 +4,10 @@ use errors::{DenoError, DenoResult}; use tokio_util; use futures::future::{loop_fn, Loop}; -use futures::{Future, Stream}; +use futures::{future, Future, Stream}; use hyper; use hyper::client::{Client, HttpConnector}; +use hyper::header::CONTENT_TYPE; use hyper::Uri; use hyper_rustls; @@ -27,7 +28,7 @@ pub fn get_client() -> Client<Connector, hyper::Body> { // The CodeFetch message is used to load HTTP javascript resources and expects a // synchronous response, this utility method supports that. -pub fn fetch_sync_string(module_name: &str) -> DenoResult<String> { +pub fn fetch_sync_string(module_name: &str) -> DenoResult<(String, String)> { let url = module_name.parse::<Uri>().unwrap(); let client = get_client(); // TODO(kevinkassimo): consider set a max redirection counter @@ -63,11 +64,18 @@ pub fn fetch_sync_string(module_name: &str) -> DenoResult<String> { Ok(Loop::Break(response)) }) }).and_then(|response| { - response + let content_type = response + .headers() + .get(CONTENT_TYPE) + .map(|content_type| content_type.to_str().unwrap().to_string()); + let body = response .into_body() .concat2() .map(|body| String::from_utf8(body.to_vec()).unwrap()) - .map_err(|err| DenoError::from(err)) + .map_err(|err| DenoError::from(err)); + body.join(future::ok(content_type)) + }).and_then(|(body_string, maybe_content_type)| { + future::ok((body_string, maybe_content_type.unwrap())) }); tokio_util::block_on(fetch_future) @@ -77,9 +85,11 @@ pub fn fetch_sync_string(module_name: &str) -> DenoResult<String> { fn test_fetch_sync_string() { // Relies on external http server. See tools/http_server.py tokio_util::init(|| { - let p = fetch_sync_string("http://127.0.0.1:4545/package.json").unwrap(); + let (p, m) = + fetch_sync_string("http://127.0.0.1:4545/package.json").unwrap(); println!("package.json len {}", p.len()); assert!(p.len() > 1); + assert!(m == "application/json") }); } @@ -87,8 +97,10 @@ fn test_fetch_sync_string() { fn test_fetch_sync_string_with_redirect() { // Relies on external http server. See tools/http_server.py tokio_util::init(|| { - let p = fetch_sync_string("http://127.0.0.1:4546/package.json").unwrap(); + let (p, m) = + fetch_sync_string("http://127.0.0.1:4546/package.json").unwrap(); println!("package.json len {}", p.len()); assert!(p.len() > 1); + assert!(m == "application/json") }); } |