Skip to content

fix(response): brings Response object to parity with Functions #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,30 @@ import { Response } from '../../../src/dev-runtime/internal/response';

test('has correct defaults', () => {
const response = new Response();
expect(response['body']).toBeUndefined();
expect(response['body']).toBeNull();
expect(response['statusCode']).toBe(200);
expect(response['headers']).toEqual({});
});

test('sets status code, body and headers from constructor', () => {
const response = new Response({
headers: {
'Access-Control-Allow-Origin': 'example.com',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE',
'Access-Control-Allow-Headers': 'Content-Type',
},
body: 'Error',
statusCode: 400,
});
expect(response['statusCode']).toBe(400);
expect(response['body']).toBe('Error');
expect(response['headers']).toEqual({
'Access-Control-Allow-Origin': 'example.com',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE',
'Access-Control-Allow-Headers': 'Content-Type',
});
});

test('sets status code', () => {
const response = new Response();
expect(response['statusCode']).toBe(200);
Expand All @@ -17,7 +36,7 @@ test('sets status code', () => {

test('sets body correctly', () => {
const response = new Response();
expect(response['body']).toBeUndefined();
expect(response['body']).toBeNull();
response.setBody('Hello');
expect(response['body']).toBe('Hello');
response.setBody({ url: 'https://dkundel.com' });
Expand Down Expand Up @@ -68,6 +87,26 @@ test('appends a header correctly with no existing one', () => {
});
});

test('setStatusCode returns the response', () => {
const response = new Response();
expect(response.setStatusCode(418)).toBe(response);
});

test('setBody returns the response', () => {
const response = new Response();
expect(response.setBody('Hello')).toBe(response);
});

test('setHeader returns the response', () => {
const response = new Response();
expect(response.setHeaders({ 'X-Test': 'Hello' })).toBe(response);
});

test('appendHeader returns the response', () => {
const response = new Response();
expect(response.appendHeader('X-Test', 'Hello')).toBe(response);
});

test('calls express response correctly', () => {
const mockRes = {
status: jest.fn(),
Expand Down
36 changes: 28 additions & 8 deletions packages/runtime-handler/src/dev-runtime/internal/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,64 @@ import debug from '../utils/debug';

const log = debug('twilio-runtime-handler:dev:response');

type ResponseOptions = {
headers?: Headers;
statusCode?: number;
body?: object | string;
};

type HeaderValue = number | string;
type Headers = {
[key: string]: HeaderValue;
};

export class Response implements TwilioResponse {
private body: undefined | any;
private body: null | any;
private statusCode: number;
private headers: Headers;

constructor() {
this.body = undefined;
constructor(options?: ResponseOptions) {
this.body = null;
this.statusCode = 200;
this.headers = {};

if (options && options.statusCode) {
this.statusCode = options.statusCode;
}
if (options && options.body) {
this.body = options.body;
}
if (options && options.headers) {
this.headers = options.headers;
}
}

setStatusCode(statusCode: number): void {
setStatusCode(statusCode: number): Response {
log('Setting status code to %d', statusCode);
this.statusCode = statusCode;
return this;
}

setBody(body: object | string): void {
setBody(body: object | string): Response {
log('Setting response body to %o', body);
this.body = body;
return this;
}

setHeaders(headersObject: Headers): void {
setHeaders(headersObject: Headers): Response {
log('Setting headers to: %P', headersObject);
if (typeof headersObject !== 'object') {
return;
return this;
}
this.headers = headersObject;
return this;
}

appendHeader(key: string, value: HeaderValue): void {
appendHeader(key: string, value: HeaderValue): Response {
log('Appending header for %s', key, value);
this.headers = this.headers || {};
this.headers[key] = value;
return this;
}

applyToExpressResponse(res: ExpressResponse): void {
Expand Down
43 changes: 41 additions & 2 deletions packages/twilio-run/__tests__/runtime/internal/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,30 @@ import { Response as ExpressResponse } from 'express';

test('has correct defaults', () => {
const response = new Response();
expect(response['body']).toBeUndefined();
expect(response['body']).toBeNull();
expect(response['statusCode']).toBe(200);
expect(response['headers']).toEqual({});
});

test('sets status code, body and headers from constructor', () => {
const response = new Response({
headers: {
'Access-Control-Allow-Origin': 'example.com',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE',
'Access-Control-Allow-Headers': 'Content-Type',
},
body: 'Error',
statusCode: 400,
});
expect(response['statusCode']).toBe(400);
expect(response['body']).toBe('Error');
expect(response['headers']).toEqual({
'Access-Control-Allow-Origin': 'example.com',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE',
'Access-Control-Allow-Headers': 'Content-Type',
});
});

test('sets status code', () => {
const response = new Response();
expect(response['statusCode']).toBe(200);
Expand All @@ -17,7 +36,7 @@ test('sets status code', () => {

test('sets body correctly', () => {
const response = new Response();
expect(response['body']).toBeUndefined();
expect(response['body']).toBeNull();
response.setBody('Hello');
expect(response['body']).toBe('Hello');
response.setBody({ url: 'https://dkundel.com' });
Expand Down Expand Up @@ -68,6 +87,26 @@ test('appends a header correctly with no existing one', () => {
});
});

test('setStatusCode returns the response', () => {
const response = new Response();
expect(response.setStatusCode(418)).toBe(response);
});

test('setBody returns the response', () => {
const response = new Response();
expect(response.setBody('Hello')).toBe(response);
});

test('setHeader returns the response', () => {
const response = new Response();
expect(response.setHeaders({ 'X-Test': 'Hello' })).toBe(response);
});

test('appendHeader returns the response', () => {
const response = new Response();
expect(response.appendHeader('X-Test', 'Hello')).toBe(response);
});

test('calls express response correctly', () => {
const mockRes = {
status: jest.fn(),
Expand Down
36 changes: 28 additions & 8 deletions packages/twilio-run/src/runtime/internal/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,64 @@ import { getDebugFunction } from '../../utils/logger';

const debug = getDebugFunction('twilio-run:response');

type ResponseOptions = {
headers?: Headers;
statusCode?: number;
body?: object | string;
};

type HeaderValue = number | string;
type Headers = {
[key: string]: HeaderValue;
};

export class Response implements TwilioResponse {
private body: undefined | any;
private body: null | any;
private statusCode: number;
private headers: Headers;

constructor() {
this.body = undefined;
constructor(options?: ResponseOptions) {
this.body = null;
this.statusCode = 200;
this.headers = {};

if (options && options.statusCode) {
this.statusCode = options.statusCode;
}
if (options && options.body) {
this.body = options.body;
}
if (options && options.headers) {
this.headers = options.headers;
}
}

setStatusCode(statusCode: number): void {
setStatusCode(statusCode: number): Response {
debug('Setting status code to %d', statusCode);
this.statusCode = statusCode;
return this;
}

setBody(body: object | string): void {
setBody(body: object | string): Response {
debug('Setting response body to %o', body);
this.body = body;
return this;
}

setHeaders(headersObject: Headers): void {
setHeaders(headersObject: Headers): Response {
debug('Setting headers to: %P', headersObject);
if (typeof headersObject !== 'object') {
return;
return this;
}
this.headers = headersObject;
return this;
}

appendHeader(key: string, value: HeaderValue): void {
appendHeader(key: string, value: HeaderValue): Response {
debug('Appending header for %s', key, value);
this.headers = this.headers || {};
this.headers[key] = value;
return this;
}

applyToExpressResponse(res: ExpressResponse): void {
Expand Down