mirror of
https://github.com/denoland/deno.git
synced 2024-11-25 15:29:32 -05:00
Use tokio::test for some of cli's unit tests (#3868)
This commit is contained in:
parent
55063dd8e8
commit
fba40d86c4
2 changed files with 495 additions and 594 deletions
File diff suppressed because it is too large
Load diff
179
cli/http_util.rs
179
cli/http_util.rs
|
@ -269,37 +269,28 @@ impl AsyncRead for HttpBody {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::tokio_util;
|
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_fetch_sync_string() {
|
async fn test_fetch_sync_string() {
|
||||||
let http_server_guard = crate::test_util::http_server();
|
let http_server_guard = crate::test_util::http_server();
|
||||||
// Relies on external http server. See tools/http_server.py
|
// Relies on external http server. See tools/http_server.py
|
||||||
let url =
|
let url =
|
||||||
Url::parse("http://127.0.0.1:4545/cli/tests/fixture.json").unwrap();
|
Url::parse("http://127.0.0.1:4545/cli/tests/fixture.json").unwrap();
|
||||||
let client = create_http_client();
|
let client = create_http_client();
|
||||||
let fut =
|
let result = fetch_string_once(client, &url, None).await;
|
||||||
fetch_string_once(client, &url, None).map(|result| match result {
|
if let Ok(FetchOnceResult::Code(payload)) = result {
|
||||||
Ok(FetchOnceResult::Code(ResultPayload {
|
assert!(!payload.body.is_empty());
|
||||||
body: code,
|
assert_eq!(payload.content_type, Some("application/json".to_string()));
|
||||||
content_type: maybe_content_type,
|
assert_eq!(payload.etag, None);
|
||||||
etag,
|
assert_eq!(payload.x_typescript_types, None);
|
||||||
x_typescript_types,
|
} else {
|
||||||
})) => {
|
panic!();
|
||||||
assert!(!code.is_empty());
|
}
|
||||||
assert_eq!(maybe_content_type, Some("application/json".to_string()));
|
|
||||||
assert_eq!(etag, None);
|
|
||||||
assert_eq!(x_typescript_types, None);
|
|
||||||
}
|
|
||||||
_ => panic!(),
|
|
||||||
});
|
|
||||||
|
|
||||||
tokio_util::run(fut);
|
|
||||||
drop(http_server_guard);
|
drop(http_server_guard);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_fetch_gzip() {
|
async fn test_fetch_gzip() {
|
||||||
let http_server_guard = crate::test_util::http_server();
|
let http_server_guard = crate::test_util::http_server();
|
||||||
// Relies on external http server. See tools/http_server.py
|
// Relies on external http server. See tools/http_server.py
|
||||||
let url = Url::parse(
|
let url = Url::parse(
|
||||||
|
@ -307,72 +298,53 @@ mod tests {
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let client = create_http_client();
|
let client = create_http_client();
|
||||||
let fut =
|
let result = fetch_string_once(client, &url, None).await;
|
||||||
fetch_string_once(client, &url, None).map(|result| match result {
|
if let Ok(FetchOnceResult::Code(payload)) = result {
|
||||||
Ok(FetchOnceResult::Code(ResultPayload {
|
assert_eq!(payload.body, "console.log('gzip')");
|
||||||
body: code,
|
assert_eq!(
|
||||||
content_type: maybe_content_type,
|
payload.content_type,
|
||||||
etag,
|
Some("application/javascript".to_string())
|
||||||
x_typescript_types,
|
);
|
||||||
})) => {
|
assert_eq!(payload.etag, None);
|
||||||
assert!(!code.is_empty());
|
assert_eq!(payload.x_typescript_types, None);
|
||||||
assert_eq!(code, "console.log('gzip')");
|
} else {
|
||||||
assert_eq!(
|
panic!();
|
||||||
maybe_content_type,
|
}
|
||||||
Some("application/javascript".to_string())
|
|
||||||
);
|
|
||||||
assert_eq!(etag, None);
|
|
||||||
assert_eq!(x_typescript_types, None);
|
|
||||||
}
|
|
||||||
_ => panic!(),
|
|
||||||
});
|
|
||||||
|
|
||||||
tokio_util::run(fut);
|
|
||||||
drop(http_server_guard);
|
drop(http_server_guard);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_fetch_with_etag() {
|
async fn test_fetch_with_etag() {
|
||||||
let http_server_guard = crate::test_util::http_server();
|
let http_server_guard = crate::test_util::http_server();
|
||||||
let url = Url::parse("http://127.0.0.1:4545/etag_script.ts").unwrap();
|
let url = Url::parse("http://127.0.0.1:4545/etag_script.ts").unwrap();
|
||||||
let client = create_http_client();
|
let client = create_http_client();
|
||||||
let fut = async move {
|
let result = fetch_string_once(client.clone(), &url, None).await;
|
||||||
fetch_string_once(client.clone(), &url, None)
|
if let Ok(FetchOnceResult::Code(ResultPayload {
|
||||||
.map(|result| match result {
|
body,
|
||||||
Ok(FetchOnceResult::Code(ResultPayload {
|
content_type,
|
||||||
body: code,
|
etag,
|
||||||
content_type: maybe_content_type,
|
x_typescript_types,
|
||||||
etag,
|
})) = result
|
||||||
x_typescript_types,
|
{
|
||||||
})) => {
|
assert!(!body.is_empty());
|
||||||
assert!(!code.is_empty());
|
assert_eq!(body, "console.log('etag')");
|
||||||
assert_eq!(code, "console.log('etag')");
|
assert_eq!(content_type, Some("application/typescript".to_string()));
|
||||||
assert_eq!(
|
assert_eq!(etag, Some("33a64df551425fcc55e".to_string()));
|
||||||
maybe_content_type,
|
assert_eq!(x_typescript_types, None);
|
||||||
Some("application/typescript".to_string())
|
} else {
|
||||||
);
|
panic!();
|
||||||
assert_eq!(etag, Some("33a64df551425fcc55e".to_string()));
|
}
|
||||||
assert_eq!(x_typescript_types, None);
|
|
||||||
}
|
let res =
|
||||||
_ => panic!(),
|
fetch_string_once(client, &url, Some("33a64df551425fcc55e".to_string()))
|
||||||
})
|
|
||||||
.await;
|
.await;
|
||||||
|
assert_eq!(res.unwrap(), FetchOnceResult::NotModified);
|
||||||
|
|
||||||
let res = fetch_string_once(
|
|
||||||
client,
|
|
||||||
&url,
|
|
||||||
Some("33a64df551425fcc55e".to_string()),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
assert_eq!(res.unwrap(), FetchOnceResult::NotModified);
|
|
||||||
};
|
|
||||||
|
|
||||||
tokio_util::run(fut);
|
|
||||||
drop(http_server_guard);
|
drop(http_server_guard);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_fetch_brotli() {
|
async fn test_fetch_brotli() {
|
||||||
let http_server_guard = crate::test_util::http_server();
|
let http_server_guard = crate::test_util::http_server();
|
||||||
// Relies on external http server. See tools/http_server.py
|
// Relies on external http server. See tools/http_server.py
|
||||||
let url = Url::parse(
|
let url = Url::parse(
|
||||||
|
@ -380,32 +352,24 @@ mod tests {
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let client = create_http_client();
|
let client = create_http_client();
|
||||||
let fut =
|
let result = fetch_string_once(client, &url, None).await;
|
||||||
fetch_string_once(client, &url, None).map(|result| match result {
|
if let Ok(FetchOnceResult::Code(payload)) = result {
|
||||||
Ok(FetchOnceResult::Code(ResultPayload {
|
assert!(!payload.body.is_empty());
|
||||||
body: code,
|
assert_eq!(payload.body, "console.log('brotli');");
|
||||||
content_type: maybe_content_type,
|
assert_eq!(
|
||||||
etag,
|
payload.content_type,
|
||||||
x_typescript_types,
|
Some("application/javascript".to_string())
|
||||||
})) => {
|
);
|
||||||
assert!(!code.is_empty());
|
assert_eq!(payload.etag, None);
|
||||||
assert_eq!(code, "console.log('brotli');");
|
assert_eq!(payload.x_typescript_types, None);
|
||||||
assert_eq!(
|
} else {
|
||||||
maybe_content_type,
|
panic!();
|
||||||
Some("application/javascript".to_string())
|
}
|
||||||
);
|
|
||||||
assert_eq!(etag, None);
|
|
||||||
assert_eq!(x_typescript_types, None);
|
|
||||||
}
|
|
||||||
_ => panic!(),
|
|
||||||
});
|
|
||||||
|
|
||||||
tokio_util::run(fut);
|
|
||||||
drop(http_server_guard);
|
drop(http_server_guard);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_fetch_string_once_with_redirect() {
|
async fn test_fetch_string_once_with_redirect() {
|
||||||
let http_server_guard = crate::test_util::http_server();
|
let http_server_guard = crate::test_util::http_server();
|
||||||
// Relies on external http server. See tools/http_server.py
|
// Relies on external http server. See tools/http_server.py
|
||||||
let url =
|
let url =
|
||||||
|
@ -414,15 +378,12 @@ mod tests {
|
||||||
let target_url =
|
let target_url =
|
||||||
Url::parse("http://localhost:4545/cli/tests/fixture.json").unwrap();
|
Url::parse("http://localhost:4545/cli/tests/fixture.json").unwrap();
|
||||||
let client = create_http_client();
|
let client = create_http_client();
|
||||||
let fut =
|
let result = fetch_string_once(client, &url, None).await;
|
||||||
fetch_string_once(client, &url, None).map(move |result| match result {
|
if let Ok(FetchOnceResult::Redirect(url)) = result {
|
||||||
Ok(FetchOnceResult::Redirect(url)) => {
|
assert_eq!(url, target_url);
|
||||||
assert_eq!(url, target_url);
|
} else {
|
||||||
}
|
panic!();
|
||||||
_ => panic!(),
|
}
|
||||||
});
|
|
||||||
|
|
||||||
tokio_util::run(fut);
|
|
||||||
drop(http_server_guard);
|
drop(http_server_guard);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue