Skip to content

Commit

Permalink
Allow overriding the default accept headers (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
sholladay authored and sindresorhus committed Nov 9, 2019
1 parent a1c000e commit 65f38f9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class Ky {

for (const [type, mimeType] of Object.entries(responseTypes)) {
result[type] = async () => {
this.request.headers.set('accept', mimeType);
this.request.headers.set('accept', this.request.headers.get('accept') || mimeType);
const response = (await result).clone();
return (type === 'json' && response.status === 204) ? '' : response[type]();
};
Expand Down
18 changes: 18 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,24 @@ test('JSON with custom Headers instance', async t => {
await server.close();
});

test('.json() with custom accept header', async t => {
t.plan(2);

const server = await createTestServer();
server.get('/', async (request, response) => {
t.is(request.headers.accept, 'foo/bar');
response.json({});
});

const responseJson = await ky(server.url, {
headers: {accept: 'foo/bar'}
}).json();

t.deepEqual(responseJson, {});

await server.close();
});

test('.json() with 200 response and empty body', async t => {
t.plan(2);

Expand Down

0 comments on commit 65f38f9

Please sign in to comment.