2020-01-02 20:13:47 +00:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-06-20 02:07:01 +00:00
|
|
|
use crate::deno_error;
|
|
|
|
use crate::deno_error::DenoError;
|
2019-09-11 11:31:00 +00:00
|
|
|
use crate::version;
|
2020-01-11 10:11:05 +00:00
|
|
|
use brotli2::read::BrotliDecoder;
|
2019-12-31 14:09:58 +00:00
|
|
|
use bytes::Bytes;
|
2020-01-05 16:56:18 +00:00
|
|
|
use deno_core::ErrBox;
|
2019-11-17 00:17:47 +00:00
|
|
|
use futures::future::FutureExt;
|
2019-04-25 17:29:21 +00:00
|
|
|
use reqwest;
|
2020-01-26 18:59:41 +00:00
|
|
|
use reqwest::header::HeaderMap;
|
|
|
|
use reqwest::header::HeaderValue;
|
2020-01-11 10:11:05 +00:00
|
|
|
use reqwest::header::ACCEPT_ENCODING;
|
|
|
|
use reqwest::header::CONTENT_ENCODING;
|
2019-04-25 17:29:21 +00:00
|
|
|
use reqwest::header::CONTENT_TYPE;
|
2020-01-11 10:11:05 +00:00
|
|
|
use reqwest::header::ETAG;
|
|
|
|
use reqwest::header::IF_NONE_MATCH;
|
2019-04-25 17:29:21 +00:00
|
|
|
use reqwest::header::LOCATION;
|
2019-09-11 11:31:00 +00:00
|
|
|
use reqwest::header::USER_AGENT;
|
2019-12-30 13:57:17 +00:00
|
|
|
use reqwest::redirect::Policy;
|
|
|
|
use reqwest::Client;
|
2019-12-31 14:09:58 +00:00
|
|
|
use reqwest::Response;
|
2020-01-11 10:11:05 +00:00
|
|
|
use reqwest::StatusCode;
|
2019-12-31 14:09:58 +00:00
|
|
|
use std::cmp::min;
|
2019-11-17 00:17:47 +00:00
|
|
|
use std::future::Future;
|
2019-12-31 14:09:58 +00:00
|
|
|
use std::io;
|
2020-01-11 10:11:05 +00:00
|
|
|
use std::io::Read;
|
2019-12-31 14:09:58 +00:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::Context;
|
|
|
|
use std::task::Poll;
|
|
|
|
use tokio::io::AsyncRead;
|
2019-04-25 17:29:21 +00:00
|
|
|
use url::Url;
|
|
|
|
|
2020-01-15 18:23:29 +00:00
|
|
|
/// Create new instance of async reqwest::Client. This client supports
|
2019-04-25 17:29:21 +00:00
|
|
|
/// proxies and doesn't follow redirects.
|
2020-01-16 15:10:01 +00:00
|
|
|
pub fn create_http_client() -> Client {
|
2020-01-15 18:23:29 +00:00
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
headers.insert(
|
|
|
|
USER_AGENT,
|
|
|
|
format!("Deno/{}", version::DENO).parse().unwrap(),
|
|
|
|
);
|
|
|
|
Client::builder()
|
|
|
|
.redirect(Policy::none())
|
|
|
|
.default_headers(headers)
|
|
|
|
.use_rustls_tls()
|
|
|
|
.build()
|
|
|
|
.unwrap()
|
2018-09-24 23:51:37 +00:00
|
|
|
}
|
2018-08-14 20:50:53 +00:00
|
|
|
|
2018-11-30 03:01:01 +00:00
|
|
|
/// Construct the next uri based on base uri and location header fragment
|
2018-11-30 08:30:49 +00:00
|
|
|
/// See <https://tools.ietf.org/html/rfc3986#section-4.2>
|
2019-04-25 17:29:21 +00:00
|
|
|
fn resolve_url_from_location(base_url: &Url, location: &str) -> Url {
|
2018-11-30 03:01:01 +00:00
|
|
|
if location.starts_with("http://") || location.starts_with("https://") {
|
|
|
|
// absolute uri
|
2019-04-25 17:29:21 +00:00
|
|
|
Url::parse(location).expect("provided redirect url should be a valid url")
|
2018-11-30 03:01:01 +00:00
|
|
|
} else if location.starts_with("//") {
|
|
|
|
// "//" authority path-abempty
|
2019-04-25 17:29:21 +00:00
|
|
|
Url::parse(&format!("{}:{}", base_url.scheme(), location))
|
2018-11-30 08:30:49 +00:00
|
|
|
.expect("provided redirect url should be a valid url")
|
|
|
|
} else if location.starts_with('/') {
|
2018-11-30 03:01:01 +00:00
|
|
|
// path-absolute
|
2019-04-25 17:29:21 +00:00
|
|
|
base_url
|
|
|
|
.join(location)
|
|
|
|
.expect("provided redirect url should be a valid url")
|
2018-11-30 03:01:01 +00:00
|
|
|
} else {
|
|
|
|
// assuming path-noscheme | path-empty
|
2019-04-25 17:29:21 +00: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-30 03:01:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-26 18:59:41 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub struct ResultPayload {
|
2020-02-04 16:27:50 +00:00
|
|
|
pub body: Vec<u8>,
|
2020-01-26 18:59:41 +00:00
|
|
|
pub content_type: Option<String>,
|
|
|
|
pub etag: Option<String>,
|
|
|
|
pub x_typescript_types: Option<String>,
|
|
|
|
}
|
|
|
|
|
2019-04-25 17:29:21 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
2019-04-02 01:46:40 +00:00
|
|
|
pub enum FetchOnceResult {
|
2020-01-26 18:59:41 +00:00
|
|
|
Code(ResultPayload),
|
2020-01-11 10:11:05 +00:00
|
|
|
NotModified,
|
2019-04-25 17:29:21 +00:00
|
|
|
Redirect(Url),
|
2019-04-02 01:46:40 +00:00
|
|
|
}
|
|
|
|
|
2020-01-02 12:48:46 +00:00
|
|
|
/// Asynchronously fetches the given HTTP URL one pass only.
|
2019-04-02 01:46:40 +00:00
|
|
|
/// If no redirect is present and no error occurs,
|
2020-01-26 18:59:41 +00:00
|
|
|
/// yields Code(ResultPayload).
|
2019-04-02 01:46:40 +00:00
|
|
|
/// If redirect occurs, does not follow and
|
|
|
|
/// yields Redirect(url).
|
2020-02-04 16:27:50 +00:00
|
|
|
pub fn fetch_once(
|
2020-01-16 15:10:01 +00:00
|
|
|
client: Client,
|
2019-04-25 17:29:21 +00:00
|
|
|
url: &Url,
|
2020-01-11 10:11:05 +00:00
|
|
|
cached_etag: Option<String>,
|
2019-11-17 00:17:47 +00:00
|
|
|
) -> impl Future<Output = Result<FetchOnceResult, ErrBox>> {
|
2019-04-25 17:29:21 +00:00
|
|
|
let url = url.clone();
|
|
|
|
|
2019-12-30 13:57:17 +00:00
|
|
|
let fut = async move {
|
2020-01-11 10:11:05 +00:00
|
|
|
let mut request = client
|
|
|
|
.get(url.clone())
|
|
|
|
.header(ACCEPT_ENCODING, HeaderValue::from_static("gzip, br"));
|
|
|
|
|
|
|
|
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?;
|
|
|
|
|
|
|
|
if response.status() == StatusCode::NOT_MODIFIED {
|
|
|
|
return Ok(FetchOnceResult::NotModified);
|
|
|
|
}
|
2019-12-30 13:57:17 +00:00
|
|
|
|
|
|
|
if response.status().is_redirection() {
|
|
|
|
let location_string = response
|
|
|
|
.headers()
|
|
|
|
.get(LOCATION)
|
|
|
|
.expect("url redirection should provide 'location' header")
|
|
|
|
.to_str()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
debug!("Redirecting to {:?}...", &location_string);
|
|
|
|
let new_url = resolve_url_from_location(&url, location_string);
|
|
|
|
return Ok(FetchOnceResult::Redirect(new_url));
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.status().is_client_error()
|
|
|
|
|| response.status().is_server_error()
|
|
|
|
{
|
|
|
|
let err = DenoError::new(
|
|
|
|
deno_error::ErrorKind::Other,
|
|
|
|
format!("Import '{}' failed: {}", &url, response.status()),
|
|
|
|
);
|
|
|
|
return Err(err.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
let content_type = response
|
|
|
|
.headers()
|
|
|
|
.get(CONTENT_TYPE)
|
|
|
|
.map(|content_type| content_type.to_str().unwrap().to_owned());
|
|
|
|
|
2020-01-11 10:11:05 +00:00
|
|
|
let etag = response
|
|
|
|
.headers()
|
|
|
|
.get(ETAG)
|
|
|
|
.map(|etag| etag.to_str().unwrap().to_owned());
|
|
|
|
|
|
|
|
let content_encoding = response
|
|
|
|
.headers()
|
|
|
|
.get(CONTENT_ENCODING)
|
|
|
|
.map(|content_encoding| content_encoding.to_str().unwrap().to_owned());
|
|
|
|
|
2020-01-26 18:59:41 +00:00
|
|
|
const X_TYPESCRIPT_TYPES: &str = "X-TypeScript-Types";
|
|
|
|
|
|
|
|
let x_typescript_types =
|
|
|
|
response
|
|
|
|
.headers()
|
|
|
|
.get(X_TYPESCRIPT_TYPES)
|
|
|
|
.map(|x_typescript_types| {
|
|
|
|
x_typescript_types.to_str().unwrap().to_owned()
|
|
|
|
});
|
|
|
|
|
2020-01-11 10:11:05 +00:00
|
|
|
let body;
|
|
|
|
if let Some(content_encoding) = content_encoding {
|
|
|
|
body = match content_encoding {
|
|
|
|
_ if content_encoding == "br" => {
|
|
|
|
let full_bytes = response.bytes().await?;
|
|
|
|
let mut decoder = BrotliDecoder::new(full_bytes.as_ref());
|
2020-02-04 16:27:50 +00:00
|
|
|
let mut body = vec![];
|
|
|
|
decoder.read_to_end(&mut body)?;
|
2020-01-11 10:11:05 +00:00
|
|
|
body
|
|
|
|
}
|
2020-02-04 16:27:50 +00:00
|
|
|
_ => response.bytes().await?.to_vec(),
|
2020-01-11 10:11:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-02-04 16:27:50 +00:00
|
|
|
body = response.bytes().await?.to_vec();
|
2020-01-11 10:11:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 18:59:41 +00:00
|
|
|
return Ok(FetchOnceResult::Code(ResultPayload {
|
|
|
|
body,
|
|
|
|
content_type,
|
|
|
|
etag,
|
|
|
|
x_typescript_types,
|
|
|
|
}));
|
2019-12-30 13:57:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
fut.boxed()
|
2019-04-02 01:46:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-31 14:09:58 +00:00
|
|
|
/// Wraps reqwest `Response` so that it can be exposed as an `AsyncRead` and integrated
|
|
|
|
/// into resources more easily.
|
|
|
|
pub struct HttpBody {
|
|
|
|
response: Response,
|
|
|
|
chunk: Option<Bytes>,
|
|
|
|
pos: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HttpBody {
|
|
|
|
pub fn from(body: Response) -> Self {
|
|
|
|
Self {
|
|
|
|
response: body,
|
|
|
|
chunk: None,
|
|
|
|
pos: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsyncRead for HttpBody {
|
|
|
|
fn poll_read(
|
|
|
|
self: Pin<&mut Self>,
|
|
|
|
cx: &mut Context,
|
|
|
|
buf: &mut [u8],
|
|
|
|
) -> Poll<Result<usize, io::Error>> {
|
|
|
|
let mut inner = self.get_mut();
|
|
|
|
if let Some(chunk) = inner.chunk.take() {
|
|
|
|
debug!(
|
|
|
|
"HttpBody Fake Read buf {} chunk {} pos {}",
|
|
|
|
buf.len(),
|
|
|
|
chunk.len(),
|
|
|
|
inner.pos
|
|
|
|
);
|
|
|
|
let n = min(buf.len(), chunk.len() - inner.pos);
|
|
|
|
{
|
|
|
|
let rest = &chunk[inner.pos..];
|
|
|
|
buf[..n].clone_from_slice(&rest[..n]);
|
|
|
|
}
|
|
|
|
inner.pos += n;
|
|
|
|
if inner.pos == chunk.len() {
|
|
|
|
inner.pos = 0;
|
|
|
|
} else {
|
|
|
|
inner.chunk = Some(chunk);
|
|
|
|
}
|
|
|
|
return Poll::Ready(Ok(n));
|
|
|
|
} else {
|
|
|
|
assert_eq!(inner.pos, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
let chunk_future = &mut inner.response.chunk();
|
|
|
|
// Safety: `chunk_future` lives only for duration of this poll. So, it doesn't move.
|
|
|
|
let chunk_future = unsafe { Pin::new_unchecked(chunk_future) };
|
|
|
|
match chunk_future.poll(cx) {
|
|
|
|
Poll::Ready(Err(e)) => {
|
|
|
|
Poll::Ready(Err(io::Error::new(io::ErrorKind::Other, e)))
|
|
|
|
}
|
|
|
|
Poll::Ready(Ok(Some(chunk))) => {
|
|
|
|
debug!(
|
|
|
|
"HttpBody Real Read buf {} chunk {} pos {}",
|
|
|
|
buf.len(),
|
|
|
|
chunk.len(),
|
|
|
|
inner.pos
|
|
|
|
);
|
|
|
|
let n = min(buf.len(), chunk.len());
|
|
|
|
buf[..n].clone_from_slice(&chunk[..n]);
|
|
|
|
if buf.len() < chunk.len() {
|
|
|
|
inner.pos = n;
|
|
|
|
inner.chunk = Some(chunk);
|
|
|
|
}
|
|
|
|
Poll::Ready(Ok(n))
|
|
|
|
}
|
|
|
|
Poll::Ready(Ok(None)) => Poll::Ready(Ok(0)),
|
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 01:46:40 +00:00
|
|
|
#[cfg(test)]
|
2019-04-25 17:29:21 +00:00
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-08-14 20:50:53 +00:00
|
|
|
|
2020-02-03 13:53:50 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_sync_string() {
|
2019-09-19 18:48:05 +00:00
|
|
|
let http_server_guard = crate::test_util::http_server();
|
2019-04-25 17:29:21 +00:00
|
|
|
// Relies on external http server. See tools/http_server.py
|
2019-11-01 02:33:27 +00:00
|
|
|
let url =
|
|
|
|
Url::parse("http://127.0.0.1:4545/cli/tests/fixture.json").unwrap();
|
2020-01-16 15:10:01 +00:00
|
|
|
let client = create_http_client();
|
2020-02-04 16:27:50 +00:00
|
|
|
let result = fetch_once(client, &url, None).await;
|
2020-02-03 13:53:50 +00:00
|
|
|
if let Ok(FetchOnceResult::Code(payload)) = result {
|
|
|
|
assert!(!payload.body.is_empty());
|
|
|
|
assert_eq!(payload.content_type, Some("application/json".to_string()));
|
|
|
|
assert_eq!(payload.etag, None);
|
|
|
|
assert_eq!(payload.x_typescript_types, None);
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
2020-01-11 10:11:05 +00:00
|
|
|
drop(http_server_guard);
|
|
|
|
}
|
|
|
|
|
2020-02-03 13:53:50 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_gzip() {
|
2020-01-11 10:11:05 +00:00
|
|
|
let http_server_guard = crate::test_util::http_server();
|
|
|
|
// Relies on external http server. See tools/http_server.py
|
|
|
|
let url = Url::parse(
|
|
|
|
"http://127.0.0.1:4545/cli/tests/053_import_compression/gziped",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-01-16 15:10:01 +00:00
|
|
|
let client = create_http_client();
|
2020-02-04 16:27:50 +00:00
|
|
|
let result = fetch_once(client, &url, None).await;
|
2020-02-03 13:53:50 +00:00
|
|
|
if let Ok(FetchOnceResult::Code(payload)) = result {
|
2020-02-04 16:27:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
String::from_utf8(payload.body).unwrap(),
|
|
|
|
"console.log('gzip')"
|
|
|
|
);
|
2020-02-03 13:53:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
payload.content_type,
|
|
|
|
Some("application/javascript".to_string())
|
|
|
|
);
|
|
|
|
assert_eq!(payload.etag, None);
|
|
|
|
assert_eq!(payload.x_typescript_types, None);
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
2020-01-11 10:11:05 +00:00
|
|
|
drop(http_server_guard);
|
|
|
|
}
|
|
|
|
|
2020-02-03 13:53:50 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_with_etag() {
|
2020-01-11 10:11:05 +00:00
|
|
|
let http_server_guard = crate::test_util::http_server();
|
|
|
|
let url = Url::parse("http://127.0.0.1:4545/etag_script.ts").unwrap();
|
2020-01-16 15:10:01 +00:00
|
|
|
let client = create_http_client();
|
2020-02-04 16:27:50 +00:00
|
|
|
let result = fetch_once(client.clone(), &url, None).await;
|
2020-02-03 13:53:50 +00:00
|
|
|
if let Ok(FetchOnceResult::Code(ResultPayload {
|
|
|
|
body,
|
|
|
|
content_type,
|
|
|
|
etag,
|
|
|
|
x_typescript_types,
|
|
|
|
})) = result
|
|
|
|
{
|
|
|
|
assert!(!body.is_empty());
|
2020-02-04 16:27:50 +00:00
|
|
|
assert_eq!(String::from_utf8(body).unwrap(), "console.log('etag')");
|
2020-02-03 13:53:50 +00:00
|
|
|
assert_eq!(content_type, Some("application/typescript".to_string()));
|
|
|
|
assert_eq!(etag, Some("33a64df551425fcc55e".to_string()));
|
|
|
|
assert_eq!(x_typescript_types, None);
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
2020-01-11 10:11:05 +00:00
|
|
|
|
2020-02-03 13:53:50 +00:00
|
|
|
let res =
|
2020-02-04 16:27:50 +00:00
|
|
|
fetch_once(client, &url, Some("33a64df551425fcc55e".to_string())).await;
|
2020-02-03 13:53:50 +00:00
|
|
|
assert_eq!(res.unwrap(), FetchOnceResult::NotModified);
|
2020-01-11 10:11:05 +00:00
|
|
|
|
|
|
|
drop(http_server_guard);
|
|
|
|
}
|
|
|
|
|
2020-02-03 13:53:50 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_brotli() {
|
2020-01-11 10:11:05 +00:00
|
|
|
let http_server_guard = crate::test_util::http_server();
|
|
|
|
// Relies on external http server. See tools/http_server.py
|
|
|
|
let url = Url::parse(
|
|
|
|
"http://127.0.0.1:4545/cli/tests/053_import_compression/brotli",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-01-16 15:10:01 +00:00
|
|
|
let client = create_http_client();
|
2020-02-04 16:27:50 +00:00
|
|
|
let result = fetch_once(client, &url, None).await;
|
2020-02-03 13:53:50 +00:00
|
|
|
if let Ok(FetchOnceResult::Code(payload)) = result {
|
|
|
|
assert!(!payload.body.is_empty());
|
2020-02-04 16:27:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
String::from_utf8(payload.body).unwrap(),
|
|
|
|
"console.log('brotli');"
|
|
|
|
);
|
2020-02-03 13:53:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
payload.content_type,
|
|
|
|
Some("application/javascript".to_string())
|
|
|
|
);
|
|
|
|
assert_eq!(payload.etag, None);
|
|
|
|
assert_eq!(payload.x_typescript_types, None);
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
2019-09-19 18:48:05 +00:00
|
|
|
drop(http_server_guard);
|
2019-04-25 17:29:21 +00:00
|
|
|
}
|
2019-03-19 18:06:44 +00:00
|
|
|
|
2020-02-03 13:53:50 +00:00
|
|
|
#[tokio::test]
|
2020-02-04 16:27:50 +00:00
|
|
|
async fn test_fetch_once_with_redirect() {
|
2019-09-19 18:48:05 +00:00
|
|
|
let http_server_guard = crate::test_util::http_server();
|
2019-04-25 17:29:21 +00:00
|
|
|
// Relies on external http server. See tools/http_server.py
|
2019-11-01 02:33:27 +00:00
|
|
|
let url =
|
|
|
|
Url::parse("http://127.0.0.1:4546/cli/tests/fixture.json").unwrap();
|
2019-04-25 17:29:21 +00:00
|
|
|
// Dns resolver substitutes `127.0.0.1` with `localhost`
|
2019-11-01 02:33:27 +00:00
|
|
|
let target_url =
|
|
|
|
Url::parse("http://localhost:4545/cli/tests/fixture.json").unwrap();
|
2020-01-16 15:10:01 +00:00
|
|
|
let client = create_http_client();
|
2020-02-04 16:27:50 +00:00
|
|
|
let result = fetch_once(client, &url, None).await;
|
2020-02-03 13:53:50 +00:00
|
|
|
if let Ok(FetchOnceResult::Redirect(url)) = result {
|
|
|
|
assert_eq!(url, target_url);
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
2019-09-19 18:48:05 +00:00
|
|
|
drop(http_server_guard);
|
2019-04-25 17:29:21 +00:00
|
|
|
}
|
2018-11-30 03:01:01 +00:00
|
|
|
|
2019-04-25 17:29:21 +00: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-30 03:01:01 +00:00
|
|
|
|
2019-04-25 17:29:21 +00: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-30 03:01:01 +00:00
|
|
|
|
2019-04-25 17:29:21 +00: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-30 03:01:01 +00:00
|
|
|
|
2019-04-25 17:29:21 +00: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-30 03:01:01 +00:00
|
|
|
|
2019-04-25 17:29:21 +00: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");
|
|
|
|
}
|
2018-11-30 03:01:01 +00:00
|
|
|
}
|