Skip to content
New issue

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

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

Already on GitHub? # to your account

CF Worker: Fix tests #1224

Merged
merged 1 commit into from
Apr 27, 2021
Merged
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
8 changes: 4 additions & 4 deletions packages/cloudflare-optimizer-scripts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ async function handleEvent(event, config) {
async function handleRequest(event, config) {
validateConfiguration(config);

const request = event.request;
let request = event.request;
const url = new URL(request.url);
if (isReverseProxy(config)) {
url.hostname = config.proxy.origin;
}
request = new Request(url.toString(), request);

// Immediately return if not GET.
if (request.method !== 'GET') {
request.url = url;
return fetch(request);
}

Expand All @@ -85,7 +85,7 @@ async function handleRequest(event, config) {
}
}

const response = await fetch(url, request);
const response = await fetch(request);
const clonedResponse = response.clone();
const {headers, status, statusText} = response;

Expand Down Expand Up @@ -113,7 +113,7 @@ async function handleRequest(event, config) {
return rewritten;
} catch (err) {
if (process.env.NODE_ENV !== 'test') {
console.error(`Failed to optimize: ${url.toString()}, with Error; ${err}`);
console.error(`Failed to optimize: ${url.toString()}, with Error: ${err}`);
}
return clonedResponse;
}
Expand Down
7 changes: 6 additions & 1 deletion packages/cloudflare-optimizer-scripts/test/builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class Response {
});
}
}
class Request {
constructor(url, oldRequest) {
return {...oldRequest, url};
}
}

class HTMLRewriter {
on() {
Expand All @@ -40,4 +45,4 @@ class HTMLRewriter {
}
}

module.exports = {Response, HTMLRewriter};
module.exports = {Response, HTMLRewriter, Request};
7 changes: 4 additions & 3 deletions packages/cloudflare-optimizer-scripts/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const {
validateConfiguration,
resetOptimizerForTesting,
} = require('../src/index');
const {Response, HTMLRewriter} = require('./builtins');
const {Request, Response, HTMLRewriter} = require('./builtins');

beforeEach(() => {
global.fetch = jest.fn();
Expand All @@ -39,6 +39,7 @@ beforeEach(() => {
AmpOptimizer.transformHtmlSpy.mockReset();
AmpOptimizer.transformHtmlSpy.mockImplementation((input) => `transformed-${input}`);

global.Request = Request;
global.Response = Response;
global.HTMLRewriter = HTMLRewriter;
});
Expand Down Expand Up @@ -108,7 +109,7 @@ describe('handleEvent', () => {
const input = `<html amp><body></body></html>`;
global.fetch.mockReturnValue(getResponse(input));
await getOutput('http://test.com');
expect(global.fetch).toBeCalledWith('http://test.com/', expect.anything());
expect(global.fetch).toBeCalledWith({url: 'http://test.com/', method: 'GET'});
});

it('Should modify request url for reverse-proxy', async () => {
Expand All @@ -117,7 +118,7 @@ describe('handleEvent', () => {
global.fetch.mockReturnValue(getResponse(input));

await getOutput('http://test.com', config);
expect(global.fetch).toBeCalledWith('http://test-origin.com/', expect.anything());
expect(global.fetch).toBeCalledWith({url: 'http://test-origin.com/', method: 'GET'});
});

it('should call enable passThroughOnException', async () => {
Expand Down