mirror of
https://github.com/lexmount/moli.git
synced 2026-10-02 00:00:49 +00:00
fix(fetch): reuse MIME extraction for body Blob results
This commit is contained in:
@@ -6,7 +6,7 @@ use super::super::fetch_surface::{
|
||||
};
|
||||
use super::*;
|
||||
use moli_web_mime::{
|
||||
is_form_urlencoded_mime, multipart_form_data_boundary, response_blob_mime_type,
|
||||
extract_response_mime_type, is_form_urlencoded_mime, multipart_form_data_boundary,
|
||||
response_content_type,
|
||||
};
|
||||
|
||||
@@ -198,7 +198,8 @@ fn response_blob_mime_type_from_object<'s>(
|
||||
) -> String {
|
||||
body_headers(scope, consumption.object, consumption.receiver)
|
||||
.as_deref()
|
||||
.map(response_blob_mime_type)
|
||||
.and_then(extract_response_mime_type)
|
||||
.map(|mime| mime.to_string())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
|
||||
@@ -2160,3 +2160,5 @@ mod history_replace_forward;
|
||||
|
||||
mod extracted;
|
||||
mod navigation_timing_inheritance;
|
||||
|
||||
mod response_blob_mime;
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn body_blob_mime_uses_the_shared_header_list_parser() {
|
||||
let mut vm = new_storage_test_vm("https://body-blob-mime.test/");
|
||||
vm.eval(r#"
|
||||
globalThis.blobMimeResult = null;
|
||||
(async () => {
|
||||
const cases = [
|
||||
[[], ''],
|
||||
[['', 'text/plain'], 'text/plain'],
|
||||
[['text/plain', ''], 'text/plain'],
|
||||
[['text/plain', 'text/html'], 'text/html'],
|
||||
[['TEXT/PLAIN;Charset=GBK', 'text/plain'], 'text/plain;charset=GBK'],
|
||||
[['text/html', '*/*'], 'text/html'],
|
||||
[['text/html; x="A,b"'], 'text/html;x="A,b"'],
|
||||
[['invalid'], ''],
|
||||
[['*/*'], '']
|
||||
];
|
||||
for (const kind of ['Request', 'Response']) {
|
||||
for (const [values, expected] of cases) {
|
||||
const headers = values.map(value => ['Content-Type', value]);
|
||||
const bytes = new Uint8Array([1, 2, 3]);
|
||||
const body = kind === 'Request'
|
||||
? new Request('https://body-blob-mime.test/', {method:'POST', body:bytes, headers})
|
||||
: new Response(bytes, {headers});
|
||||
const before = body.headers.get('Content-Type');
|
||||
const blob = await body.blob();
|
||||
if (blob.type !== expected || blob.size !== 3 || body.headers.get('Content-Type') !== before)
|
||||
throw new Error(kind + ' ' + JSON.stringify(values) + ': ' + blob.type);
|
||||
}
|
||||
}
|
||||
return 'passed';
|
||||
})().then(value => blobMimeResult = value, error => blobMimeResult = String(error));
|
||||
"#).unwrap();
|
||||
for _ in 0..64 {
|
||||
if vm.eval("String(blobMimeResult !== null)").unwrap() == "true" {
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert_eq!(vm.eval("blobMimeResult").unwrap(), "passed");
|
||||
}
|
||||
@@ -556,7 +556,7 @@ fn response_body_consumers_use_shared_content_type_helpers() {
|
||||
|
||||
assert_eq!(
|
||||
result,
|
||||
r#"["a,1|b,two","value|true|blob|application/json|{\"ok\":true}","application/json","text/plain; charset=utf-8","text/plain, application/json"]"#
|
||||
r#"["a,1|b,two","value|true|blob|application/json|{\"ok\":true}","application/json","text/plain;charset=UTF-8","application/json"]"#
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::classification::is_binary_document_mime_type;
|
||||
use crate::parse::{mime_essence, normalize_web_api_mime_type};
|
||||
use crate::parse::mime_essence;
|
||||
|
||||
pub fn response_header_value(headers: &[(String, Vec<u8>)], name: &str) -> Option<String> {
|
||||
response_header_values(headers, name).into_iter().next()
|
||||
@@ -122,16 +122,6 @@ pub fn effective_response_mime_essence(
|
||||
.and_then(mime_essence)
|
||||
}
|
||||
|
||||
pub fn response_blob_mime_type(headers: &[(String, Vec<u8>)]) -> String {
|
||||
normalize_response_blob_mime_type(response_content_type(headers).as_deref())
|
||||
}
|
||||
|
||||
pub fn normalize_response_blob_mime_type(content_type: Option<&str>) -> String {
|
||||
content_type
|
||||
.map(normalize_web_api_mime_type)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn parsed_header_name(name: &str) -> Option<http::HeaderName> {
|
||||
http::HeaderName::from_bytes(name.as_bytes()).ok()
|
||||
}
|
||||
|
||||
@@ -24,9 +24,8 @@ pub use data_url::{
|
||||
pub use destination::FetchDestination;
|
||||
pub use headers::{
|
||||
effective_response_mime_essence, effective_response_mime_type, extract_response_mime_essence,
|
||||
extract_response_mime_type, normalize_response_blob_mime_type, response_blob_mime_type,
|
||||
response_content_type, response_document_content_type, response_header_value,
|
||||
response_header_values, response_headers_indicate_attachment_download,
|
||||
extract_response_mime_type, response_content_type, response_document_content_type,
|
||||
response_header_value, response_header_values, response_headers_indicate_attachment_download,
|
||||
response_headers_indicate_binary_document, response_headers_indicate_raw_document,
|
||||
};
|
||||
pub use media::{MediaMimeSupport, is_media_source_type_supported, media_mime_support};
|
||||
|
||||
@@ -532,14 +532,6 @@ fn derives_effective_response_mime_for_body_consumers() {
|
||||
effective_response_mime_essence(&headers, Some("Application/XHTML+XML")),
|
||||
Some("application/xhtml+xml".to_owned())
|
||||
);
|
||||
assert_eq!(
|
||||
response_blob_mime_type(&headers),
|
||||
"text/html; charset=utf-8"
|
||||
);
|
||||
|
||||
let invalid: Vec<(String, Vec<u8>)> =
|
||||
vec![("Content-Type".to_owned(), b"text/plain\n".to_vec())];
|
||||
assert_eq!(response_blob_mime_type(&invalid), "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user