From 062e4140b2aaedfb8aee9809e2697be66806e660 Mon Sep 17 00:00:00 2001 From: Niels Reijers Date: Wed, 3 Jul 2024 22:48:00 +0800 Subject: [PATCH] Fix the worked-speedtest template (#5830) (#5840) The code had been converted to typescript in 026d944d, but packages.json and several other places still refered to .js files. In addition a refactoring of the test code meant tests for both /up and /down urls were now being sent to the handler for /up, causing the tests to fail. I removed some of the test in up.test.ts since they were testing different values of the ?bytes= parameter, but this parameter is only used in down.ts, not in up.ts. Co-authored-by: Sunil Pai --- templates/worker-speedtest/README.md | 3 +-- templates/worker-speedtest/jest.config.json | 4 ++-- templates/worker-speedtest/package.json | 4 ++-- templates/worker-speedtest/src/down.ts | 2 +- templates/worker-speedtest/src/index.ts | 4 ++-- templates/worker-speedtest/src/up.ts | 2 +- templates/worker-speedtest/test/down.test.ts | 12 ++++++------ templates/worker-speedtest/test/helpers.ts | 9 +++++---- templates/worker-speedtest/test/up.test.ts | 18 +++--------------- 9 files changed, 23 insertions(+), 35 deletions(-) diff --git a/templates/worker-speedtest/README.md b/templates/worker-speedtest/README.md index b5361b3746f3..934daa109d26 100644 --- a/templates/worker-speedtest/README.md +++ b/templates/worker-speedtest/README.md @@ -4,8 +4,7 @@ Worker for measuring download / upload connection speed from the client side, using the [Performance Timing API](https://w3c.github.io/perf-timing-primer/). -[`index.js`](https://github.com/cloudflare/worker-speedtest-template/blob/master/src/index.js) is the content of the Workers script. - +[`index.ts`](https://github.com/cloudflare/workers-sdk/tree/main/templates/worker-speedtest/src/index.ts) is the content of the Workers script. _Note:_ when running this as your own worker, your latency measurements may differ a small amount from the [official version](https://speed.cloudflare.com). This is due to the fact that we rely on an internal mechanism to determine the amount of server processing time, which is then subtracted from the measurement. ## Setup diff --git a/templates/worker-speedtest/jest.config.json b/templates/worker-speedtest/jest.config.json index 5d491731bd8e..ba361dc04cf3 100644 --- a/templates/worker-speedtest/jest.config.json +++ b/templates/worker-speedtest/jest.config.json @@ -11,8 +11,8 @@ "testRegex": "/test/.*\\.(test|spec)\\.(ts|tsx|js)$", "testEnvironment": "miniflare", "testEnvironmentOptions": { - "scriptPath": "./src/index.js", + "scriptPath": "./src/index.ts", "modules": true }, - "collectCoverageFrom": ["src/**/*.{js,jsx}"] + "collectCoverageFrom": ["src/**/*.{ts,js,jsx}"] } diff --git a/templates/worker-speedtest/package.json b/templates/worker-speedtest/package.json index e5b2dbc37d35..598e5a605816 100644 --- a/templates/worker-speedtest/package.json +++ b/templates/worker-speedtest/package.json @@ -3,8 +3,8 @@ "version": "0.0.0", "private": true, "scripts": { - "deploy": "wrangler deploy src/index.js", - "dev": "wrangler dev src/index.js", + "deploy": "wrangler deploy src/index.ts", + "dev": "wrangler dev src/index.ts", "test": "jest" }, "devDependencies": { diff --git a/templates/worker-speedtest/src/down.ts b/templates/worker-speedtest/src/down.ts index e52894c1d2d4..c82cd3bf5cda 100644 --- a/templates/worker-speedtest/src/down.ts +++ b/templates/worker-speedtest/src/down.ts @@ -17,7 +17,7 @@ export default async function (request: Request) { res.headers.set('content-type', 'application/octet-stream'); if (request.cf && request.cf.colo) { - res.headers.set('cf-meta-colo', request.cf.colo); + res.headers.set('cf-meta-colo', String(request.cf.colo)); } res.headers.set('access-control-expose-headers', 'cf-meta-colo, cf-meta-request-time'); diff --git a/templates/worker-speedtest/src/index.ts b/templates/worker-speedtest/src/index.ts index 5921b6c91734..26b3bbb774f4 100644 --- a/templates/worker-speedtest/src/index.ts +++ b/templates/worker-speedtest/src/index.ts @@ -1,5 +1,5 @@ -import down from './down.js'; -import up from './up.js'; +import down from './down'; +import up from './up'; export default { async fetch(req: Request) { diff --git a/templates/worker-speedtest/src/up.ts b/templates/worker-speedtest/src/up.ts index 8b879451e3ec..e0538161db58 100644 --- a/templates/worker-speedtest/src/up.ts +++ b/templates/worker-speedtest/src/up.ts @@ -7,7 +7,7 @@ export default async function (request: Request) { res.headers.set('timing-allow-origin', '*'); if (request.cf && request.cf.colo) { - res.headers.set('cf-meta-colo', request.cf.colo); + res.headers.set('cf-meta-colo', String(request.cf.colo)); } res.headers.set('access-control-expose-headers', 'cf-meta-colo, cf-meta-request-time'); diff --git a/templates/worker-speedtest/test/down.test.ts b/templates/worker-speedtest/test/down.test.ts index bbe2bfcd5d82..a9da64c31d31 100644 --- a/templates/worker-speedtest/test/down.test.ts +++ b/templates/worker-speedtest/test/down.test.ts @@ -5,36 +5,36 @@ async function read(res: Response) { } test('default bytes', async () => { - const text = await run().then(read); + const text = await run('down').then(read); expect(text.length).toBe(0); }); [0, 1, 10, 50, 99].forEach(bytes => { test(`low request bytes :: ${bytes}`, async () => { - const text = await run('GET', bytes).then(read); + const text = await run('down', 'GET', bytes).then(read); expect(text.length).toEqual(bytes); }); }); [100, 1e3, 1e6, 1e7].forEach(bytes => { test(`request bytes :: get ${bytes} bytes`, async () => { - const text = await run('GET', bytes).then(read); + const text = await run('down', 'GET', bytes).then(read); expect(text.length).toEqual(bytes); }); }); test('max bytes', async () => { - const text = await run('GET', Infinity).then(read); + const text = await run('down', 'GET', Infinity).then(read); expect(text.length).toEqual(1e8); }); test('negative bytes', async () => { - const content = await run('GET', -100).then(read); + const content = await run('down', 'GET', -100).then(read); expect(content.length).toBe(100); }); test('includes request time', async () => { - const { headers } = await run(); + const { headers } = await run('down'); const reqTime = headers.get('cf-meta-request-time'); if (!reqTime) throw new Error('missing request time header'); diff --git a/templates/worker-speedtest/test/helpers.ts b/templates/worker-speedtest/test/helpers.ts index f5825afca996..2da4adfeeb1a 100644 --- a/templates/worker-speedtest/test/helpers.ts +++ b/templates/worker-speedtest/test/helpers.ts @@ -1,10 +1,11 @@ -import worker from '../src/up'; +import worker from '../src/index'; type methods = 'GET' | 'POST'; +type directions = 'up' | 'down'; -export async function run(method?: methods, num?: number) { - let url = 'https://example.com'; +export async function run(direction: directions, method?: methods, num?: number) { + let url = `https://example.com/${direction}`; if (num != null) url += '?bytes=' + num; let req = new Request(url, { method }); - return worker(req); + return worker.fetch(req); } diff --git a/templates/worker-speedtest/test/up.test.ts b/templates/worker-speedtest/test/up.test.ts index e58982b71748..905c01a33f32 100644 --- a/templates/worker-speedtest/test/up.test.ts +++ b/templates/worker-speedtest/test/up.test.ts @@ -5,31 +5,19 @@ async function read(res: Response) { } test('get request', async () => { - const res = await run('GET'); + const res = await run('up', 'GET'); expect(await read(res)).toEqual('OK'); expect(res.status).toBe(200); }); test('empty post request', async () => { - const res = await run('POST', 0); - expect(await read(res)).toEqual('OK'); - expect(res.status).toBe(200); -}); - -test('small post request', async () => { - const res = await run('POST', 10); - expect(await read(res)).toEqual('OK'); - expect(res.status).toBe(200); -}); - -test('large post request', async () => { - const res = await run('POST', 1e8); + const res = await run('up', 'POST'); expect(await read(res)).toEqual('OK'); expect(res.status).toBe(200); }); test('includes request time', async () => { - const { headers } = await run('POST'); + const { headers } = await run('up', 'POST'); const reqTime = headers.get('cf-meta-request-time'); if (!reqTime) throw new Error('missing request time header');