fetch_cached_remote_source support redirect URL without base (#4099)

This commit is contained in:
Kevin (Kun) "Kassimo" Qian 2020-02-28 06:04:18 -08:00 committed by GitHub
parent 9b6a9b769e
commit b5bf28e68f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 2 deletions

View file

@ -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);
}
@ -1089,6 +1099,55 @@ mod tests {
drop(http_server_guard);
}
#[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();