2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-02-15 21:50:27 -05:00
|
|
|
use crate::auth_tokens::AuthToken;
|
2022-12-12 20:52:10 -05:00
|
|
|
use crate::util::progress_bar::UpdateGuard;
|
2022-11-18 17:28:14 -05:00
|
|
|
use crate::version::get_user_agent;
|
2021-02-15 21:50:27 -05:00
|
|
|
|
2021-12-09 06:16:17 -05:00
|
|
|
use cache_control::Cachability;
|
|
|
|
use cache_control::CacheControl;
|
|
|
|
use chrono::DateTime;
|
2022-12-12 20:52:10 -05:00
|
|
|
use deno_core::anyhow::bail;
|
2021-11-08 20:26:39 -05:00
|
|
|
use deno_core::error::custom_error;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::generic_error;
|
|
|
|
use deno_core::error::AnyError;
|
2022-12-12 20:52:10 -05:00
|
|
|
use deno_core::futures::StreamExt;
|
2020-09-16 14:28:07 -04:00
|
|
|
use deno_core::url::Url;
|
2022-11-18 17:28:14 -05:00
|
|
|
use deno_runtime::deno_fetch::create_http_client;
|
|
|
|
use deno_runtime::deno_fetch::reqwest;
|
2020-12-13 13:45:53 -05:00
|
|
|
use deno_runtime::deno_fetch::reqwest::header::HeaderValue;
|
2021-12-20 21:40:22 -05:00
|
|
|
use deno_runtime::deno_fetch::reqwest::header::ACCEPT;
|
2021-02-15 21:50:27 -05:00
|
|
|
use deno_runtime::deno_fetch::reqwest::header::AUTHORIZATION;
|
2020-12-13 13:45:53 -05:00
|
|
|
use deno_runtime::deno_fetch::reqwest::header::IF_NONE_MATCH;
|
|
|
|
use deno_runtime::deno_fetch::reqwest::header::LOCATION;
|
|
|
|
use deno_runtime::deno_fetch::reqwest::StatusCode;
|
2022-11-18 17:28:14 -05:00
|
|
|
use deno_runtime::deno_tls::rustls::RootCertStore;
|
2021-03-26 12:34:25 -04:00
|
|
|
use log::debug;
|
2020-02-19 08:17:13 -05:00
|
|
|
use std::collections::HashMap;
|
2021-12-09 06:16:17 -05:00
|
|
|
use std::time::Duration;
|
|
|
|
use std::time::SystemTime;
|
2019-04-25 13:29:21 -04: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
|
|
|
|
2021-12-09 06:16:17 -05:00
|
|
|
/// A structure used to determine if a entity in the http cache can be used.
|
|
|
|
///
|
|
|
|
/// This is heavily influenced by
|
2022-07-30 06:20:29 -04:00
|
|
|
/// <https://github.com/kornelski/rusty-http-cache-semantics> which is BSD
|
2021-12-09 06:16:17 -05:00
|
|
|
/// 2-Clause Licensed and copyright Kornel Lesiński
|
2022-03-23 09:54:22 -04:00
|
|
|
pub struct CacheSemantics {
|
2021-12-09 06:16:17 -05:00
|
|
|
cache_control: CacheControl,
|
|
|
|
cached: SystemTime,
|
|
|
|
headers: HashMap<String, String>,
|
|
|
|
now: SystemTime,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CacheSemantics {
|
|
|
|
pub fn new(
|
|
|
|
headers: HashMap<String, String>,
|
|
|
|
cached: SystemTime,
|
|
|
|
now: SystemTime,
|
|
|
|
) -> Self {
|
|
|
|
let cache_control = headers
|
|
|
|
.get("cache-control")
|
|
|
|
.map(|v| CacheControl::from_value(v).unwrap_or_default())
|
|
|
|
.unwrap_or_default();
|
|
|
|
Self {
|
|
|
|
cache_control,
|
|
|
|
cached,
|
|
|
|
headers,
|
|
|
|
now,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn age(&self) -> Duration {
|
|
|
|
let mut age = self.age_header_value();
|
|
|
|
|
|
|
|
if let Ok(resident_time) = self.now.duration_since(self.cached) {
|
|
|
|
age += resident_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
age
|
|
|
|
}
|
|
|
|
|
|
|
|
fn age_header_value(&self) -> Duration {
|
|
|
|
Duration::from_secs(
|
|
|
|
self
|
|
|
|
.headers
|
|
|
|
.get("age")
|
|
|
|
.and_then(|v| v.parse().ok())
|
|
|
|
.unwrap_or(0),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_stale(&self) -> bool {
|
|
|
|
self.max_age() <= self.age()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn max_age(&self) -> Duration {
|
|
|
|
if self.cache_control.cachability == Some(Cachability::NoCache) {
|
|
|
|
return Duration::from_secs(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.headers.get("vary").map(|s| s.trim()) == Some("*") {
|
|
|
|
return Duration::from_secs(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(max_age) = self.cache_control.max_age {
|
|
|
|
return max_age;
|
|
|
|
}
|
|
|
|
|
|
|
|
let default_min_ttl = Duration::from_secs(0);
|
|
|
|
|
|
|
|
let server_date = self.raw_server_date();
|
|
|
|
if let Some(expires) = self.headers.get("expires") {
|
|
|
|
return match DateTime::parse_from_rfc2822(expires) {
|
|
|
|
Err(_) => Duration::from_secs(0),
|
|
|
|
Ok(expires) => {
|
|
|
|
let expires = SystemTime::UNIX_EPOCH
|
|
|
|
+ Duration::from_secs(expires.timestamp().max(0) as _);
|
|
|
|
return default_min_ttl
|
|
|
|
.max(expires.duration_since(server_date).unwrap_or_default());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(last_modified) = self.headers.get("last-modified") {
|
|
|
|
if let Ok(last_modified) = DateTime::parse_from_rfc2822(last_modified) {
|
|
|
|
let last_modified = SystemTime::UNIX_EPOCH
|
|
|
|
+ Duration::from_secs(last_modified.timestamp().max(0) as _);
|
|
|
|
if let Ok(diff) = server_date.duration_since(last_modified) {
|
|
|
|
let secs_left = diff.as_secs() as f64 * 0.1;
|
|
|
|
return default_min_ttl.max(Duration::from_secs(secs_left as _));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
default_min_ttl
|
|
|
|
}
|
|
|
|
|
|
|
|
fn raw_server_date(&self) -> SystemTime {
|
|
|
|
self
|
|
|
|
.headers
|
|
|
|
.get("date")
|
|
|
|
.and_then(|d| DateTime::parse_from_rfc2822(d).ok())
|
|
|
|
.and_then(|d| {
|
|
|
|
SystemTime::UNIX_EPOCH
|
|
|
|
.checked_add(Duration::from_secs(d.timestamp() as _))
|
|
|
|
})
|
|
|
|
.unwrap_or(self.cached)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the cached value is "fresh" respecting cached headers,
|
|
|
|
/// otherwise returns false.
|
|
|
|
pub fn should_use(&self) -> bool {
|
|
|
|
if self.cache_control.cachability == Some(Cachability::NoCache) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(max_age) = self.cache_control.max_age {
|
|
|
|
if self.age() > max_age {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(min_fresh) = self.cache_control.min_fresh {
|
|
|
|
if self.time_to_live() < min_fresh {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.is_stale() {
|
|
|
|
let has_max_stale = self.cache_control.max_stale.is_some();
|
|
|
|
let allows_stale = has_max_stale
|
|
|
|
&& self
|
|
|
|
.cache_control
|
|
|
|
.max_stale
|
|
|
|
.map_or(true, |val| val > self.age() - self.max_age());
|
|
|
|
if !allows_stale {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn time_to_live(&self) -> Duration {
|
|
|
|
self.max_age().checked_sub(self.age()).unwrap_or_default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-19 04:25:03 -04:00
|
|
|
#[derive(Debug, Eq, 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
|
|
|
}
|
|
|
|
|
2021-02-15 21:50:27 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct FetchOnceArgs {
|
|
|
|
pub url: Url,
|
2021-12-20 21:40:22 -05:00
|
|
|
pub maybe_accept: Option<String>,
|
2021-02-15 21:50:27 -05:00
|
|
|
pub maybe_etag: Option<String>,
|
|
|
|
pub maybe_auth_token: Option<AuthToken>,
|
|
|
|
}
|
|
|
|
|
2022-11-18 17:28:14 -05:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct HttpClient(reqwest::Client);
|
2019-04-25 13:29:21 -04:00
|
|
|
|
2022-11-18 17:28:14 -05:00
|
|
|
impl HttpClient {
|
|
|
|
pub fn new(
|
|
|
|
root_cert_store: Option<RootCertStore>,
|
|
|
|
unsafely_ignore_certificate_errors: Option<Vec<String>>,
|
|
|
|
) -> Result<Self, AnyError> {
|
|
|
|
Ok(HttpClient::from_client(create_http_client(
|
|
|
|
get_user_agent(),
|
|
|
|
root_cert_store,
|
|
|
|
vec![],
|
|
|
|
None,
|
|
|
|
unsafely_ignore_certificate_errors,
|
|
|
|
None,
|
|
|
|
)?))
|
2021-02-15 21:50:27 -05:00
|
|
|
}
|
2022-11-18 17:28:14 -05:00
|
|
|
|
|
|
|
pub fn from_client(client: reqwest::Client) -> Self {
|
|
|
|
Self(client)
|
2021-12-20 21:40:22 -05:00
|
|
|
}
|
2020-01-11 05:11:05 -05:00
|
|
|
|
2022-11-18 17:28:14 -05:00
|
|
|
pub fn get<U: reqwest::IntoUrl>(&self, url: U) -> reqwest::RequestBuilder {
|
|
|
|
self.0.get(url)
|
2020-09-04 06:43:20 -04:00
|
|
|
}
|
2019-12-30 08:57:17 -05:00
|
|
|
|
2022-12-12 20:52:10 -05:00
|
|
|
pub async fn download_with_progress<U: reqwest::IntoUrl>(
|
|
|
|
&self,
|
|
|
|
url: U,
|
|
|
|
progress_guard: &UpdateGuard,
|
|
|
|
) -> Result<Option<Vec<u8>>, AnyError> {
|
|
|
|
let response = self.get(url).send().await?;
|
|
|
|
|
|
|
|
if response.status() == 404 {
|
|
|
|
Ok(None)
|
|
|
|
} else if !response.status().is_success() {
|
|
|
|
let status = response.status();
|
|
|
|
let maybe_response_text = response.text().await.ok();
|
|
|
|
bail!(
|
|
|
|
"Bad response: {:?}{}",
|
|
|
|
status,
|
|
|
|
match maybe_response_text {
|
|
|
|
Some(text) => format!("\n\n{}", text),
|
|
|
|
None => String::new(),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else if let Some(total_size) = response.content_length() {
|
|
|
|
progress_guard.set_total_size(total_size);
|
|
|
|
let mut current_size = 0;
|
|
|
|
let mut data = Vec::with_capacity(total_size as usize);
|
|
|
|
let mut stream = response.bytes_stream();
|
|
|
|
while let Some(item) = stream.next().await {
|
|
|
|
let bytes = item?;
|
|
|
|
current_size += bytes.len() as u64;
|
|
|
|
progress_guard.set_position(current_size);
|
|
|
|
data.extend(bytes.into_iter());
|
|
|
|
}
|
|
|
|
Ok(Some(data))
|
|
|
|
} else {
|
|
|
|
let bytes = response.bytes().await?;
|
|
|
|
Ok(Some(bytes.into()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 17:28:14 -05:00
|
|
|
/// Asynchronously fetches the given HTTP URL one pass only.
|
|
|
|
/// If no redirect is present and no error occurs,
|
|
|
|
/// yields Code(ResultPayload).
|
|
|
|
/// If redirect occurs, does not follow and
|
|
|
|
/// yields Redirect(url).
|
|
|
|
pub async fn fetch_once(
|
|
|
|
&self,
|
|
|
|
args: FetchOnceArgs,
|
|
|
|
) -> Result<FetchOnceResult, AnyError> {
|
|
|
|
let mut request = self.get(args.url.clone());
|
2020-05-09 12:43:24 -04:00
|
|
|
|
2022-11-18 17:28:14 -05:00
|
|
|
if let Some(etag) = args.maybe_etag {
|
|
|
|
let if_none_match_val = HeaderValue::from_str(&etag)?;
|
|
|
|
request = request.header(IF_NONE_MATCH, if_none_match_val);
|
|
|
|
}
|
|
|
|
if let Some(auth_token) = args.maybe_auth_token {
|
|
|
|
let authorization_val = HeaderValue::from_str(&auth_token.to_string())?;
|
|
|
|
request = request.header(AUTHORIZATION, authorization_val);
|
|
|
|
}
|
|
|
|
if let Some(accept) = args.maybe_accept {
|
|
|
|
let accepts_val = HeaderValue::from_str(&accept)?;
|
|
|
|
request = request.header(ACCEPT, accepts_val);
|
|
|
|
}
|
|
|
|
let response = request.send().await?;
|
2020-02-19 08:17:13 -05:00
|
|
|
|
2022-11-18 17:28:14 -05:00
|
|
|
if response.status() == StatusCode::NOT_MODIFIED {
|
|
|
|
return Ok(FetchOnceResult::NotModified);
|
|
|
|
}
|
2019-12-30 08:57:17 -05:00
|
|
|
|
2022-11-18 17:28:14 -05:00
|
|
|
let mut result_headers = HashMap::new();
|
|
|
|
let response_headers = response.headers();
|
|
|
|
|
|
|
|
if let Some(warning) = response_headers.get("X-Deno-Warning") {
|
2022-12-09 10:54:24 -05:00
|
|
|
log::warn!(
|
2022-11-18 17:28:14 -05:00
|
|
|
"{} {}",
|
|
|
|
crate::colors::yellow("Warning"),
|
|
|
|
warning.to_str().unwrap()
|
|
|
|
);
|
2019-12-30 08:57:17 -05:00
|
|
|
}
|
|
|
|
|
2022-11-18 17:28:14 -05:00
|
|
|
for key in response_headers.keys() {
|
|
|
|
let key_str = key.to_string();
|
|
|
|
let values = response_headers.get_all(key);
|
|
|
|
let values_str = values
|
|
|
|
.iter()
|
|
|
|
.map(|e| e.to_str().unwrap().to_string())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(",");
|
|
|
|
result_headers.insert(key_str, values_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.status().is_redirection() {
|
|
|
|
if let Some(location) = response.headers().get(LOCATION) {
|
|
|
|
let location_string = location.to_str().unwrap();
|
|
|
|
debug!("Redirecting to {:?}...", &location_string);
|
|
|
|
let new_url = resolve_url_from_location(&args.url, location_string);
|
|
|
|
return Ok(FetchOnceResult::Redirect(new_url, result_headers));
|
|
|
|
} else {
|
|
|
|
return Err(generic_error(format!(
|
|
|
|
"Redirection from '{}' did not provide location header",
|
|
|
|
args.url
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.status().is_client_error()
|
|
|
|
|| response.status().is_server_error()
|
|
|
|
{
|
|
|
|
let err = if response.status() == StatusCode::NOT_FOUND {
|
|
|
|
custom_error(
|
|
|
|
"NotFound",
|
|
|
|
format!("Import '{}' failed, not found.", args.url),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
generic_error(format!(
|
|
|
|
"Import '{}' failed: {}",
|
|
|
|
args.url,
|
|
|
|
response.status()
|
|
|
|
))
|
|
|
|
};
|
|
|
|
return Err(err);
|
|
|
|
}
|
2020-01-11 05:11:05 -05:00
|
|
|
|
2022-11-18 17:28:14 -05:00
|
|
|
let body = response.bytes().await?.to_vec();
|
2019-12-30 08:57:17 -05:00
|
|
|
|
2022-11-18 17:28:14 -05:00
|
|
|
Ok(FetchOnceResult::Code(body, result_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-12-01 11:13:11 -05:00
|
|
|
use deno_runtime::deno_fetch::create_http_client;
|
2021-01-04 18:15:52 -05:00
|
|
|
use std::fs::read;
|
2018-08-14 16:50:53 -04:00
|
|
|
|
2022-11-18 17:28:14 -05:00
|
|
|
fn create_test_client() -> HttpClient {
|
|
|
|
HttpClient::from_client(
|
|
|
|
create_http_client(
|
|
|
|
"test_client".to_string(),
|
|
|
|
None,
|
|
|
|
vec![],
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2021-08-25 08:25:12 -04:00
|
|
|
)
|
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
|
2022-09-19 10:32:21 -04:00
|
|
|
let url = Url::parse("http://127.0.0.1:4545/assets/fixture.json").unwrap();
|
2021-09-30 03:26:15 -04:00
|
|
|
let client = create_test_client();
|
2022-11-18 17:28:14 -05:00
|
|
|
let result = client
|
|
|
|
.fetch_once(FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: 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
|
2022-09-19 10:32:21 -04:00
|
|
|
let url = Url::parse("http://127.0.0.1:4545/run/import_compression/gziped")
|
2021-08-11 10:20:47 -04:00
|
|
|
.unwrap();
|
2021-09-30 03:26:15 -04:00
|
|
|
let client = create_test_client();
|
2022-11-18 17:28:14 -05:00
|
|
|
let result = client
|
|
|
|
.fetch_once(FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: 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();
|
2021-09-30 03:26:15 -04:00
|
|
|
let client = create_test_client();
|
2022-11-18 17:28:14 -05:00
|
|
|
let result = client
|
|
|
|
.fetch_once(FetchOnceArgs {
|
|
|
|
url: url.clone(),
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: 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
|
|
|
|
2022-11-18 17:28:14 -05:00
|
|
|
let res = client
|
|
|
|
.fetch_once(FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: Some("33a64df551425fcc55e".to_string()),
|
|
|
|
maybe_auth_token: None,
|
|
|
|
})
|
|
|
|
.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
|
2022-09-19 10:32:21 -04:00
|
|
|
let url = Url::parse("http://127.0.0.1:4545/run/import_compression/brotli")
|
2021-08-11 10:20:47 -04:00
|
|
|
.unwrap();
|
2021-09-30 03:26:15 -04:00
|
|
|
let client = create_test_client();
|
2022-11-18 17:28:14 -05:00
|
|
|
let result = client
|
|
|
|
.fetch_once(FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: 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
|
|
|
|
2021-12-20 21:40:22 -05:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_accept() {
|
|
|
|
let _http_server_guard = test_util::http_server();
|
|
|
|
// Relies on external http server. See target/debug/test_server
|
|
|
|
let url = Url::parse("http://127.0.0.1:4545/echo_accept").unwrap();
|
|
|
|
let client = create_test_client();
|
2022-11-18 17:28:14 -05:00
|
|
|
let result = client
|
|
|
|
.fetch_once(FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: Some("application/json".to_string()),
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: None,
|
|
|
|
})
|
|
|
|
.await;
|
2021-12-20 21:40:22 -05:00
|
|
|
if let Ok(FetchOnceResult::Code(body, _)) = result {
|
|
|
|
assert_eq!(body, r#"{"accept":"application/json"}"#.as_bytes());
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2022-09-19 10:32:21 -04:00
|
|
|
let url = Url::parse("http://127.0.0.1:4546/assets/fixture.json").unwrap();
|
2019-04-25 13:29:21 -04:00
|
|
|
// Dns resolver substitutes `127.0.0.1` with `localhost`
|
2022-09-19 10:32:21 -04:00
|
|
|
let target_url =
|
|
|
|
Url::parse("http://localhost:4545/assets/fixture.json").unwrap();
|
2021-09-30 03:26:15 -04:00
|
|
|
let client = create_test_client();
|
2022-11-18 17:28:14 -05:00
|
|
|
let result = client
|
|
|
|
.fetch_once(FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: 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
|
2022-09-19 10:32:21 -04:00
|
|
|
let url = Url::parse("https://localhost:5545/assets/fixture.json").unwrap();
|
2021-01-04 18:15:52 -05:00
|
|
|
|
2022-11-18 17:28:14 -05:00
|
|
|
let client = HttpClient::from_client(
|
|
|
|
create_http_client(
|
|
|
|
version::get_user_agent(),
|
|
|
|
None,
|
|
|
|
vec![read(
|
|
|
|
test_util::testdata_path()
|
|
|
|
.join("tls/RootCA.pem")
|
|
|
|
.to_str()
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap()],
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
2021-09-30 03:26:15 -04:00
|
|
|
)
|
2022-11-18 17:28:14 -05:00
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
let result = client
|
|
|
|
.fetch_once(FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: 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!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-07 08:49:38 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_with_default_certificate_store() {
|
|
|
|
let _http_server_guard = test_util::http_server();
|
|
|
|
// Relies on external http server with a valid mozilla root CA cert.
|
|
|
|
let url = Url::parse("https://deno.land").unwrap();
|
2022-11-18 17:28:14 -05:00
|
|
|
let client = HttpClient::from_client(
|
|
|
|
create_http_client(
|
|
|
|
version::get_user_agent(),
|
|
|
|
None, // This will load mozilla certs by default
|
|
|
|
vec![],
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
);
|
2021-08-07 08:49:38 -04:00
|
|
|
|
2022-11-18 17:28:14 -05:00
|
|
|
let result = client
|
|
|
|
.fetch_once(FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: None,
|
|
|
|
})
|
|
|
|
.await;
|
2021-08-07 08:49:38 -04:00
|
|
|
|
|
|
|
println!("{:?}", result);
|
|
|
|
if let Ok(FetchOnceResult::Code(body, _headers)) = result {
|
|
|
|
assert!(!body.is_empty());
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(@justinmchase): Windows should verify certs too and fail to make this request without ca certs
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
#[tokio::test]
|
2022-03-15 11:19:59 -04:00
|
|
|
#[ignore] // https://github.com/denoland/deno/issues/12561
|
2021-08-07 08:49:38 -04:00
|
|
|
async fn test_fetch_with_empty_certificate_store() {
|
2021-12-08 19:12:14 -05:00
|
|
|
use deno_runtime::deno_tls::rustls::RootCertStore;
|
|
|
|
|
2021-08-07 08:49:38 -04:00
|
|
|
let _http_server_guard = test_util::http_server();
|
|
|
|
// Relies on external http server with a valid mozilla root CA cert.
|
|
|
|
let url = Url::parse("https://deno.land").unwrap();
|
2022-11-18 17:28:14 -05:00
|
|
|
let client = HttpClient::new(
|
2021-12-01 11:13:11 -05:00
|
|
|
Some(RootCertStore::empty()), // no certs loaded at all
|
2021-08-25 08:25:12 -04:00
|
|
|
None,
|
2021-08-07 08:49:38 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2022-11-18 17:28:14 -05:00
|
|
|
let result = client
|
|
|
|
.fetch_once(FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: None,
|
|
|
|
})
|
|
|
|
.await;
|
2021-08-07 08:49:38 -04:00
|
|
|
|
|
|
|
if let Ok(FetchOnceResult::Code(_body, _headers)) = result {
|
|
|
|
// This test is expected to fail since to CA certs have been loaded
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 11:59:51 -05:00
|
|
|
#[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
|
2021-08-11 10:20:47 -04:00
|
|
|
let url =
|
2022-09-19 10:32:21 -04:00
|
|
|
Url::parse("https://localhost:5545/run/import_compression/gziped")
|
2021-08-11 10:20:47 -04:00
|
|
|
.unwrap();
|
2022-11-18 17:28:14 -05:00
|
|
|
let client = HttpClient::from_client(
|
|
|
|
create_http_client(
|
|
|
|
version::get_user_agent(),
|
|
|
|
None,
|
|
|
|
vec![read(
|
|
|
|
test_util::testdata_path()
|
|
|
|
.join("tls/RootCA.pem")
|
|
|
|
.to_str()
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap()],
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
2021-09-30 03:26:15 -04:00
|
|
|
)
|
2022-11-18 17:28:14 -05:00
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
let result = client
|
|
|
|
.fetch_once(FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: 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();
|
2022-11-18 17:28:14 -05:00
|
|
|
let client = HttpClient::from_client(
|
|
|
|
create_http_client(
|
|
|
|
version::get_user_agent(),
|
|
|
|
None,
|
|
|
|
vec![read(
|
|
|
|
test_util::testdata_path()
|
|
|
|
.join("tls/RootCA.pem")
|
|
|
|
.to_str()
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap()],
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
2021-09-30 03:26:15 -04:00
|
|
|
)
|
2022-11-18 17:28:14 -05:00
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
let result = client
|
|
|
|
.fetch_once(FetchOnceArgs {
|
|
|
|
url: url.clone(),
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: 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!();
|
|
|
|
}
|
|
|
|
|
2022-11-18 17:28:14 -05:00
|
|
|
let res = client
|
|
|
|
.fetch_once(FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: Some("33a64df551425fcc55e".to_string()),
|
|
|
|
maybe_auth_token: None,
|
|
|
|
})
|
|
|
|
.await;
|
2020-02-17 11:59:51 -05:00
|
|
|
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
|
2021-08-11 10:20:47 -04:00
|
|
|
let url =
|
2022-09-19 10:32:21 -04:00
|
|
|
Url::parse("https://localhost:5545/run/import_compression/brotli")
|
2021-08-11 10:20:47 -04:00
|
|
|
.unwrap();
|
2022-11-18 17:28:14 -05:00
|
|
|
let client = HttpClient::from_client(
|
|
|
|
create_http_client(
|
|
|
|
version::get_user_agent(),
|
|
|
|
None,
|
|
|
|
vec![read(
|
|
|
|
test_util::testdata_path()
|
|
|
|
.join("tls/RootCA.pem")
|
|
|
|
.to_str()
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap()],
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
2021-09-30 03:26:15 -04:00
|
|
|
)
|
2022-11-18 17:28:14 -05:00
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
let result = client
|
|
|
|
.fetch_once(FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: 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();
|
2021-09-30 03:26:15 -04:00
|
|
|
let client = create_test_client();
|
2022-11-18 17:28:14 -05:00
|
|
|
let result = client
|
|
|
|
.fetch_once(FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: None,
|
|
|
|
})
|
|
|
|
.await;
|
2020-09-04 06:43:20 -04:00
|
|
|
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
|
|
|
}
|