Skip to content

Instantly share code, notes, and snippets.

@BrunoQuaresma
Created April 19, 2020 05:48
Show Gist options
  • Save BrunoQuaresma/2b572e8c915599290725a2306c810b72 to your computer and use it in GitHub Desktop.
Save BrunoQuaresma/2b572e8c915599290725a2306c810b72 to your computer and use it in GitHub Desktop.
Zeit Now Test Utils for Cloud Functions/Lambda
import logsFunction from '../logs';
import { IncomingMessage, ServerResponse } from 'http';
import { Socket } from 'net';
import { NowRequestQuery, NowRequestCookies, NowRequestBody } from '@now/node';
class NowRequestMock extends IncomingMessage {
public query: NowRequestQuery = {};
public cookies: NowRequestCookies = {};
public body: NowRequestBody;
constructor(options?: Partial<NowRequestMock>) {
super(new Socket());
if (options) {
this.method = options.method;
this.body = options.body;
this.query = options.query || {};
this.headers = options.headers || {};
}
}
}
class NowResponseMock extends ServerResponse {
public body: any;
public jsonBody: any;
send(body: any) {
this.body = body;
return this;
}
json(jsonBody: any) {
this.jsonBody = jsonBody;
return this;
}
status(statusCode: number) {
this.statusCode = statusCode;
return this;
}
}
// Samples
it('returns 404 when request is not a POST', async () => {
const req = new NowRequestMock({ method: 'GET' });
const res = new NowResponseMock(req);
await logsFunction(req, res);
expect(res.statusCode).toBe(404);
});
describe('POST /api/boxes/[boxId]/logs', () => {
it('returns 400 when there is no request body', async () => {
const req = new NowRequestMock({ method: 'POST' });
const res = new NowResponseMock(req);
await logsFunction(req, res);
expect(res.statusCode).toBe(400);
expect(res.jsonBody).toMatchInlineSnapshot(`
Object {
"message": "A request body is required.",
}
`);
});
it('returns 404 when there is no box with the given ID', async () => {
const nonExistentId = String(faker.random.number());
const req = new NowRequestMock({
method: 'POST',
query: { boxId: nonExistentId },
body: 'Any log body',
});
const res = new NowResponseMock(req);
await logsFunction(req, res);
expect(res.statusCode).toBe(404);
expect(res.jsonBody).toMatchInlineSnapshot(`
Object {
"message": "No box found with id ${nonExistentId}",
}
`);
});
describe('when is a public box', () => {
it('returns 201', async () => {
const req = new NowRequestMock({
method: 'POST',
query: { boxId: box.id },
body: 'Any log body',
});
const res = new NowResponseMock(req);
await logsFunction(req, res);
expect(res.statusCode).toBe(201);
expect(res.jsonBody.id).not.toBeNull();
});
});
describe('when is a user box', () => {
it('returns 401 when authorization header is empty', async () => {
const req = new NowRequestMock({
method: 'POST',
query: { boxId: box.id },
body: 'Any log body',
});
const res = new NowResponseMock(req);
await logsFunction(req, res);
expect(res.statusCode).toBe(401);
expect(res.jsonBody).toMatchInlineSnapshot(`
Object {
"message": "Permission denied.",
}
`);
});
it('returns 201', async () => {
const req = new NowRequestMock({
method: 'POST',
query: { boxId: box.id },
headers: { authorization: box.key },
body: 'Any log body',
});
const res = new NowResponseMock(req);
await logsFunction(req, res);
expect(res.statusCode).toBe(201);
expect(res.jsonBody.id).not.toBeNull();
});
});
});
@BrunoQuaresma
Copy link
Author

the logsFunction is a function like this:

export default async (req: NowRequest, res: NowResponse) => { }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment