From 39c73c2bb72837183e2d6446033079f62a1f6df5 Mon Sep 17 00:00:00 2001 From: Jake Fried Date: Tue, 27 Apr 2021 15:10:36 -0400 Subject: [PATCH] Fix tests and simplify logic --- packages/cloudflare-optimizer-scripts/src/index.js | 8 ++++---- packages/cloudflare-optimizer-scripts/test/builtins.js | 7 ++++++- packages/cloudflare-optimizer-scripts/test/index.test.js | 7 ++++--- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/cloudflare-optimizer-scripts/src/index.js b/packages/cloudflare-optimizer-scripts/src/index.js index 7f0170130..a1c073685 100644 --- a/packages/cloudflare-optimizer-scripts/src/index.js +++ b/packages/cloudflare-optimizer-scripts/src/index.js @@ -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); } @@ -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; @@ -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; } diff --git a/packages/cloudflare-optimizer-scripts/test/builtins.js b/packages/cloudflare-optimizer-scripts/test/builtins.js index bbc623027..21489d786 100644 --- a/packages/cloudflare-optimizer-scripts/test/builtins.js +++ b/packages/cloudflare-optimizer-scripts/test/builtins.js @@ -30,6 +30,11 @@ class Response { }); } } +class Request { + constructor(url, oldRequest) { + return {...oldRequest, url}; + } +} class HTMLRewriter { on() { @@ -40,4 +45,4 @@ class HTMLRewriter { } } -module.exports = {Response, HTMLRewriter}; +module.exports = {Response, HTMLRewriter, Request}; diff --git a/packages/cloudflare-optimizer-scripts/test/index.test.js b/packages/cloudflare-optimizer-scripts/test/index.test.js index 484de0fb8..bed38451f 100644 --- a/packages/cloudflare-optimizer-scripts/test/index.test.js +++ b/packages/cloudflare-optimizer-scripts/test/index.test.js @@ -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(); @@ -39,6 +39,7 @@ beforeEach(() => { AmpOptimizer.transformHtmlSpy.mockReset(); AmpOptimizer.transformHtmlSpy.mockImplementation((input) => `transformed-${input}`); + global.Request = Request; global.Response = Response; global.HTMLRewriter = HTMLRewriter; }); @@ -108,7 +109,7 @@ describe('handleEvent', () => { const input = ``; 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 () => { @@ -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 () => {