summaryrefslogtreecommitdiff
path: root/cli/file_fetcher.rs
diff options
context:
space:
mode:
authorKevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com>2020-02-28 06:04:18 -0800
committerGitHub <noreply@github.com>2020-02-28 09:04:18 -0500
commitb5bf28e68f38396defd50be4ac72ae19a6bf5eb5 (patch)
tree1cd5832fd23c6139c2a10c931ccd1fc5aec86d91 /cli/file_fetcher.rs
parent9b6a9b769ed6df9d24dc0c5173cf2590a4bdcb47 (diff)
fetch_cached_remote_source support redirect URL without base (#4099)
Diffstat (limited to 'cli/file_fetcher.rs')
-rw-r--r--cli/file_fetcher.rs61
1 files changed, 60 insertions, 1 deletions
diff --git a/cli/file_fetcher.rs b/cli/file_fetcher.rs
index e9309c7e4..2d9f3c104 100644
--- a/cli/file_fetcher.rs
+++ b/cli/file_fetcher.rs
@@ -312,7 +312,17 @@ impl SourceFileFetcher {
let (mut source_file, headers) = result;
if let Some(redirect_to) = headers.get("location") {
- let redirect_url = Url::parse(redirect_to).expect("Should be valid URL");
+ let redirect_url = match Url::parse(redirect_to) {
+ Ok(redirect_url) => redirect_url,
+ Err(url::ParseError::RelativeUrlWithoutBase) => {
+ let mut url = module_url.clone();
+ url.set_path(redirect_to);
+ url
+ }
+ Err(e) => {
+ return Err(e.into());
+ }
+ };
return self.fetch_cached_remote_source(&redirect_url);
}
@@ -1090,6 +1100,55 @@ mod tests {
}
#[tokio::test]
+ async fn test_get_source_code_7() {
+ let http_server_guard = crate::test_util::http_server();
+ let (_temp_dir, fetcher) = test_setup();
+
+ // Testing redirect with Location set to absolute url.
+ let redirect_module_url = Url::parse(
+ "http://localhost:4550/REDIRECT/cli/tests/subdir/redirects/redirect1.js",
+ )
+ .unwrap();
+ let redirect_source_filepath =
+ fetcher.http_cache.get_cache_filename(&redirect_module_url);
+ let redirect_source_filename =
+ redirect_source_filepath.to_str().unwrap().to_string();
+ let target_module_url = Url::parse(
+ "http://localhost:4550/cli/tests/subdir/redirects/redirect1.js",
+ )
+ .unwrap();
+ let redirect_target_filepath =
+ fetcher.http_cache.get_cache_filename(&target_module_url);
+ let redirect_target_filename =
+ redirect_target_filepath.to_str().unwrap().to_string();
+
+ // Test basic follow and headers recording
+ let result = fetcher
+ .get_source_file(&redirect_module_url, true, false, false)
+ .await;
+ assert!(result.is_ok());
+ let mod_meta = result.unwrap();
+ // File that requires redirection should be empty file.
+ assert_eq!(fs::read_to_string(&redirect_source_filename).unwrap(), "");
+ let (_, headers) = fetcher.http_cache.get(&redirect_module_url).unwrap();
+ assert_eq!(
+ headers.get("location").unwrap(),
+ "/cli/tests/subdir/redirects/redirect1.js"
+ );
+ // The target of redirection is downloaded instead.
+ assert_eq!(
+ fs::read_to_string(&redirect_target_filename).unwrap(),
+ "export const redirect = 1;\n"
+ );
+ let (_, headers) = fetcher.http_cache.get(&target_module_url).unwrap();
+ assert!(headers.get("location").is_none());
+ // Examine the meta result.
+ assert_eq!(mod_meta.url, target_module_url);
+
+ drop(http_server_guard);
+ }
+
+ #[tokio::test]
async fn test_get_source_no_remote() {
let http_server_guard = crate::test_util::http_server();
let (_temp_dir, fetcher) = test_setup();