Skip to content

Commit

Permalink
Merge pull request #52 from aniefer/main
Browse files Browse the repository at this point in the history
[EW-13279] Unit test mocks for request.arrayBuffer
  • Loading branch information
bmatthew committed Jan 31, 2023
2 parents 9a47ff1 + 5c87912 commit c39e911
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 8 deletions.
2 changes: 2 additions & 0 deletions jest/__mocks__/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const mockSetVariable = jest.fn();
export const mockRoute = jest.fn();
export const mockJson = jest.fn();
export const mockText = jest.fn();
export const mockArrayBuffer = jest.fn();

const Request = jest.fn().mockImplementation(() => {
return {
Expand All @@ -38,6 +39,7 @@ const Request = jest.fn().mockImplementation(() => {
route: mockRoute,
json: mockJson,
text: mockText,
arrayBuffer: mockArrayBuffer,
body: new ReadableStream()
};
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"edgeworker-version": "0.1",
"description" : "Hello World responseProvider request arrayBuffer example"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

import { createResponse } from 'create-response';

export function responseProvider(request) {
return request.arrayBuffer().then(function(ab) {
const decoder = new TextDecoder();

var html = '<html><body><p>' + decoder.decode( new Uint8Array(ab)) + '</p></body></html>';
return createResponse(200, {}, html);
});
}
4 changes: 2 additions & 2 deletions jest/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion jest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "edgeworkers-jest-mocks",
"version": "1.0.21",
"version": "1.0.22",
"description": "Akamai EdgeWorkers Jest mocks",
"dependencies": {
"@babel/core": "^7.17.8",
Expand Down
2 changes: 1 addition & 1 deletion jest/test/examples/conference-details/main.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {onClientRequest} from "respond-from-edgeworkers/respondWith/conference-details/main";
import {onClientRequest} from "respond-from-edgeworkers/respondwith/conference-details/main";
import Request from "request";

describe('Conference Attendance Code API call that returns the meeting details of a conference as HTML if the user provides the correct code', () => {
Expand Down
21 changes: 21 additions & 0 deletions jest/test/examples/hello-world-request-array-buffer/main.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {createResponse} from "create-response";
import {responseProvider} from "examples/respond-from-edgeworkers/responseprovider/hello-world-request-arrayBuffer/main";
import Request from "request";
import {mockArrayBuffer} from "request";

describe('EdgeWorker that generates a simple html page at the Edge and adds the request body to the response header', () => {

test('responseProvider should retrieve the body of the request', async () => {
const requestMock = new Request();

mockArrayBuffer.mockReturnValue(new Promise(function(resolve) {
const encoder = new TextEncoder();
resolve(encoder.encode("Hello World").buffer);
}));

await responseProvider(requestMock);

expect(requestMock.arrayBuffer).toHaveBeenCalled();
expect(createResponse).toHaveBeenCalledWith(200, {}, '<html><body><p>Hello World</p></body></html>');
});
});
2 changes: 2 additions & 0 deletions mocha/__mocks__/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const mockSetVariable = sinon.stub();
export const mockRoute = sinon.stub();
export const mockJson = sinon.stub();
export const mockText = sinon.stub();
export const mockArrayBuffer = sinon.stub();

export default class Request {
constructor() {
Expand All @@ -40,6 +41,7 @@ export default class Request {
this.route = mockRoute;
this.json = mockJson;
this.text = mockText;
this.arrayBuffer = mockArrayBuffer;
this.body = new ReadableStream();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"edgeworker-version": "0.1",
"description" : "Hello World responseProvider request arrayBuffer example"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

import { createResponse } from 'create-response';

export function responseProvider(request) {
return request.arrayBuffer().then(function(ab) {
const decoder = new TextDecoder();

var html = '<html><body><p>' + decoder.decode( new Uint8Array(ab)) + '</p></body></html>';
return createResponse(200, {}, html);
});
}
4 changes: 2 additions & 2 deletions mocha/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mocha/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "edgeworkers-mocha-mocks",
"version": "1.0.21",
"version": "1.0.22",
"description": "Akamai EdgeWorkers Mocha mocks",
"dependencies": {
"@babel/core": "^7.17.8",
Expand Down
2 changes: 1 addition & 1 deletion mocha/test/examples/conference-details/main.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {onClientRequest} from "../../../src/edgeworkers/examples/respond-from-edgeworkers/respondWith/conference-details/main";
import {onClientRequest} from "../../../src/edgeworkers/examples/respond-from-edgeworkers/respondwith/conference-details/main";
import Request from "request";

const sinon = require("sinon");
Expand Down
28 changes: 28 additions & 0 deletions mocha/test/examples/hello-world-request-arrayBuffer/main.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {createResponse} from "create-response";
import {responseProvider} from "../../../edgeworkers/examples/respond-from-edgeworkers/responseprovider/hello-world-request-arrayBuffer/main";
import Request from "request";
import {mockArrayBuffer} from "request";

const sinon = require("sinon");
const expect = require('expect.js');

describe('EdgeWorker that generates a simple html page at the Edge and adds the request body to the response header', () => {

afterEach(() => {
sinon.reset();
});

it('responseProvider should retrieve the body of the request', async () => {
const requestMock = new Request();

mockArrayBuffer.returns(new Promise(function(resolve) {
const encoder = new TextEncoder();
resolve(encoder.encode("Hello World").buffer);
}));

await responseProvider(requestMock);

expect(requestMock.arrayBuffer.callCount).to.be(1);
expect(createResponse.calledWith(200, {}, '<html><body><p>Hello World</p></body></html>'));
});
});

0 comments on commit c39e911

Please sign in to comment.