2018-09-24 19:51:37 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2018-08-14 16:50:53 -04:00
|
|
|
use errors::DenoResult;
|
2018-09-18 11:53:16 -07:00
|
|
|
use tokio_util;
|
2018-09-24 19:51:37 -04:00
|
|
|
|
|
|
|
use futures::Future;
|
|
|
|
use futures::Stream;
|
2018-08-28 13:49:19 -04:00
|
|
|
use hyper;
|
2018-09-18 11:53:16 -07:00
|
|
|
use hyper::client::Client;
|
2018-09-24 19:51:37 -04:00
|
|
|
use hyper::client::HttpConnector;
|
|
|
|
use hyper::Uri;
|
2018-08-28 13:49:19 -04:00
|
|
|
use hyper_rustls;
|
2018-09-24 19:51:37 -04:00
|
|
|
|
|
|
|
type Connector = hyper_rustls::HttpsConnector<HttpConnector>;
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref CONNECTOR: Connector = {
|
|
|
|
let num_dns_threads = 4;
|
|
|
|
Connector::new(num_dns_threads)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_client() -> Client<Connector, hyper::Body> {
|
|
|
|
// TODO use Hyper's connection pool.
|
|
|
|
let c = CONNECTOR.clone();
|
|
|
|
Client::builder().build(c)
|
|
|
|
}
|
2018-08-14 16:50:53 -04:00
|
|
|
|
|
|
|
// 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> {
|
|
|
|
let url = module_name.parse::<Uri>().unwrap();
|
2018-09-24 19:51:37 -04:00
|
|
|
let client = get_client();
|
2018-09-18 11:53:16 -07:00
|
|
|
let future = client
|
|
|
|
.get(url)
|
|
|
|
.and_then(|response| response.into_body().concat2());
|
|
|
|
let body = tokio_util::block_on(future)?;
|
2018-08-14 16:50:53 -04:00
|
|
|
Ok(String::from_utf8(body.to_vec()).unwrap())
|
|
|
|
}
|
|
|
|
|
2018-09-25 09:56:51 -04:00
|
|
|
/* TODO(ry) Re-enabled this test. Disabling to work around bug in #782.
|
|
|
|
|
2018-08-14 16:50:53 -04:00
|
|
|
#[test]
|
|
|
|
fn test_fetch_sync_string() {
|
|
|
|
// Relies on external http server. See tools/http_server.py
|
2018-09-18 11:53:16 -07:00
|
|
|
use futures;
|
|
|
|
|
|
|
|
tokio_util::init(|| {
|
|
|
|
tokio_util::block_on(futures::future::lazy(|| -> DenoResult<()> {
|
|
|
|
let p = fetch_sync_string("http://127.0.0.1:4545/package.json")?;
|
|
|
|
println!("package.json len {}", p.len());
|
|
|
|
assert!(p.len() > 1);
|
|
|
|
Ok(())
|
|
|
|
})).unwrap();
|
|
|
|
});
|
2018-08-14 16:50:53 -04:00
|
|
|
}
|
2018-09-25 09:56:51 -04:00
|
|
|
|
|
|
|
*/
|