Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using the wildcard token to try to match random ports doesn't work if preceded by a colon #2204

Open
4 tasks done
silvenon opened this issue Jul 9, 2024 · 1 comment · May be fixed by #2209
Open
4 tasks done

Using the wildcard token to try to match random ports doesn't work if preceded by a colon #2204

silvenon opened this issue Jul 9, 2024 · 1 comment · May be fixed by #2209
Assignees
Labels
bug Something isn't working DX scope:browser Related to MSW running in a browser scope:node Related to MSW running in Node

Comments

@silvenon
Copy link
Sponsor

silvenon commented Jul 9, 2024

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 18 or higher

Node.js version

v20.15.1

Reproduction repository

https://github.com/silvenon/repro/tree/msw-wildcard-port

Reproduction steps

The reproduction branch contains a project with two test, both of them are passing, but the the first one is set to fail using test.fails.

In a situation where you have to match an unknown port, atttempt to match it in MSW with something like this:

import { setupServer } from "msw/node";
import { http, HttpResponse } from "msw";

const server = setupServer(
  http.get("http://localhost:*", () => HttpResponse.text("Hello world!")),
);

server.listen();

Then attempt to fetch something from localhost endpoints with random ports:

const res = await fetch("http://localhost:3000");
const result = await res.text();

Current behavior

Currently the library for matching the endpoint, path-to-regexp, seems to detect this as the parameter:

{"name":"TypeError","message":"Missing parameter name at 17","stack":"TypeError: Missing parameter name at 17
    at lexer (/Users/silvenon/Code/repro/node_modules/path-to-regexp/src/index.ts:72:24)
    at parse (/Users/silvenon/Code/repro/node_modules/path-to-regexp/src/index.ts:141:18)
    at stringToRegexp (/Users/silvenon/Code/repro/node_modules/path-to-regexp/src/index.ts:493:25)
    at pathToRegexp (/Users/silvenon/Code/repro/node_modules/path-to-regexp/src/index.ts:620:10)
    at match (/Users/silvenon/Code/repro/node_modules/path-to-regexp/src/index.ts:379:14)
    at matchRequestUrl (file:///Users/silvenon/Code/repro/node_modules/msw/src/core/utils/matching/matchRequestUrl.ts:66:18)
    at HttpHandler.parse (file:///Users/silvenon/Code/repro/node_modules/msw/src/core/handlers/HttpHandler.ts:114:19)
    at HttpHandler.run (file:///Users/silvenon/Code/repro/node_modules/msw/src/core/handlers/RequestHandler.ts:239:37)
    at executeHandlers (file:///Users/silvenon/Code/repro/node_modules/msw/src/core/utils/executeHandlers.ts:36:28)
    at file:///Users/silvenon/Code/repro/node_modules/msw/src/core/utils/handleRequest.ts:64:12"}

If I remove the colon it acts as expected.

Expected behavior

Regardless of whether the wildcard is used with or without colon it should achieve a similar result:

  • localhost:*
  • localhost*

I.e. for this use case it should work. Except that in the latter pattern I would expect the hostname to not only match localhost, but also localhostable etc., so I consider it to be a workaround rather than a solution.

@silvenon silvenon added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node labels Jul 9, 2024
@kettanaito kettanaito linked a pull request Jul 18, 2024 that will close this issue
2 tasks
@kettanaito
Copy link
Member

Thanks for raising this, @silvenon.

The issue occurs because of the way MSW coerces the * into a regular expression. We are using path-to-regexp for path matching, and the version we depend on doesn't understand wildcards.

Some later versions do but they ship their own breaking changes, some of which are still incompatible with the path matching in MSW.

In attempts to resolve this, I've tried the standard URLPattern API (#2209). It does resolve your issue! But it has one new of its own (tracked in the PR). I will wait for the folks behind the spec to comment before I continue with this.

@kettanaito kettanaito self-assigned this Jul 18, 2024
@kettanaito kettanaito added scope:browser Related to MSW running in a browser DX and removed needs:triage Issues that have not been investigated yet. labels Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working DX scope:browser Related to MSW running in a browser scope:node Related to MSW running in Node
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants