Use tokio::test for some of cli's unit tests (#3868)

This commit is contained in:
Ryan Dahl 2020-02-03 08:53:50 -05:00 committed by GitHub
parent 55063dd8e8
commit fba40d86c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 495 additions and 594 deletions

File diff suppressed because it is too large Load diff

View file

@ -269,37 +269,28 @@ impl AsyncRead for HttpBody {
#[cfg(test)]
mod tests {
use super::*;
use crate::tokio_util;
#[test]
fn test_fetch_sync_string() {
#[tokio::test]
async fn test_fetch_sync_string() {
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/fixture.json").unwrap();
let client = create_http_client();
let fut =
fetch_string_once(client, &url, None).map(|result| match result {
Ok(FetchOnceResult::Code(ResultPayload {
body: code,
content_type: maybe_content_type,
etag,
x_typescript_types,
})) => {
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);
let result = fetch_string_once(client, &url, None).await;
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!();
}
drop(http_server_guard);
}
#[test]
fn test_fetch_gzip() {
#[tokio::test]
async fn test_fetch_gzip() {
let http_server_guard = crate::test_util::http_server();
// Relies on external http server. See tools/http_server.py
let url = Url::parse(
@ -307,72 +298,53 @@ mod tests {
)
.unwrap();
let client = create_http_client();
let fut =
fetch_string_once(client, &url, None).map(|result| match result {
Ok(FetchOnceResult::Code(ResultPayload {
body: code,
content_type: maybe_content_type,
etag,
x_typescript_types,
})) => {
assert!(!code.is_empty());
assert_eq!(code, "console.log('gzip')");
assert_eq!(
maybe_content_type,
Some("application/javascript".to_string())
);
assert_eq!(etag, None);
assert_eq!(x_typescript_types, None);
}
_ => panic!(),
});
tokio_util::run(fut);
let result = fetch_string_once(client, &url, None).await;
if let Ok(FetchOnceResult::Code(payload)) = result {
assert_eq!(payload.body, "console.log('gzip')");
assert_eq!(
payload.content_type,
Some("application/javascript".to_string())
);
assert_eq!(payload.etag, None);
assert_eq!(payload.x_typescript_types, None);
} else {
panic!();
}
drop(http_server_guard);
}
#[test]
fn test_fetch_with_etag() {
#[tokio::test]
async fn test_fetch_with_etag() {
let http_server_guard = crate::test_util::http_server();
let url = Url::parse("http://127.0.0.1:4545/etag_script.ts").unwrap();
let client = create_http_client();
let fut = async move {
fetch_string_once(client.clone(), &url, None)
.map(|result| match result {
Ok(FetchOnceResult::Code(ResultPayload {
body: code,
content_type: maybe_content_type,
etag,
x_typescript_types,
})) => {
assert!(!code.is_empty());
assert_eq!(code, "console.log('etag')");
assert_eq!(
maybe_content_type,
Some("application/typescript".to_string())
);
assert_eq!(etag, Some("33a64df551425fcc55e".to_string()));
assert_eq!(x_typescript_types, None);
}
_ => panic!(),
})
let result = fetch_string_once(client.clone(), &url, None).await;
if let Ok(FetchOnceResult::Code(ResultPayload {
body,
content_type,
etag,
x_typescript_types,
})) = result
{
assert!(!body.is_empty());
assert_eq!(body, "console.log('etag')");
assert_eq!(content_type, Some("application/typescript".to_string()));
assert_eq!(etag, Some("33a64df551425fcc55e".to_string()));
assert_eq!(x_typescript_types, None);
} else {
panic!();
}
let res =
fetch_string_once(client, &url, Some("33a64df551425fcc55e".to_string()))
.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);
}
#[test]
fn test_fetch_brotli() {
#[tokio::test]
async fn test_fetch_brotli() {
let http_server_guard = crate::test_util::http_server();
// Relies on external http server. See tools/http_server.py
let url = Url::parse(
@ -380,32 +352,24 @@ mod tests {
)
.unwrap();
let client = create_http_client();
let fut =
fetch_string_once(client, &url, None).map(|result| match result {
Ok(FetchOnceResult::Code(ResultPayload {
body: code,
content_type: maybe_content_type,
etag,
x_typescript_types,
})) => {
assert!(!code.is_empty());
assert_eq!(code, "console.log('brotli');");
assert_eq!(
maybe_content_type,
Some("application/javascript".to_string())
);
assert_eq!(etag, None);
assert_eq!(x_typescript_types, None);
}
_ => panic!(),
});
tokio_util::run(fut);
let result = fetch_string_once(client, &url, None).await;
if let Ok(FetchOnceResult::Code(payload)) = result {
assert!(!payload.body.is_empty());
assert_eq!(payload.body, "console.log('brotli');");
assert_eq!(
payload.content_type,
Some("application/javascript".to_string())
);
assert_eq!(payload.etag, None);
assert_eq!(payload.x_typescript_types, None);
} else {
panic!();
}
drop(http_server_guard);
}
#[test]
fn test_fetch_string_once_with_redirect() {
#[tokio::test]
async fn test_fetch_string_once_with_redirect() {
let http_server_guard = crate::test_util::http_server();
// Relies on external http server. See tools/http_server.py
let url =
@ -414,15 +378,12 @@ mod tests {
let target_url =
Url::parse("http://localhost:4545/cli/tests/fixture.json").unwrap();
let client = create_http_client();
let fut =
fetch_string_once(client, &url, None).map(move |result| match result {
Ok(FetchOnceResult::Redirect(url)) => {
assert_eq!(url, target_url);
}
_ => panic!(),
});
tokio_util::run(fut);
let result = fetch_string_once(client, &url, None).await;
if let Ok(FetchOnceResult::Redirect(url)) = result {
assert_eq!(url, target_url);
} else {
panic!();
}
drop(http_server_guard);
}