-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from aniefer/main
[EW-13279] Unit test mocks for request.arrayBuffer
- Loading branch information
Showing
14 changed files
with
91 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...les/respond-from-edgeworkers/responseprovider/hello-world-request-arrayBuffer/bundle.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
11 changes: 11 additions & 0 deletions
11
...xamples/respond-from-edgeworkers/responseprovider/hello-world-request-arrayBuffer/main.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
jest/test/examples/hello-world-request-array-buffer/main.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...les/respond-from-edgeworkers/responseprovider/hello-world-request-arrayBuffer/bundle.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
11 changes: 11 additions & 0 deletions
11
...xamples/respond-from-edgeworkers/responseprovider/hello-world-request-arrayBuffer/main.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
mocha/test/examples/hello-world-request-arrayBuffer/main.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>')); | ||
}); | ||
}); |