Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for base16.encode(), base64.encode(), base64url.encode() #62

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 33 additions & 23 deletions jest/__mocks__/encoding.js
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();
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."
}
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]"
}
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.25",
"version": "1.0.26",
"description": "Akamai EdgeWorkers Jest mocks",
"dependencies": {
"@babel/core": "^7.17.8",
Expand Down
105 changes: 72 additions & 33 deletions jest/test/examples/encoding/main.test.js
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"
);
});
});

6 changes: 6 additions & 0 deletions mocha/__mocks__/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,32 @@ export const atob = sinon.stub();
export const btoa = sinon.stub();

export const mock_base64_decode = sinon.stub();
export const mock_base64_encode = sinon.stub();
class Base64 {
constructor() {
this.decode = mock_base64_decode;
this.encode = mock_base64_encode;
}
}
export const base64 = new Base64();


export const mock_base64url_decode = sinon.stub();
export const mock_base64url_encode = sinon.stub();
class Base64url {
constructor() {
this.decode = mock_base64url_decode;
this.encode = mock_base64url_encode;
}
}
export const base64url = new Base64url();

export const mock_base16_decode = sinon.stub();
export const mock_base16_encode = sinon.stub();
class Base16 {
constructor() {
this.decode = mock_base16_decode;
this.encode = mock_base16_encode;
}
}
export const base16 = new Base16();
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."
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
import { atob, btoa, base64, base64url, base16 } from "encoding";

export function onClientRequest(request) {
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 enc3 = base16.encode(new Uint8Array([72, 101, 108, 108, 111])); // "Hello"
}

export function onOriginResponse(request, response) {
Expand Down
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.25",
"version": "1.0.26",
"description": "Akamai EdgeWorkers Mocha mocks",
"dependencies": {
"@babel/core": "^7.17.8",
Expand Down
Loading
Loading