Skip to content

Commit

Permalink
Fix the worked-speedtest template (#5830) (#5840)
Browse files Browse the repository at this point in the history
The code had been converted to typescript in 026d944, 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 <spai@cloudflare.com>
  • Loading branch information
nielsreijers and threepointone authored Jul 3, 2024
1 parent d4eb5c5 commit 062e414
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 35 deletions.
3 changes: 1 addition & 2 deletions templates/worker-speedtest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions templates/worker-speedtest/jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}"]
}
4 changes: 2 additions & 2 deletions templates/worker-speedtest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion templates/worker-speedtest/src/down.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions templates/worker-speedtest/src/index.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion templates/worker-speedtest/src/up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
12 changes: 6 additions & 6 deletions templates/worker-speedtest/test/down.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
9 changes: 5 additions & 4 deletions templates/worker-speedtest/test/helpers.ts
Original file line number Diff line number Diff line change
@@ -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);
}
18 changes: 3 additions & 15 deletions templates/worker-speedtest/test/up.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 062e414

Please sign in to comment.