Skip to content

Commit

Permalink
don't parse body as json on 204 responses (stoplightio#2252)
Browse files Browse the repository at this point in the history
  • Loading branch information
skarimo authored Apr 7, 2023
1 parent 686bdb1 commit 00233b7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
20 changes: 20 additions & 0 deletions packages/http/src/utils/__tests__/parseResponse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('parseResponseBody()', () => {
headers: new Headers({ 'content-type': 'application/json' }),
json: jest.fn().mockResolvedValue({ test: 'test' }),
text: jest.fn(),
status: 200,
};

expect(response.text).not.toHaveBeenCalled();
Expand All @@ -20,6 +21,7 @@ describe('parseResponseBody()', () => {
describe('body is not parseable', () => {
it('returns error', () => {
const response = {
status: 200,
headers: new Headers({ 'content-type': 'application/json' }),
json: jest.fn().mockRejectedValue(new Error('Big Bada Boom')),
text: jest.fn(),
Expand All @@ -35,6 +37,7 @@ describe('parseResponseBody()', () => {
describe('body is readable', () => {
it('returns body text', () => {
const response = {
status: 200,
headers: new Headers({ 'content-type': 'text/html' }),
json: jest.fn(),
text: jest.fn().mockResolvedValue('<html>Test</html>'),
Expand All @@ -48,9 +51,11 @@ describe('parseResponseBody()', () => {
describe('body is not readable', () => {
it('returns error', () => {
const response = {
status: 200,
headers: new Headers(),
json: jest.fn(),
text: jest.fn().mockRejectedValue(new Error('Big Bada Boom')),

};

expect(response.json).not.toHaveBeenCalled();
Expand All @@ -62,6 +67,7 @@ describe('parseResponseBody()', () => {
describe('content-type header not set', () => {
it('returns body text', () => {
const response = {
status: 200,
headers: new Headers(),
json: jest.fn(),
text: jest.fn().mockResolvedValue('Plavalaguna'),
Expand All @@ -71,6 +77,20 @@ describe('parseResponseBody()', () => {
return assertResolvesRight(parseResponseBody(response), body => expect(body).toEqual('Plavalaguna'));
});
});

describe('content-type header is set', () => {
it('does not call json() on 204', () => {
const response = {
status: 204,
headers: new Headers({ 'content-type': 'application/json' }),
json: jest.fn(),
text: jest.fn().mockResolvedValue(''),
};

expect(response.json).not.toHaveBeenCalled();
return assertResolvesRight(parseResponseBody(response), body => expect(body).toEqual(''));
});
});
});

describe('parseResponseHeaders()', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/http/src/utils/parseResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import { Dictionary } from '@stoplight/types';
import { IHttpResponse } from '../types';

export const parseResponseBody = (
response: Pick<Response, 'headers' | 'json' | 'text'>
response: Pick<Response, 'headers' | 'json' | 'text' | 'status'>
): TE.TaskEither<Error, unknown> =>
TE.tryCatch(
() =>
typeIs(response.headers.get('content-type') || '', ['application/json', 'application/*+json'])
(response.status != 204 && typeIs(response.headers.get('content-type') || '', ['application/json', 'application/*+json']))
? response.json()
: response.text(),
E.toError
Expand Down

0 comments on commit 00233b7

Please sign in to comment.