Skip to content

🐛 BUG: unstable_dev fetch cannot accept query param #2641

@WalshyDev

Description

@WalshyDev

Which Cloudflare product(s) does this pertain to?

Wrangler

What version of Wrangler are you using?

2.8.1

What operating system are you using?

Mac

Describe the Bug

Due to the code here:
https://github.com/cloudflare/wrangler2/blob/89d78c0ac444885298ac052b0b3de23b69fb029b/packages/wrangler/src/api/dev.ts#L302-L328

The URL that's passed into fetch for unstable_dev does not handle query params. This obviously causes any test using these to fail.

Repro:

import { writeFile } from 'fs/promises';
import { expect, test } from 'vitest';
import { unstable_dev } from 'wrangler';

test('unstable_dev fetch repro', async () => {
	const script = `
	export default {
		fetch(req) {
			const url = new URL(req.url);
			const name = url.searchParams.get('name');
			return new Response('Hello, ' + name);
		}
	}
	`;
	await writeFile('script.js', script, { encoding: 'utf8' });

	const worker = await unstable_dev('script.js');

	const res = await worker.fetch('http://worker?name=Walshy');

	expect(res.status).toBe(200);

	const text = await res.text();

	expect(text).toBe('Hello, Walshy');

	await worker.stop();
});

Error:

 FAIL  test/repro.test.ts > unstable_dev fetch repro
AssertionError: expected 'Hello, null' to be 'Hello, Walshy' // Object.is equality
 ❯ test/repro.test.ts:25:15
     23|  const text = await res.text();
     24|
     25|  expect(text).toBe('Hello, Walshy');
       |               ^
     26|
     27|  await worker.stop();

  - Expected   "Hello, Walshy"
  + Received   "Hello, null"

A fix I'd suggest is instead just relying on the URL object, something like:

const url = new URL(input);
url.protocol = `${protocol}:`;
url.hostname = readyAddress;
url.port = readyPort;
input = url.toString();

Instead of manually constructing the URL string.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething that isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions