[whatwg/fetch] Clarify priority between blob.type and Content-Type in headers for request/response.blob() (Issue #1630)

Currently, the spec of [`blob()`](https://fetch.spec.whatwg.org/#dom-body-blob) says

> return a [Blob](https://w3c.github.io/FileAPI/#dfn-Blob) whose contents are bytes and whose [type](https://w3c.github.io/FileAPI/#dfn-type) attribute is [this](https://webidl.spec.whatwg.org/#this)’s [MIME type](https://fetch.spec.whatwg.org/#concept-body-mime-type).

however, it doesn't mention when the body is a Blob with type, browsers should respect the blob's type or the Content-Type in headers.

there's a test in [Chromium](https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/web_tests/http/tests/fetch/script-tests/request.js;l=1021-1032;drc=05c5ed4e58b4cc7d50d0018f7b4d2cdc17211389) and [WebKit](https://github.com/WebKit/WebKit/blob/512db9a6900b400f9ce93e90e6e178fa74fef03e/LayoutTests/http/wpt/fetch/fetch-as-blob.js#L62-L73).

```js
 promise_test(function(t) { 
     var req = new Request('http://localhost/', 
                           {method: 'POST', 
                            body: new Blob([''], {type: 'Text/Plain'})}); 
     req.headers.set('Content-Type', 'Text/Html'); 
     return req.blob() 
       .then(function(blob) { 
           assert_equals(blob.type, 'text/plain'); 
           assert_equals(req.headers.get('Content-Type'), 'Text/Html'); 
         }); 
   }, 
   'MIME type unchanged if headers are modified after Request() constructor'); 
```

Chromium and WebKit are respecting blob's type so they could pass the test now. For Gecko, it fails to run the test because it would respect the headers' content-type.

With confirming with @annevk in https://github.com/WebKit/WebKit/pull/12376#issuecomment-1503175231, it seems the `blob()` should always respect the header's content-type as "this’s MIME type" wins. So, I wonder maybe we would need some notes (or algorithms in the `blob()`) to clarify more about blob's type when it exists in the spec?

Finally, if we should respect headers' Content-Type, I will fix the test to

```diff
-           assert_equals(blob.type, 'text/plain'); 
+           assert_equals(blob.type, 'text/html'); 
```

(As this test exists in Chromium and WebKit separately, I will try to make a WPT PR and help to remove the related one in Chromium and WebKit :pray:)


-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/fetch/issues/1630
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/fetch/issues/1630@github.com>

Received on Wednesday, 12 April 2023 08:02:13 UTC