Skip to content

Commit

Permalink
apollo-datasource-rest: Dont' parse as json when status code is 204 N…
Browse files Browse the repository at this point in the history
…o Content (#2446)
  • Loading branch information
bartverbruggen authored and abernix committed Apr 5, 2019
1 parent 984fb45 commit aa7c2b8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/apollo-datasource-rest/src/RESTDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ export abstract class RESTDataSource<TContext = any> extends DataSource {
protected parseBody(response: Response): Promise<object | string> {
const contentType = response.headers.get('Content-Type');
if (
// As one might expect, a "204 No Content" is empty! This means there
// isn't enough to `JSON.parse`, and trying will result in an error.
response.status !== 204 &&
contentType &&
(contentType.startsWith('application/json') ||
contentType.startsWith('application/hal+json'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,24 @@ describe('RESTDataSource', () => {

expect(data).toEqual('bar');
});

it('returns data as a string when response status code is 204 no content', async () => {
const dataSource = new class extends RESTDataSource {
baseURL = 'https://api.example.com';

getFoo() {
return this.get('');
}
}();

dataSource.httpCache = httpCache;

fetch.mockResponseOnce('', { 'Content-Type': 'application/json' }, 204);

const data = await dataSource.getFoo();

expect(data).toEqual('');
});
});

describe('memoization', () => {
Expand Down

0 comments on commit aa7c2b8

Please sign in to comment.