diff options
Diffstat (limited to 'test_util/src/lib.rs')
-rw-r--r-- | test_util/src/lib.rs | 48 |
1 files changed, 47 insertions, 1 deletions
diff --git a/test_util/src/lib.rs b/test_util/src/lib.rs index 1e80c0d1d..af4c814c4 100644 --- a/test_util/src/lib.rs +++ b/test_util/src/lib.rs @@ -14,6 +14,7 @@ use hyper::Request; use hyper::Response; use hyper::StatusCode; use lazy_static::lazy_static; +use npm::custom_npm_cache; use os_pipe::pipe; use pretty_assertions::assert_eq; use regex::Regex; @@ -51,6 +52,7 @@ use tokio_tungstenite::accept_async; pub mod assertions; pub mod lsp; +mod npm; pub mod pty; mod temp_dir; @@ -953,7 +955,22 @@ async fn main_server( } // serve npm registry files - if req.uri().path().starts_with("/npm/registry/") { + if let Some(suffix) = + req.uri().path().strip_prefix("/npm/registry/@denotest/") + { + // serve all requests to /npm/registry/@deno using the file system + // at that path + match handle_custom_npm_registry_path(suffix) { + Ok(Some(response)) => return Ok(response), + Ok(None) => {} // ignore, not found + Err(err) => { + return Response::builder() + .status(StatusCode::INTERNAL_SERVER_ERROR) + .body(format!("{:#}", err).into()); + } + } + } else if req.uri().path().starts_with("/npm/registry/") { + // otherwise, serve based on registry.json and tgz files let is_tarball = req.uri().path().ends_with(".tgz"); if !is_tarball { file_path.push("registry.json"); @@ -985,6 +1002,33 @@ async fn main_server( }; } +fn handle_custom_npm_registry_path( + path: &str, +) -> Result<Option<Response<Body>>, anyhow::Error> { + let parts = path + .split('/') + .filter(|p| !p.is_empty()) + .collect::<Vec<_>>(); + let cache = custom_npm_cache()?; + let package_name = format!("@denotest/{}", parts[0]); + if parts.len() == 2 { + if let Some(file_bytes) = + cache.tarball_bytes(&package_name, parts[1].trim_end_matches(".tgz")) + { + let file_resp = custom_headers("file.tgz", file_bytes.to_owned()); + return Ok(Some(file_resp)); + } + } else if parts.len() == 1 { + if let Some(registry_file) = cache.registry_file(&package_name) { + let file_resp = + custom_headers("registry.json", registry_file.as_bytes().to_vec()); + return Ok(Some(file_resp)); + } + } + + Ok(None) +} + fn should_download_npm_packages() -> bool { // when this env var is set, it will download and save npm packages // to the testdata/npm/registry directory @@ -1489,6 +1533,8 @@ fn custom_headers(p: &str, body: Vec<u8>) -> Response<Body> { Some("application/json") } else if p.ends_with(".wasm") { Some("application/wasm") + } else if p.ends_with(".tgz") { + Some("application/gzip") } else { None }; |