Skip to content

Commit

Permalink
module: add application/json in accept header when fetching json module
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-ippolito committed Oct 10, 2023
1 parent 20b996d commit af0919f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/internal/modules/esm/fetch_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ function fetchWithRedirects(parsed) {
}
const handler = parsed.protocol === 'http:' ? HTTPGet : HTTPSGet;
const result = (async () => {
const extension = parsed.pathname.split('.').pop();
const req = handler(parsed, {
headers: { Accept: '*/*' },
headers: { Accept: extension === 'json' ? 'application/json,*/*;' : '*/*;' },
});
// Note that `once` is used here to handle `error` and that it hits the
// `finally` on network error/timeout.
Expand Down
7 changes: 7 additions & 0 deletions test/es-module/test-http-imports.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ for (const { protocol, createServer } of [
// ?body sets the body, string
const server = createServer(function(_req, res) {
const url = new URL(_req.url, host);

if (url.pathname.includes('json')) {
assert.strictEqual(_req.headers.accept, 'application/json,*/*;');
}

const redirect = url.searchParams.get('redirect');
if (url.pathname === '/not-found') {
res.writeHead(404);
Expand Down Expand Up @@ -203,6 +208,8 @@ for (const { protocol, createServer } of [
import(notFound.href),
{ code: 'ERR_MODULE_NOT_FOUND' },
);
// Import file with json extension
await import(url.href + 'empty.json');

server.close();
}
Expand Down

0 comments on commit af0919f

Please sign in to comment.