-
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.
Add support for base16.encode(), base64.encode(), base64url.encode()
- Loading branch information
1 parent
6e0982a
commit a74c853
Showing
12 changed files
with
228 additions
and
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,54 @@ | ||
export const mock_TextEncoder_encode = jest.fn(); | ||
export const TextEncoder = jest.fn().mockImplementation(() => { | ||
return { | ||
encoding: "utf-8", | ||
encode: mock_TextEncoder_encode, | ||
}; | ||
return { | ||
encoding: "utf-8", | ||
encode: mock_TextEncoder_encode | ||
}; | ||
}); | ||
|
||
export const mock_TextDecoder_decode = jest.fn(); | ||
export const TextDecoder = jest.fn().mockImplementation( () => { | ||
return { | ||
fatal: false, | ||
ignoreBOM: false, | ||
encoding: "utf-8", | ||
decode: mock_TextDecoder_decode | ||
}; | ||
export const TextDecoder = jest.fn().mockImplementation(() => { | ||
return { | ||
fatal: false, | ||
ignoreBOM: false, | ||
encoding: "utf-8", | ||
decode: mock_TextDecoder_decode | ||
}; | ||
}); | ||
|
||
export const atob = jest.fn(); | ||
export const btoa = jest.fn(); | ||
|
||
export const mock_base64_decode = jest.fn(); | ||
const Base64 = jest.fn().mockImplementation( () => { | ||
return { | ||
decode: mock_base64_decode, | ||
}; | ||
export const mock_base64_encode = jest.fn(); | ||
|
||
const Base64 = jest.fn().mockImplementation(() => { | ||
return { | ||
decode: mock_base64_decode, | ||
encode: mock_base64_encode | ||
}; | ||
}); | ||
|
||
export const base64 = new Base64(); | ||
|
||
export const mock_base64url_decode = jest.fn(); | ||
const Base64url = jest.fn().mockImplementation( () => { | ||
return { | ||
decode: mock_base64url_decode, | ||
}; | ||
export const mock_base64url_encode = jest.fn(); | ||
|
||
const Base64url = jest.fn().mockImplementation(() => { | ||
return { | ||
decode: mock_base64url_decode, | ||
encode: mock_base64url_encode | ||
}; | ||
}); | ||
export const base64url = new Base64url(); | ||
|
||
export const mock_base16_decode = jest.fn(); | ||
const Base16 = jest.fn().mockImplementation( () => { | ||
return { | ||
decode: mock_base16_decode, | ||
}; | ||
export const mock_base16_encode = jest.fn(); | ||
|
||
const Base16 = jest.fn().mockImplementation(() => { | ||
return { | ||
decode: mock_base16_decode, | ||
encode: mock_base16_encode | ||
}; | ||
}); | ||
export const base16 = new Base16(); |
4 changes: 2 additions & 2 deletions
4
jest/edgeworkers/examples/respond-from-edgeworkers/encoding/encoding/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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"edgeworker-version": "0.1", | ||
"description" : "Examples of atob, btoa, baseX.decode usage." | ||
"edgeworker-version": "0.2", | ||
"description" : "Examples of atob, btoa, baseX.decode, baseX.encode usage." | ||
} |
31 changes: 26 additions & 5 deletions
31
jest/edgeworkers/examples/respond-from-edgeworkers/encoding/encoding/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 |
---|---|---|
@@ -1,16 +1,37 @@ | ||
import { atob, btoa, base64, base64url, base16 } from "encoding"; | ||
|
||
export function onClientRequest(request) { | ||
let result = base64.decode("SGVsbG8=", "String"); //decodes to "Hello" | ||
let dec = atob("V29ybGQ="); //decodes to "World" | ||
let encoded = base64.encode( | ||
new Uint8Array([ | ||
72, | ||
101, | ||
108, | ||
108, | ||
111, | ||
44, | ||
32, | ||
119, | ||
111, | ||
114, | ||
108, | ||
100, | ||
33 | ||
]) | ||
); | ||
let result = base64.decode("SGVsbG8=", "String"); // Decodes to "Hello" | ||
let dec = atob("V29ybGQ="); // Decodes to "World" | ||
} | ||
|
||
export function onOriginRequest(request) { | ||
let enc = btoa("Hello"); | ||
let encoded = base64url.encode( | ||
new Uint8Array([72, 101, 108, 108, 111, 32, 116, 104, 101, 114, 101]) | ||
); | ||
let result1 = base64url.decode(enc, "String"); | ||
let result2 = base16.decode("576F726C64", "String"); //decodes to "World" | ||
let result2 = base16.decode("576F726C64", "String"); // Decodes to "World" | ||
let enc3 = base16.encode(new Uint8Array([72, 101, 108, 108, 111])); // "Hello" | ||
} | ||
|
||
export function onOriginResponse(request, response) { | ||
let result = base64.decode("SGVsbG8sIHdvcmxk", "Uint8Array"); //decodes to "[72,101,108,108,111,44,32,119,111,114,108,100]" | ||
} | ||
let result = base64.decode("SGVsbG8sIHdvcmxk", "Uint8Array"); // Decodes to "[72,101,108,108,111,44,32,119,111,114,108,100]" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,81 @@ | ||
import {onClientRequest, onOriginResponse, onOriginRequest} from "../../../edgeworkers/examples/respond-from-edgeworkers/encoding/encoding/main"; | ||
import { | ||
onClientRequest, | ||
onOriginResponse, | ||
onOriginRequest | ||
} from "../../../edgeworkers/examples/respond-from-edgeworkers/encoding/encoding/main"; | ||
import Request from "request"; | ||
import Response from "response"; | ||
import {atob, btoa, mock_base16_decode, mock_base64_decode, mock_base64url_decode} from "../../../__mocks__/encoding"; | ||
import { | ||
atob, | ||
btoa, | ||
mock_base16_decode, | ||
mock_base16_encode, | ||
mock_base64_decode, | ||
mock_base64_encode, | ||
mock_base64url_decode, | ||
mock_base64url_encode | ||
} from "../../../__mocks__/encoding"; | ||
|
||
describe("EdgeWorker that has atob, btoa, baseX.decode, baseX.encode usage", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('EdgeWorker that has atob, btoa, baseX.decode usage', () => { | ||
test("onClientRequest decodes hello World by using decode and atob ", () => { | ||
let requestMock = new Request(); | ||
onClientRequest(requestMock); | ||
expect(mock_base64_decode).toHaveBeenCalledTimes(1); | ||
expect(mock_base64_encode).toHaveBeenCalledTimes(1); | ||
expect(atob).toHaveBeenCalledTimes(1); | ||
expect(mock_base64_decode).toHaveBeenCalledWith("SGVsbG8=", "String"); | ||
expect(mock_base64_encode).toHaveBeenCalledWith( | ||
new Uint8Array([ | ||
72, | ||
101, | ||
108, | ||
108, | ||
111, | ||
44, | ||
32, | ||
119, | ||
111, | ||
114, | ||
108, | ||
100, | ||
33 | ||
]) | ||
); | ||
expect(atob).toBeCalledWith("V29ybGQ="); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
test("onOriginRequest decodes Hello World by using btoa, base64url.decode, base64url.encode, base16.encode and base16.decode", () => { | ||
let requestMock = new Request(); | ||
onOriginRequest(requestMock); | ||
expect(btoa).toHaveBeenCalledTimes(1); | ||
expect(mock_base64url_decode).toHaveBeenCalledTimes(1); | ||
expect(mock_base64url_encode).toHaveBeenCalledTimes(1); | ||
expect(mock_base16_decode).toHaveBeenCalledTimes(1); | ||
expect(mock_base16_encode).toHaveBeenCalledTimes(1); | ||
|
||
test("onClientRequest decodes hello World by using decode and atob ", () => { | ||
let requestMock = new Request(); | ||
onClientRequest(requestMock); | ||
expect(mock_base64_decode).toHaveBeenCalledTimes(1); | ||
expect(atob).toHaveBeenCalledTimes(1); | ||
expect(mock_base64_decode).toHaveBeenCalledWith("SGVsbG8=", "String"); | ||
expect(atob).toBeCalledWith("V29ybGQ="); | ||
}); | ||
expect(btoa).toHaveBeenCalledWith("Hello"); | ||
expect(mock_base16_decode).toHaveBeenCalledWith("576F726C64", "String"); | ||
expect(mock_base16_encode).toHaveBeenCalledWith( | ||
new Uint8Array([72, 101, 108, 108, 111]) | ||
); | ||
expect(mock_base64url_encode).toHaveBeenCalledWith( | ||
new Uint8Array([72, 101, 108, 108, 111, 32, 116, 104, 101, 114, 101]) | ||
); | ||
}); | ||
|
||
test("onOriginRequest decodes Hello World by using btoa, base64url.decode and base16.decode", () => { | ||
let requestMock = new Request(); | ||
onOriginRequest(requestMock); | ||
expect(btoa).toHaveBeenCalledTimes(1); | ||
expect(mock_base64url_decode).toHaveBeenCalledTimes(1); | ||
expect(mock_base16_decode).toHaveBeenCalledTimes(1); | ||
test("onOriginResponse decodes to Uint8Array using base64.decode", () => { | ||
let requestMock = new Request(); | ||
let responseMock = new Response(); | ||
onOriginResponse(requestMock, responseMock); | ||
expect(mock_base64_decode).toHaveBeenCalledTimes(1); | ||
|
||
expect(btoa).toHaveBeenCalledWith("Hello"); | ||
expect(mock_base16_decode).toHaveBeenCalledWith("576F726C64", "String"); | ||
}); | ||
|
||
test("onOriginResponse decodes to Uint8Array using base64.decode", () => { | ||
let requestMock = new Request(); | ||
let responseMock = new Response(); | ||
onOriginResponse(requestMock, responseMock); | ||
expect(mock_base64_decode).toHaveBeenCalledTimes(1); | ||
|
||
expect(mock_base64_decode).toHaveBeenCalledWith("SGVsbG8sIHdvcmxk", "Uint8Array"); | ||
}); | ||
expect(mock_base64_decode).toHaveBeenCalledWith( | ||
"SGVsbG8sIHdvcmxk", | ||
"Uint8Array" | ||
); | ||
}); | ||
}); | ||
|
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: 2 additions & 2 deletions
4
mocha/edgeworkers/examples/respond-from-edgeworkers/encoding/encoding/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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"edgeworker-version": "0.1", | ||
"description" : "Examples of atob, btoa, baseX.decode usage." | ||
"edgeworker-version": "0.2", | ||
"description" : "Examples of atob, btoa, baseX.decode, baseX.encode usage." | ||
} |
21 changes: 21 additions & 0 deletions
21
mocha/edgeworkers/examples/respond-from-edgeworkers/encoding/encoding/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
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
Oops, something went wrong.