diff options
Diffstat (limited to 'cli/http_util.rs')
-rw-r--r-- | cli/http_util.rs | 65 |
1 files changed, 23 insertions, 42 deletions
diff --git a/cli/http_util.rs b/cli/http_util.rs index f6f8095a0..cedaa1d0e 100644 --- a/cli/http_util.rs +++ b/cli/http_util.rs @@ -14,8 +14,6 @@ use deno_runtime::deno_fetch::reqwest::redirect::Policy; use deno_runtime::deno_fetch::reqwest::Client; use deno_runtime::deno_fetch::reqwest::StatusCode; use std::collections::HashMap; -use std::fs::File; -use std::io::Read; pub fn get_user_agent() -> String { format!("Deno/{}", version::deno()) @@ -25,7 +23,7 @@ pub fn get_user_agent() -> String { /// proxies and doesn't follow redirects. pub fn create_http_client( user_agent: String, - ca_file: Option<&str>, + ca_data: Option<&str>, ) -> Result<Client, AnyError> { let mut headers = HeaderMap::new(); headers.insert(USER_AGENT, user_agent.parse().unwrap()); @@ -34,10 +32,9 @@ pub fn create_http_client( .default_headers(headers) .use_rustls_tls(); - if let Some(ca_file) = ca_file { - let mut buf = Vec::new(); - File::open(ca_file)?.read_to_end(&mut buf)?; - let cert = reqwest::Certificate::from_pem(&buf)?; + if let Some(ca_data) = ca_data { + let ca_data_vec = ca_data.as_bytes().to_vec(); + let cert = reqwest::Certificate::from_pem(&ca_data_vec)?; builder = builder.add_root_certificate(cert); } @@ -159,9 +156,10 @@ pub async fn fetch_once( #[cfg(test)] mod tests { use super::*; + use std::fs::read_to_string; - fn create_test_client(ca_file: Option<&str>) -> Client { - create_http_client("test_client".to_string(), ca_file).unwrap() + fn create_test_client(ca_data: Option<&str>) -> Client { + create_http_client("test_client".to_string(), ca_data).unwrap() } #[tokio::test] @@ -314,17 +312,12 @@ mod tests { // Relies on external http server. See target/debug/test_server let url = Url::parse("https://localhost:5545/cli/tests/fixture.json").unwrap(); - - let client = create_http_client( - get_user_agent(), - Some( - test_util::root_path() - .join("std/http/testdata/tls/RootCA.pem") - .to_str() - .unwrap(), - ), + let ca_data: String = read_to_string( + test_util::root_path().join("std/http/testdata/tls/RootCA.pem"), ) .unwrap(); + let client = + create_http_client(get_user_agent(), Some(ca_data.as_str())).unwrap(); let result = fetch_once(client, &url, None).await; if let Ok(FetchOnceResult::Code(body, headers)) = result { assert!(!body.is_empty()); @@ -344,16 +337,12 @@ mod tests { "https://localhost:5545/cli/tests/053_import_compression/gziped", ) .unwrap(); - let client = create_http_client( - get_user_agent(), - Some( - test_util::root_path() - .join("std/http/testdata/tls/RootCA.pem") - .to_str() - .unwrap(), - ), + let ca_data: String = read_to_string( + test_util::root_path().join("std/http/testdata/tls/RootCA.pem"), ) .unwrap(); + let client = + create_http_client(get_user_agent(), Some(ca_data.as_str())).unwrap(); let result = fetch_once(client, &url, None).await; if let Ok(FetchOnceResult::Code(body, headers)) = result { assert_eq!(String::from_utf8(body).unwrap(), "console.log('gzip')"); @@ -372,16 +361,12 @@ mod tests { async fn test_fetch_with_cafile_with_etag() { let _http_server_guard = test_util::http_server(); let url = Url::parse("https://localhost:5545/etag_script.ts").unwrap(); - let client = create_http_client( - get_user_agent(), - Some( - test_util::root_path() - .join("std/http/testdata/tls/RootCA.pem") - .to_str() - .unwrap(), - ), + let ca_data: String = read_to_string( + test_util::root_path().join("std/http/testdata/tls/RootCA.pem"), ) .unwrap(); + let client = + create_http_client(get_user_agent(), Some(ca_data.as_str())).unwrap(); let result = fetch_once(client.clone(), &url, None).await; if let Ok(FetchOnceResult::Code(body, headers)) = result { assert!(!body.is_empty()); @@ -409,16 +394,12 @@ mod tests { "https://localhost:5545/cli/tests/053_import_compression/brotli", ) .unwrap(); - let client = create_http_client( - get_user_agent(), - Some( - test_util::root_path() - .join("std/http/testdata/tls/RootCA.pem") - .to_str() - .unwrap(), - ), + let ca_data: String = read_to_string( + test_util::root_path().join("std/http/testdata/tls/RootCA.pem"), ) .unwrap(); + let client = + create_http_client(get_user_agent(), Some(ca_data.as_str())).unwrap(); let result = fetch_once(client, &url, None).await; if let Ok(FetchOnceResult::Code(body, headers)) = result { assert!(!body.is_empty()); |