Skip to content

Commit

Permalink
fix(twilio-run): correctly serialize JSON Responses (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
philnash authored Jun 17, 2021
1 parent 0dde2a5 commit a40291d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ test('appends a header correctly with no existing one', () => {
});

test('calls express response correctly', () => {
const mockRes = ({
const mockRes = {
status: jest.fn(),
set: jest.fn(),
send: jest.fn(),
} as unknown) as ExpressResponse;
} as unknown as ExpressResponse;
const response = new Response();
response.setBody(`I'm a teapot!`);
response.setStatusCode(418);
Expand All @@ -84,3 +84,31 @@ test('calls express response correctly', () => {
expect(mockRes.status).toHaveBeenCalledWith(418);
expect(mockRes.set).toHaveBeenCalledWith({ 'Content-Type': 'text/plain' });
});

test('serializes a response', () => {
const response = new Response();
response.setBody("I'm a teapot!");
response.setStatusCode(418);
response.appendHeader('Content-Type', 'text/plain');

const serialized = response.serialize();

expect(serialized.body).toEqual("I'm a teapot!");
expect(serialized.statusCode).toEqual(418);
expect(serialized.headers).toEqual({ 'Content-Type': 'text/plain' });
});

test('serializes a response with content type set to application/json', () => {
const response = new Response();
response.setBody({ url: 'https://dkundel.com' });
response.setStatusCode(200);
response.appendHeader('Content-Type', 'application/json');

const serialized = response.serialize();

expect(serialized.body).toEqual(
JSON.stringify({ url: 'https://dkundel.com' })
);
expect(serialized.statusCode).toEqual(200);
expect(serialized.headers).toEqual({ 'Content-Type': 'application/json' });
});
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ export class Response implements TwilioResponse {
serialize() {
return {
statusCode: this.statusCode,
body: this.body.toString(),
body:
this.headers['Content-Type'] === 'application/json'
? JSON.stringify(this.body)
: this.body,
headers: this.headers,
};
}
Expand Down
32 changes: 30 additions & 2 deletions packages/twilio-run/__tests__/runtime/internal/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ test('appends a header correctly with no existing one', () => {
});

test('calls express response correctly', () => {
const mockRes = ({
const mockRes = {
status: jest.fn(),
set: jest.fn(),
send: jest.fn(),
} as unknown) as ExpressResponse;
} as unknown as ExpressResponse;
const response = new Response();
response.setBody(`I'm a teapot!`);
response.setStatusCode(418);
Expand All @@ -84,3 +84,31 @@ test('calls express response correctly', () => {
expect(mockRes.status).toHaveBeenCalledWith(418);
expect(mockRes.set).toHaveBeenCalledWith({ 'Content-Type': 'text/plain' });
});

test('serializes a response', () => {
const response = new Response();
response.setBody("I'm a teapot!");
response.setStatusCode(418);
response.appendHeader('Content-Type', 'text/plain');

const serialized = response.serialize();

expect(serialized.body).toEqual("I'm a teapot!");
expect(serialized.statusCode).toEqual(418);
expect(serialized.headers).toEqual({ 'Content-Type': 'text/plain' });
});

test('serializes a response with content type set to application/json', () => {
const response = new Response();
response.setBody({ url: 'https://dkundel.com' });
response.setStatusCode(200);
response.appendHeader('Content-Type', 'application/json');

const serialized = response.serialize();

expect(serialized.body).toEqual(
JSON.stringify({ url: 'https://dkundel.com' })
);
expect(serialized.statusCode).toEqual(200);
expect(serialized.headers).toEqual({ 'Content-Type': 'application/json' });
});
5 changes: 4 additions & 1 deletion packages/twilio-run/src/runtime/internal/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ export class Response implements TwilioResponse {
serialize() {
return {
statusCode: this.statusCode,
body: this.body.toString(),
body:
this.headers['Content-Type'] === 'application/json'
? JSON.stringify(this.body)
: this.body,
headers: this.headers,
};
}
Expand Down

0 comments on commit a40291d

Please sign in to comment.