Skip to content

Conversation

@SksOp
Copy link

@SksOp SksOp commented Jan 7, 2026

This relates to #4703

Changes

  • adds a normalizeOrigin function in lib/mock/mock-utils.js

Features

Bug Fixes

MockAgent origin is case-sensetive #4703

Status

dispatcher[kDispatches] = result.dispatcher[kDispatches]
return dispatcher
if (result && typeof keyMatcher !== 'string') {
const normalizedKeyMatcher = normalizeOrigin(keyMatcher, this[kIgnoreTrailingSlash])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, if stored correctly, the key matcher doe snot need new normalization, isn't it?

[kMockAgentGet] (origin) {
// First check if we can immediately find it
const result = this[kClients].get(origin)
const normalizedOrigin = normalizeOrigin(origin, this[kIgnoreTrailingSlash])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On L59 the origin is already normalized by the caller, we can save another normalization, wdyt?


[kMockAgentSet] (origin, dispatcher) {
this[kClients].set(origin, { count: 0, dispatcher })
const normalizedOrigin = normalizeOrigin(origin, this[kIgnoreTrailingSlash])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as per the getter one, unless this is called explicitly somewhere else and the origin is not normalized, we can save this call as well

return origin
}

let originString = typeof origin === 'string' ? origin : origin.toString()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe?

Suggested change
let originString = typeof origin === 'string' ? origin : origin.toString()
let originString = typeof origin === 'string' ? origin : origin.origin

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yahh true, since we are just dealing with origin and its an instance of URL

So maybe I should just check if its a URL instance i can just return origin

let originString = typeof origin === 'string' ? origin : origin.toString()

try {
const url = new URL(originString)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the passed arg is already an URL instance, maybe we can try to just infer the origin first?


try {
const url = new URL(originString)
url.hostname = url.hostname.toLowerCase()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The URL already does the parsing into lower case as part of the spec

SksOp added 2 commits January 8, 2026 15:48
…eter and simplify URL handling. Update MockAgent to utilize the new implementation for origin normalization.
…ethod, improving clarity and consistency in origin handling.
@SksOp
Copy link
Author

SksOp commented Jan 8, 2026

I removed the redundant normalisation in mock agent and kept the normalising logic to dispatch and get only. Also changed the logic of normalization

SksOp added 2 commits January 8, 2026 16:01
… returning the origin of a URL instance, simplifying the logic and enhancing readability.
…ameter, allowing for flexible origin normalization. Update MockAgent methods to utilize the new parameter for improved origin handling.
@SksOp
Copy link
Author

SksOp commented Jan 8, 2026

I think passed option ignoreTrailingSpace is not affecting the result.
As the URL.origin always removes the trailing space.
Even if a string is passed as origin, I am converting it to the URL instance and retreiving the origin, which again remove the trailing space.

In general, trailing space in the origin do not makes sense?
Should I just remove the option ?
WDYT?

@mcollina
Copy link
Member

mcollina commented Jan 8, 2026

Remove it!

@SksOp
Copy link
Author

SksOp commented Jan 8, 2026

Okay I will remove it.

@SksOp
Copy link
Author

SksOp commented Jan 9, 2026

Done

@metcoder95
Copy link
Member

Tests are not happy 😞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants