Skip to content

Commit

Permalink
Fix tests and simplify logic (#1224)
Browse files Browse the repository at this point in the history
  • Loading branch information
samouri authored Apr 27, 2021
1 parent 92be275 commit 7c324cf
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
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

0 comments on commit 7c324cf

Please sign in to comment.