diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2020-02-03 08:53:50 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-03 08:53:50 -0500 |
commit | fba40d86c4cbb8ff406175820ea9a2d9eb2b40cd (patch) | |
tree | fccf706ad076d4b61051670c11249ccc9f5a87de /cli/http_util.rs | |
parent | 55063dd8e8e3ae52eb90bdf42e36d979dcbb5010 (diff) |
Use tokio::test for some of cli's unit tests (#3868)
Diffstat (limited to 'cli/http_util.rs')
-rw-r--r-- | cli/http_util.rs | 179 |
1 files changed, 70 insertions, 109 deletions
diff --git a/cli/http_util.rs b/cli/http_util.rs index 466bec3f2..bada2df71 100644 --- a/cli/http_util.rs +++ b/cli/http_util.rs @@ -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!(), - }) - .await; + 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); } |