Skip to content

Commit

Permalink
Support for wasTerminated
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtanishqbhatia committed Aug 10, 2023
1 parent 3a5f3ac commit 121addb
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 7 deletions.
4 changes: 3 additions & 1 deletion jest/__mocks__/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import CacheKey from './cacheKey';
import { ReadableStream } from './streams'

export const mockRespondWith = jest.fn();
export const mockWasTerminated = jest.fn();
export const mockGetHeader = jest.fn();
export const mockSetHeader = jest.fn();
export const mockAddHeader = jest.fn();
Expand All @@ -29,6 +30,7 @@ const Request = jest.fn().mockImplementation(() => {
cpCode: 1191398,
cacheKey: new CacheKey(),
respondWith: mockRespondWith,
wasTerminated: mockWasTerminated,
getHeader: mockGetHeader,
setHeader: mockSetHeader,
addHeader: mockAddHeader,
Expand All @@ -41,7 +43,7 @@ const Request = jest.fn().mockImplementation(() => {
text: mockText,
arrayBuffer: mockArrayBuffer,
body: new ReadableStream()
};
};
});

export default Request;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"edgeworker-version": "0.1",
"description" : "Returns true if respondWith() has been called in the current handler and false otherwise."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function onClientResponse(request, response) {
request.respondWith(333, {}, "");
request.wasTerminated();
request.respondWith(222, {}, "GOOD: wasTerminated worked!");
}
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.24",
"version": "1.0.25",
"description": "Akamai EdgeWorkers Jest mocks",
"dependencies": {
"@babel/core": "^7.17.8",
Expand Down
28 changes: 28 additions & 0 deletions jest/test/examples/was-terminated/main.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { onClientResponse } from "../../../edgeworkers/examples/respond-from-edgeworkers/respondwith/was-terminated/main";
import Request from "request";

describe("demonstrates how an EdgeWorker can be used to see if respondWith() has been called.", () => {
beforeEach(() => {
jest.clearAllMocks();
});

test("Simple onClientResponse call to check that wasTerminated was called", () => {
let requestMock = new Request();
requestMock.mockWasTerminated = true;

onClientResponse(requestMock);
expect(requestMock.respondWith).toHaveBeenCalledTimes(2);
expect(requestMock.wasTerminated).toHaveBeenCalledTimes(1);
expect(requestMock.mockWasTerminated).toBeTruthy();
});

test("Simple onClientResponse call to check that wasTerminated was not called", () => {
let requestMock = new Request();
requestMock.mockWasTerminated = false;

onClientResponse(requestMock);
expect(requestMock.respondWith).toHaveBeenCalledTimes(2);
expect(requestMock.wasTerminated).toHaveBeenCalledTimes(1);
expect(requestMock.mockWasTerminated).not.toBeTruthy();
});
});
2 changes: 2 additions & 0 deletions mocha/__mocks__/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import CacheKey from './cacheKey';
import { ReadableStream } from './streams'

export const mockRespondWith = sinon.stub();
export const mockWasTerminated = sinon.stub();
export const mockGetHeader = sinon.stub();
export const mockSetHeader = sinon.stub();
export const mockAddHeader = sinon.stub();
Expand All @@ -31,6 +32,7 @@ export default class Request {
this.cpCode = 1191398;
this.cacheKey = new CacheKey();
this.respondWith = mockRespondWith;
this.wasTerminated = mockWasTerminated;
this.getHeader = mockGetHeader;
this.setHeader = mockSetHeader;
this.addHeader = mockAddHeader;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"edgeworker-version": "0.1",
"description" : "Returns true if respondWith() has been called in the current handler and false otherwise."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function onClientResponse(request, response) {
request.respondWith(333, {}, "");
request.wasTerminated();
request.respondWith(222, {}, "GOOD: wasTerminated worked!");
}
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.24",
"version": "1.0.25",
"description": "Akamai EdgeWorkers Mocha mocks",
"dependencies": {
"@babel/core": "^7.17.8",
Expand Down
31 changes: 31 additions & 0 deletions mocha/test/examples/was-terminated/main.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { onClientResponse } from "../../../edgeworkers/examples/respond-from-edgeworkers/respondwith/was-terminated/main";
import Request from "request";

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

describe("demonstrates how an EdgeWorker can be used to see if respondWith() has been called.", () => {
afterEach(function () {
sinon.reset();
});

it("Simple onClientResponse call to check that wasTerminated was called", () => {
let requestMock = new Request();
requestMock.mockWasTerminated = true;

onClientResponse(requestMock);
expect(requestMock.respondWith.callCount).to.be(2);
expect(requestMock.wasTerminated.callCount).to.be(1);
expect(requestMock.mockWasTerminated).to.be.true;
});

it("Simple onClientResponse call to check that wasTerminated was not called", () => {
let requestMock = new Request();
requestMock.mockWasTerminated = false;

onClientResponse(requestMock);
expect(requestMock.respondWith.callCount).to.be(2);
expect(requestMock.wasTerminated.callCount).to.be(1);
expect(requestMock.mockWasTerminated).to.be.false;
});
});

0 comments on commit 121addb

Please sign in to comment.