2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::generic_error;
|
|
|
|
use deno_core::error::AnyError;
|
2020-09-16 14:28:07 -04:00
|
|
|
use deno_core::url::Url;
|
2020-12-13 13:45:53 -05:00
|
|
|
use deno_runtime::deno_fetch::reqwest;
|
|
|
|
use deno_runtime::deno_fetch::reqwest::header::HeaderMap;
|
|
|
|
use deno_runtime::deno_fetch::reqwest::header::HeaderValue;
|
|
|
|
use deno_runtime::deno_fetch::reqwest::header::IF_NONE_MATCH;
|
|
|
|
use deno_runtime::deno_fetch::reqwest::header::LOCATION;
|
|
|
|
use deno_runtime::deno_fetch::reqwest::header::USER_AGENT;
|
|
|
|
use deno_runtime::deno_fetch::reqwest::redirect::Policy;
|
|
|
|
use deno_runtime::deno_fetch::reqwest::Client;
|
|
|
|
use deno_runtime::deno_fetch::reqwest::StatusCode;
|
2020-02-19 08:17:13 -05:00
|
|
|
use std::collections::HashMap;
|
2019-04-25 13:29:21 -04:00
|
|
|
|
2020-01-15 13:23:29 -05:00
|
|
|
/// Create new instance of async reqwest::Client. This client supports
|
2019-04-25 13:29:21 -04:00
|
|
|
/// proxies and doesn't follow redirects.
|
2020-12-11 18:36:18 -05:00
|
|
|
pub fn create_http_client(
|
|
|
|
user_agent: String,
|
2021-01-04 18:15:52 -05:00
|
|
|
ca_data: Option<Vec<u8>>,
|
2020-12-11 18:36:18 -05:00
|
|
|
) -> Result<Client, AnyError> {
|
2020-01-15 13:23:29 -05:00
|
|
|
let mut headers = HeaderMap::new();
|
2020-12-11 18:36:18 -05:00
|
|
|
headers.insert(USER_AGENT, user_agent.parse().unwrap());
|
2020-02-17 11:59:51 -05:00
|
|
|
let mut builder = Client::builder()
|
2020-01-15 13:23:29 -05:00
|
|
|
.redirect(Policy::none())
|
|
|
|
.default_headers(headers)
|
2020-02-17 11:59:51 -05:00
|
|
|
.use_rustls_tls();
|
|
|
|
|
2021-01-04 10:55:20 -05:00
|
|
|
if let Some(ca_data) = ca_data {
|
2021-01-04 18:15:52 -05:00
|
|
|
let cert = reqwest::Certificate::from_pem(&ca_data)?;
|
2020-02-17 11:59:51 -05:00
|
|
|
builder = builder.add_root_certificate(cert);
|
|
|
|
}
|
2018-08-14 16:50:53 -04:00
|
|
|
|
2020-08-25 18:48:25 -04:00
|
|
|
builder
|
|
|
|
.build()
|
2020-09-14 12:48:57 -04:00
|
|
|
.map_err(|_| generic_error("Unable to build http client"))
|
2020-02-17 11:59:51 -05:00
|
|
|
}
|
2018-11-29 22:01:01 -05:00
|
|
|
/// Construct the next uri based on base uri and location header fragment
|
2018-11-30 03:30:49 -05:00
|
|
|
/// See <https://tools.ietf.org/html/rfc3986#section-4.2>
|
2019-04-25 13:29:21 -04:00
|
|
|
fn resolve_url_from_location(base_url: &Url, location: &str) -> Url {
|
2018-11-29 22:01:01 -05:00
|
|
|
if location.starts_with("http://") || location.starts_with("https://") {
|
|
|
|
// absolute uri
|
2019-04-25 13:29:21 -04:00
|
|
|
Url::parse(location).expect("provided redirect url should be a valid url")
|
2018-11-29 22:01:01 -05:00
|
|
|
} else if location.starts_with("//") {
|
|
|
|
// "//" authority path-abempty
|
2019-04-25 13:29:21 -04:00
|
|
|
Url::parse(&format!("{}:{}", base_url.scheme(), location))
|
2018-11-30 03:30:49 -05:00
|
|
|
.expect("provided redirect url should be a valid url")
|
|
|
|
} else if location.starts_with('/') {
|
2018-11-29 22:01:01 -05:00
|
|
|
// path-absolute
|
2019-04-25 13:29:21 -04:00
|
|
|
base_url
|
|
|
|
.join(location)
|
|
|
|
.expect("provided redirect url should be a valid url")
|
2018-11-29 22:01:01 -05:00
|
|
|
} else {
|
|
|
|
// assuming path-noscheme | path-empty
|
2019-04-25 13:29:21 -04:00
|
|
|
let base_url_path_str = base_url.path().to_owned();
|
|
|
|
// Pop last part or url (after last slash)
|
|
|
|
let segs: Vec<&str> = base_url_path_str.rsplitn(2, '/').collect();
|
|
|
|
let new_path = format!("{}/{}", segs.last().unwrap_or(&""), location);
|
|
|
|
base_url
|
|
|
|
.join(&new_path)
|
|
|
|
.expect("provided redirect url should be a valid url")
|
2018-11-29 22:01:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 07:34:16 -04:00
|
|
|
// TODO(ry) HTTP headers are not unique key, value pairs. There may be more than
|
|
|
|
// one header line with the same key. This should be changed to something like
|
|
|
|
// Vec<(String, String)>
|
|
|
|
pub type HeadersMap = HashMap<String, String>;
|
2020-01-26 13:59:41 -05:00
|
|
|
|
2019-04-25 13:29:21 -04:00
|
|
|
#[derive(Debug, PartialEq)]
|
2019-04-01 21:46:40 -04:00
|
|
|
pub enum FetchOnceResult {
|
2020-02-19 08:17:13 -05:00
|
|
|
Code(Vec<u8>, HeadersMap),
|
2020-01-11 05:11:05 -05:00
|
|
|
NotModified,
|
2020-02-19 08:17:13 -05:00
|
|
|
Redirect(Url, HeadersMap),
|
2019-04-01 21:46:40 -04:00
|
|
|
}
|
|
|
|
|
2020-01-02 07:48:46 -05:00
|
|
|
/// Asynchronously fetches the given HTTP URL one pass only.
|
2019-04-01 21:46:40 -04:00
|
|
|
/// If no redirect is present and no error occurs,
|
2020-01-26 13:59:41 -05:00
|
|
|
/// yields Code(ResultPayload).
|
2019-04-01 21:46:40 -04:00
|
|
|
/// If redirect occurs, does not follow and
|
|
|
|
/// yields Redirect(url).
|
2020-09-04 06:43:20 -04:00
|
|
|
pub async fn fetch_once(
|
2020-01-16 10:10:01 -05:00
|
|
|
client: Client,
|
2019-04-25 13:29:21 -04:00
|
|
|
url: &Url,
|
2020-01-11 05:11:05 -05:00
|
|
|
cached_etag: Option<String>,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<FetchOnceResult, AnyError> {
|
2019-04-25 13:29:21 -04:00
|
|
|
let url = url.clone();
|
|
|
|
|
2020-09-04 06:43:20 -04:00
|
|
|
let mut request = client.get(url.clone());
|
2020-01-11 05:11:05 -05:00
|
|
|
|
2020-09-04 06:43:20 -04:00
|
|
|
if let Some(etag) = cached_etag {
|
|
|
|
let if_none_match_val = HeaderValue::from_str(&etag).unwrap();
|
|
|
|
request = request.header(IF_NONE_MATCH, if_none_match_val);
|
|
|
|
}
|
|
|
|
let response = request.send().await?;
|
2020-01-11 05:11:05 -05:00
|
|
|
|
2020-09-04 06:43:20 -04:00
|
|
|
if response.status() == StatusCode::NOT_MODIFIED {
|
|
|
|
return Ok(FetchOnceResult::NotModified);
|
|
|
|
}
|
2019-12-30 08:57:17 -05:00
|
|
|
|
2020-09-16 07:34:16 -04:00
|
|
|
let mut headers_: HashMap<String, String> = HashMap::new();
|
2020-09-04 06:43:20 -04:00
|
|
|
let headers = response.headers();
|
2020-05-09 12:43:24 -04:00
|
|
|
|
2020-09-04 06:43:20 -04:00
|
|
|
if let Some(warning) = headers.get("X-Deno-Warning") {
|
|
|
|
eprintln!(
|
|
|
|
"{} {}",
|
|
|
|
crate::colors::yellow("Warning"),
|
|
|
|
warning.to_str().unwrap()
|
|
|
|
);
|
|
|
|
}
|
2020-02-19 08:17:13 -05:00
|
|
|
|
2020-09-04 06:43:20 -04:00
|
|
|
for key in headers.keys() {
|
|
|
|
let key_str = key.to_string();
|
|
|
|
let values = headers.get_all(key);
|
|
|
|
let values_str = values
|
|
|
|
.iter()
|
|
|
|
.map(|e| e.to_str().unwrap().to_string())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(",");
|
2020-09-16 07:34:16 -04:00
|
|
|
headers_.insert(key_str, values_str);
|
2020-09-04 06:43:20 -04:00
|
|
|
}
|
2019-12-30 08:57:17 -05:00
|
|
|
|
2020-09-04 06:43:20 -04:00
|
|
|
if response.status().is_redirection() {
|
|
|
|
if let Some(location) = response.headers().get(LOCATION) {
|
|
|
|
let location_string = location.to_str().unwrap();
|
2019-12-30 08:57:17 -05:00
|
|
|
debug!("Redirecting to {:?}...", &location_string);
|
|
|
|
let new_url = resolve_url_from_location(&url, location_string);
|
2020-02-19 08:17:13 -05:00
|
|
|
return Ok(FetchOnceResult::Redirect(new_url, headers_));
|
2020-09-04 06:43:20 -04:00
|
|
|
} else {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(generic_error(format!(
|
2020-09-04 06:43:20 -04:00
|
|
|
"Redirection from '{}' did not provide location header",
|
|
|
|
url
|
|
|
|
)));
|
2019-12-30 08:57:17 -05:00
|
|
|
}
|
2020-09-04 06:43:20 -04:00
|
|
|
}
|
2019-12-30 08:57:17 -05:00
|
|
|
|
2020-09-04 06:43:20 -04:00
|
|
|
if response.status().is_client_error() || response.status().is_server_error()
|
|
|
|
{
|
|
|
|
let err =
|
2020-09-14 12:48:57 -04:00
|
|
|
generic_error(format!("Import '{}' failed: {}", &url, response.status()));
|
2020-09-04 06:43:20 -04:00
|
|
|
return Err(err);
|
|
|
|
}
|
2020-01-11 05:11:05 -05:00
|
|
|
|
2020-09-04 06:43:20 -04:00
|
|
|
let body = response.bytes().await?.to_vec();
|
2019-12-30 08:57:17 -05:00
|
|
|
|
2020-09-04 06:43:20 -04:00
|
|
|
Ok(FetchOnceResult::Code(body, headers_))
|
2019-04-01 21:46:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2019-04-25 13:29:21 -04:00
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-01-07 21:08:51 -05:00
|
|
|
use crate::version;
|
2021-01-04 18:15:52 -05:00
|
|
|
use std::fs::read;
|
2018-08-14 16:50:53 -04:00
|
|
|
|
2021-01-04 18:15:52 -05:00
|
|
|
fn create_test_client(ca_data: Option<Vec<u8>>) -> Client {
|
2021-01-04 10:55:20 -05:00
|
|
|
create_http_client("test_client".to_string(), ca_data).unwrap()
|
2020-12-11 18:36:18 -05:00
|
|
|
}
|
|
|
|
|
2020-02-03 08:53:50 -05:00
|
|
|
#[tokio::test]
|
2020-02-25 14:42:00 -05:00
|
|
|
async fn test_fetch_string() {
|
2020-08-10 17:31:05 -04:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2020-07-04 13:05:01 -04:00
|
|
|
// Relies on external http server. See target/debug/test_server
|
2019-10-31 22:33:27 -04:00
|
|
|
let url =
|
|
|
|
Url::parse("http://127.0.0.1:4545/cli/tests/fixture.json").unwrap();
|
2020-12-11 18:36:18 -05:00
|
|
|
let client = create_test_client(None);
|
2020-02-04 11:27:50 -05:00
|
|
|
let result = fetch_once(client, &url, None).await;
|
2020-02-19 08:17:13 -05:00
|
|
|
if let Ok(FetchOnceResult::Code(body, headers)) = result {
|
|
|
|
assert!(!body.is_empty());
|
2020-09-16 07:34:16 -04:00
|
|
|
assert_eq!(headers.get("content-type").unwrap(), "application/json");
|
2020-02-19 08:17:13 -05:00
|
|
|
assert_eq!(headers.get("etag"), None);
|
|
|
|
assert_eq!(headers.get("x-typescript-types"), None);
|
2020-02-03 08:53:50 -05:00
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
2020-01-11 05:11:05 -05:00
|
|
|
}
|
|
|
|
|
2020-02-03 08:53:50 -05:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_gzip() {
|
2020-08-10 17:31:05 -04:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2020-07-04 13:05:01 -04:00
|
|
|
// Relies on external http server. See target/debug/test_server
|
2020-01-11 05:11:05 -05:00
|
|
|
let url = Url::parse(
|
|
|
|
"http://127.0.0.1:4545/cli/tests/053_import_compression/gziped",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-12-11 18:36:18 -05:00
|
|
|
let client = create_test_client(None);
|
2020-02-04 11:27:50 -05:00
|
|
|
let result = fetch_once(client, &url, None).await;
|
2020-02-19 08:17:13 -05:00
|
|
|
if let Ok(FetchOnceResult::Code(body, headers)) = result {
|
|
|
|
assert_eq!(String::from_utf8(body).unwrap(), "console.log('gzip')");
|
2020-02-04 11:27:50 -05:00
|
|
|
assert_eq!(
|
2020-09-16 07:34:16 -04:00
|
|
|
headers.get("content-type").unwrap(),
|
2020-02-19 08:17:13 -05:00
|
|
|
"application/javascript"
|
2020-02-04 11:27:50 -05:00
|
|
|
);
|
2020-02-19 08:17:13 -05:00
|
|
|
assert_eq!(headers.get("etag"), None);
|
|
|
|
assert_eq!(headers.get("x-typescript-types"), None);
|
2020-02-03 08:53:50 -05:00
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
2020-01-11 05:11:05 -05:00
|
|
|
}
|
|
|
|
|
2020-02-03 08:53:50 -05:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_with_etag() {
|
2020-08-10 17:31:05 -04:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2020-01-11 05:11:05 -05:00
|
|
|
let url = Url::parse("http://127.0.0.1:4545/etag_script.ts").unwrap();
|
2020-12-11 18:36:18 -05:00
|
|
|
let client = create_test_client(None);
|
2020-02-04 11:27:50 -05:00
|
|
|
let result = fetch_once(client.clone(), &url, None).await;
|
2020-02-19 08:17:13 -05:00
|
|
|
if let Ok(FetchOnceResult::Code(body, headers)) = result {
|
2020-02-03 08:53:50 -05:00
|
|
|
assert!(!body.is_empty());
|
2020-02-04 11:27:50 -05:00
|
|
|
assert_eq!(String::from_utf8(body).unwrap(), "console.log('etag')");
|
2020-02-19 08:17:13 -05:00
|
|
|
assert_eq!(
|
2020-09-16 07:34:16 -04:00
|
|
|
headers.get("content-type").unwrap(),
|
2020-02-19 08:17:13 -05:00
|
|
|
"application/typescript"
|
|
|
|
);
|
2020-09-16 07:34:16 -04:00
|
|
|
assert_eq!(headers.get("etag").unwrap(), "33a64df551425fcc55e");
|
2020-02-03 08:53:50 -05:00
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
2020-01-11 05:11:05 -05:00
|
|
|
|
2020-02-03 08:53:50 -05:00
|
|
|
let res =
|
2020-02-04 11:27:50 -05:00
|
|
|
fetch_once(client, &url, Some("33a64df551425fcc55e".to_string())).await;
|
2020-02-03 08:53:50 -05:00
|
|
|
assert_eq!(res.unwrap(), FetchOnceResult::NotModified);
|
2020-01-11 05:11:05 -05:00
|
|
|
}
|
|
|
|
|
2020-02-03 08:53:50 -05:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_brotli() {
|
2020-08-10 17:31:05 -04:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2020-07-04 13:05:01 -04:00
|
|
|
// Relies on external http server. See target/debug/test_server
|
2020-01-11 05:11:05 -05:00
|
|
|
let url = Url::parse(
|
|
|
|
"http://127.0.0.1:4545/cli/tests/053_import_compression/brotli",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-12-11 18:36:18 -05:00
|
|
|
let client = create_test_client(None);
|
2020-02-04 11:27:50 -05:00
|
|
|
let result = fetch_once(client, &url, None).await;
|
2020-02-19 08:17:13 -05:00
|
|
|
if let Ok(FetchOnceResult::Code(body, headers)) = result {
|
|
|
|
assert!(!body.is_empty());
|
|
|
|
assert_eq!(String::from_utf8(body).unwrap(), "console.log('brotli');");
|
2020-02-03 08:53:50 -05:00
|
|
|
assert_eq!(
|
2020-09-16 07:34:16 -04:00
|
|
|
headers.get("content-type").unwrap(),
|
2020-02-19 08:17:13 -05:00
|
|
|
"application/javascript"
|
2020-02-03 08:53:50 -05:00
|
|
|
);
|
2020-02-19 08:17:13 -05:00
|
|
|
assert_eq!(headers.get("etag"), None);
|
|
|
|
assert_eq!(headers.get("x-typescript-types"), None);
|
2020-02-03 08:53:50 -05:00
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
2019-04-25 13:29:21 -04:00
|
|
|
}
|
2019-03-19 14:06:44 -04:00
|
|
|
|
2020-02-03 08:53:50 -05:00
|
|
|
#[tokio::test]
|
2020-02-04 11:27:50 -05:00
|
|
|
async fn test_fetch_once_with_redirect() {
|
2020-08-10 17:31:05 -04:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2020-07-04 13:05:01 -04:00
|
|
|
// Relies on external http server. See target/debug/test_server
|
2019-10-31 22:33:27 -04:00
|
|
|
let url =
|
|
|
|
Url::parse("http://127.0.0.1:4546/cli/tests/fixture.json").unwrap();
|
2019-04-25 13:29:21 -04:00
|
|
|
// Dns resolver substitutes `127.0.0.1` with `localhost`
|
2019-10-31 22:33:27 -04:00
|
|
|
let target_url =
|
|
|
|
Url::parse("http://localhost:4545/cli/tests/fixture.json").unwrap();
|
2020-12-11 18:36:18 -05:00
|
|
|
let client = create_test_client(None);
|
2020-02-04 11:27:50 -05:00
|
|
|
let result = fetch_once(client, &url, None).await;
|
2020-02-19 08:17:13 -05:00
|
|
|
if let Ok(FetchOnceResult::Redirect(url, _)) = result {
|
2020-02-03 08:53:50 -05:00
|
|
|
assert_eq!(url, target_url);
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
2019-04-25 13:29:21 -04:00
|
|
|
}
|
2018-11-29 22:01:01 -05:00
|
|
|
|
2019-04-25 13:29:21 -04:00
|
|
|
#[test]
|
|
|
|
fn test_resolve_url_from_location_full_1() {
|
|
|
|
let url = "http://deno.land".parse::<Url>().unwrap();
|
|
|
|
let new_uri = resolve_url_from_location(&url, "http://golang.org");
|
|
|
|
assert_eq!(new_uri.host_str().unwrap(), "golang.org");
|
|
|
|
}
|
2018-11-29 22:01:01 -05:00
|
|
|
|
2019-04-25 13:29:21 -04:00
|
|
|
#[test]
|
|
|
|
fn test_resolve_url_from_location_full_2() {
|
|
|
|
let url = "https://deno.land".parse::<Url>().unwrap();
|
|
|
|
let new_uri = resolve_url_from_location(&url, "https://golang.org");
|
|
|
|
assert_eq!(new_uri.host_str().unwrap(), "golang.org");
|
|
|
|
}
|
2018-11-29 22:01:01 -05:00
|
|
|
|
2019-04-25 13:29:21 -04:00
|
|
|
#[test]
|
|
|
|
fn test_resolve_url_from_location_relative_1() {
|
|
|
|
let url = "http://deno.land/x".parse::<Url>().unwrap();
|
|
|
|
let new_uri = resolve_url_from_location(&url, "//rust-lang.org/en-US");
|
|
|
|
assert_eq!(new_uri.host_str().unwrap(), "rust-lang.org");
|
|
|
|
assert_eq!(new_uri.path(), "/en-US");
|
|
|
|
}
|
2018-11-29 22:01:01 -05:00
|
|
|
|
2019-04-25 13:29:21 -04:00
|
|
|
#[test]
|
|
|
|
fn test_resolve_url_from_location_relative_2() {
|
|
|
|
let url = "http://deno.land/x".parse::<Url>().unwrap();
|
|
|
|
let new_uri = resolve_url_from_location(&url, "/y");
|
|
|
|
assert_eq!(new_uri.host_str().unwrap(), "deno.land");
|
|
|
|
assert_eq!(new_uri.path(), "/y");
|
|
|
|
}
|
2018-11-29 22:01:01 -05:00
|
|
|
|
2019-04-25 13:29:21 -04:00
|
|
|
#[test]
|
|
|
|
fn test_resolve_url_from_location_relative_3() {
|
|
|
|
let url = "http://deno.land/x".parse::<Url>().unwrap();
|
|
|
|
let new_uri = resolve_url_from_location(&url, "z");
|
|
|
|
assert_eq!(new_uri.host_str().unwrap(), "deno.land");
|
|
|
|
assert_eq!(new_uri.path(), "/z");
|
|
|
|
}
|
2020-02-17 11:59:51 -05:00
|
|
|
|
|
|
|
#[tokio::test]
|
2020-02-25 14:42:00 -05:00
|
|
|
async fn test_fetch_with_cafile_string() {
|
2020-08-10 17:31:05 -04:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2020-07-04 13:05:01 -04:00
|
|
|
// Relies on external http server. See target/debug/test_server
|
2020-02-17 11:59:51 -05:00
|
|
|
let url =
|
|
|
|
Url::parse("https://localhost:5545/cli/tests/fixture.json").unwrap();
|
2021-01-04 18:15:52 -05:00
|
|
|
|
|
|
|
let client = create_http_client(
|
2021-01-07 21:08:51 -05:00
|
|
|
version::get_user_agent(),
|
2021-01-04 18:15:52 -05:00
|
|
|
Some(
|
|
|
|
read(
|
|
|
|
test_util::root_path()
|
|
|
|
.join("std/http/testdata/tls/RootCA.pem")
|
|
|
|
.to_str()
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
),
|
2020-12-11 18:36:18 -05:00
|
|
|
)
|
2020-02-17 11:59:51 -05:00
|
|
|
.unwrap();
|
|
|
|
let result = fetch_once(client, &url, None).await;
|
2020-02-19 08:17:13 -05:00
|
|
|
if let Ok(FetchOnceResult::Code(body, headers)) = result {
|
|
|
|
assert!(!body.is_empty());
|
2020-09-16 07:34:16 -04:00
|
|
|
assert_eq!(headers.get("content-type").unwrap(), "application/json");
|
2020-02-19 08:17:13 -05:00
|
|
|
assert_eq!(headers.get("etag"), None);
|
|
|
|
assert_eq!(headers.get("x-typescript-types"), None);
|
2020-02-17 11:59:51 -05:00
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_with_cafile_gzip() {
|
2020-08-10 17:31:05 -04:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2020-07-04 13:05:01 -04:00
|
|
|
// Relies on external http server. See target/debug/test_server
|
2020-02-17 11:59:51 -05:00
|
|
|
let url = Url::parse(
|
|
|
|
"https://localhost:5545/cli/tests/053_import_compression/gziped",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-01-04 18:15:52 -05:00
|
|
|
let client = create_http_client(
|
2021-01-07 21:08:51 -05:00
|
|
|
version::get_user_agent(),
|
2021-01-04 18:15:52 -05:00
|
|
|
Some(
|
|
|
|
read(
|
|
|
|
test_util::root_path()
|
|
|
|
.join("std/http/testdata/tls/RootCA.pem")
|
|
|
|
.to_str()
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
),
|
2020-12-11 18:36:18 -05:00
|
|
|
)
|
2020-02-17 11:59:51 -05:00
|
|
|
.unwrap();
|
|
|
|
let result = fetch_once(client, &url, None).await;
|
2020-02-19 08:17:13 -05:00
|
|
|
if let Ok(FetchOnceResult::Code(body, headers)) = result {
|
|
|
|
assert_eq!(String::from_utf8(body).unwrap(), "console.log('gzip')");
|
2020-02-17 11:59:51 -05:00
|
|
|
assert_eq!(
|
2020-09-16 07:34:16 -04:00
|
|
|
headers.get("content-type").unwrap(),
|
2020-02-19 08:17:13 -05:00
|
|
|
"application/javascript"
|
2020-02-17 11:59:51 -05:00
|
|
|
);
|
2020-02-19 08:17:13 -05:00
|
|
|
assert_eq!(headers.get("etag"), None);
|
|
|
|
assert_eq!(headers.get("x-typescript-types"), None);
|
2020-02-17 11:59:51 -05:00
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_with_cafile_with_etag() {
|
2020-08-10 17:31:05 -04:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2020-02-17 11:59:51 -05:00
|
|
|
let url = Url::parse("https://localhost:5545/etag_script.ts").unwrap();
|
2021-01-04 18:15:52 -05:00
|
|
|
let client = create_http_client(
|
2021-01-07 21:08:51 -05:00
|
|
|
version::get_user_agent(),
|
2021-01-04 18:15:52 -05:00
|
|
|
Some(
|
|
|
|
read(
|
|
|
|
test_util::root_path()
|
|
|
|
.join("std/http/testdata/tls/RootCA.pem")
|
|
|
|
.to_str()
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
),
|
2020-12-11 18:36:18 -05:00
|
|
|
)
|
2020-02-17 11:59:51 -05:00
|
|
|
.unwrap();
|
|
|
|
let result = fetch_once(client.clone(), &url, None).await;
|
2020-02-19 08:17:13 -05:00
|
|
|
if let Ok(FetchOnceResult::Code(body, headers)) = result {
|
2020-02-17 11:59:51 -05:00
|
|
|
assert!(!body.is_empty());
|
|
|
|
assert_eq!(String::from_utf8(body).unwrap(), "console.log('etag')");
|
2020-02-19 08:17:13 -05:00
|
|
|
assert_eq!(
|
2020-09-16 07:34:16 -04:00
|
|
|
headers.get("content-type").unwrap(),
|
2020-02-19 08:17:13 -05:00
|
|
|
"application/typescript"
|
|
|
|
);
|
2020-09-16 07:34:16 -04:00
|
|
|
assert_eq!(headers.get("etag").unwrap(), "33a64df551425fcc55e");
|
2020-02-19 08:17:13 -05:00
|
|
|
assert_eq!(headers.get("x-typescript-types"), None);
|
2020-02-17 11:59:51 -05:00
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
|
|
|
|
let res =
|
|
|
|
fetch_once(client, &url, Some("33a64df551425fcc55e".to_string())).await;
|
|
|
|
assert_eq!(res.unwrap(), FetchOnceResult::NotModified);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_with_cafile_brotli() {
|
2020-08-10 17:31:05 -04:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2020-07-04 13:05:01 -04:00
|
|
|
// Relies on external http server. See target/debug/test_server
|
2020-02-17 11:59:51 -05:00
|
|
|
let url = Url::parse(
|
|
|
|
"https://localhost:5545/cli/tests/053_import_compression/brotli",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-01-04 18:15:52 -05:00
|
|
|
let client = create_http_client(
|
2021-01-07 21:08:51 -05:00
|
|
|
version::get_user_agent(),
|
2021-01-04 18:15:52 -05:00
|
|
|
Some(
|
|
|
|
read(
|
|
|
|
test_util::root_path()
|
|
|
|
.join("std/http/testdata/tls/RootCA.pem")
|
|
|
|
.to_str()
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
),
|
2020-12-11 18:36:18 -05:00
|
|
|
)
|
2020-02-17 11:59:51 -05:00
|
|
|
.unwrap();
|
|
|
|
let result = fetch_once(client, &url, None).await;
|
2020-02-19 08:17:13 -05:00
|
|
|
if let Ok(FetchOnceResult::Code(body, headers)) = result {
|
|
|
|
assert!(!body.is_empty());
|
|
|
|
assert_eq!(String::from_utf8(body).unwrap(), "console.log('brotli');");
|
2020-02-17 11:59:51 -05:00
|
|
|
assert_eq!(
|
2020-09-16 07:34:16 -04:00
|
|
|
headers.get("content-type").unwrap(),
|
2020-02-19 08:17:13 -05:00
|
|
|
"application/javascript"
|
2020-02-17 11:59:51 -05:00
|
|
|
);
|
2020-02-19 08:17:13 -05:00
|
|
|
assert_eq!(headers.get("etag"), None);
|
|
|
|
assert_eq!(headers.get("x-typescript-types"), None);
|
2020-02-17 11:59:51 -05:00
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}
|
2020-09-04 06:43:20 -04:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn bad_redirect() {
|
|
|
|
let _g = test_util::http_server();
|
|
|
|
let url_str = "http://127.0.0.1:4545/bad_redirect";
|
|
|
|
let url = Url::parse(url_str).unwrap();
|
2020-12-11 18:36:18 -05:00
|
|
|
let client = create_test_client(None);
|
2020-09-04 06:43:20 -04:00
|
|
|
let result = fetch_once(client, &url, None).await;
|
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.unwrap_err();
|
|
|
|
// Check that the error message contains the original URL
|
|
|
|
assert!(err.to_string().contains(url_str));
|
|
|
|
}
|
2018-11-29 22:01:01 -05:00
|
|
|
}
|