Skip to content

Commit 3da7048

Browse files
authored
fix(WebSocketHandler): add public test() method (#2727)
1 parent d814fc8 commit 3da7048

2 files changed

Lines changed: 94 additions & 7 deletions

File tree

src/core/handlers/WebSocketHandler.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,61 @@ describe('parse', () => {
125125
})
126126
})
127127
})
128+
129+
describe('test', () => {
130+
it('returns true for a matching string', () => {
131+
expect(
132+
new WebSocketHandler('ws://localhost/ws').test('ws://localhost/ws'),
133+
).toBe(true)
134+
})
135+
136+
it('returns false for a non-matching string', () => {
137+
expect(
138+
new WebSocketHandler('ws://localhost/ws').test('ws://localhost/other'),
139+
).toBe(false)
140+
})
141+
142+
it('returns true for a relative matching string', () => {
143+
expect(
144+
new WebSocketHandler('ws://localhost/ws').test('/ws', {
145+
baseUrl: 'ws://localhost',
146+
}),
147+
).toBe(true)
148+
})
149+
150+
it('returns false for a relative non-matching string', () => {
151+
expect(
152+
new WebSocketHandler('ws://localhost/ws').test('/other', {
153+
baseUrl: 'ws://localhost',
154+
}),
155+
).toBe(false)
156+
})
157+
158+
it('returns true for a matching URL', () => {
159+
expect(
160+
new WebSocketHandler('ws://localhost/ws').test(
161+
new URL('ws://localhost/ws'),
162+
),
163+
).toBe(true)
164+
})
165+
166+
it('returns false for a non-matching URL', () => {
167+
expect(
168+
new WebSocketHandler('ws://localhost/ws').test(
169+
new URL('ws://localhost/other'),
170+
),
171+
).toBe(false)
172+
})
173+
174+
it('returns true for a matching HTTP url string', () => {
175+
expect(
176+
new WebSocketHandler('ws://localhost/ws').test('http://localhost/ws'),
177+
).toBe(true)
178+
})
179+
180+
it('returns false for a non-matching HTTP url string', () => {
181+
expect(
182+
new WebSocketHandler('ws://localhost/ws').test('http://localhost/other'),
183+
).toBe(false)
184+
})
185+
})

src/core/handlers/WebSocketHandler.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class WebSocketHandler {
5757
}
5858

5959
public parse(args: {
60-
url: URL
60+
url: string | URL
6161
resolutionContext?: WebSocketResolutionContext
6262
}): WebSocketHandlerParsedResult {
6363
const clientUrl = new URL(args.url)
@@ -90,22 +90,26 @@ export class WebSocketHandler {
9090
}
9191

9292
public predicate(args: {
93-
url: URL
93+
url: string | URL
9494
parsedResult: WebSocketHandlerParsedResult
9595
}): boolean {
9696
return args.parsedResult.match.matches
9797
}
9898

99+
public test(
100+
url: string | URL,
101+
resolutionContext?: WebSocketResolutionContext & { strict?: boolean },
102+
): boolean {
103+
return this.#match(url, resolutionContext) != null
104+
}
105+
99106
public async run(
100107
connection: WebSocketConnectionData,
101108
resolutionContext?: WebSocketResolutionContext,
102109
): Promise<WebSocketHandlerConnection | null> {
103-
const parsedResult = this.parse({
104-
url: connection.client.url,
105-
resolutionContext,
106-
})
110+
const parsedResult = this.#match(connection.client.url, resolutionContext)
107111

108-
if (!this.predicate({ url: connection.client.url, parsedResult })) {
112+
if (parsedResult == null) {
109113
return null
110114
}
111115

@@ -125,6 +129,31 @@ export class WebSocketHandler {
125129
return resolvedConnection
126130
}
127131

132+
#match(
133+
url: string | URL,
134+
resolutionContext?: WebSocketResolutionContext & { strict?: boolean },
135+
): WebSocketHandlerParsedResult | null {
136+
const resolvedUrl = this.#resolveWebSocketUrl(
137+
url.toString(),
138+
resolutionContext?.baseUrl,
139+
)
140+
const parsedResult = this.parse({
141+
url: resolvedUrl,
142+
resolutionContext,
143+
})
144+
145+
if (
146+
this.predicate({
147+
url,
148+
parsedResult,
149+
})
150+
) {
151+
return parsedResult
152+
}
153+
154+
return null
155+
}
156+
128157
protected [kConnect](connection: WebSocketHandlerConnection): boolean {
129158
// Support `event.stopPropagation()` for various client/server events.
130159
connection.client.addEventListener(

0 commit comments

Comments
 (0)