Skip to content

Commit e2e557f

Browse files
committed
Add path matching support to the websocket rule builder
1 parent 6f97801 commit e2e557f

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

src/mockttp.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,15 @@ export interface Mockttp {
330330
forJsonRpcRequest(match?: { method?: string, params?: any }): RequestRuleBuilder;
331331

332332
/**
333-
* Get a builder for a mock rule that will match all websocket connections.
333+
* Get a builder for a mock rule that will match websocket connections.
334+
*
335+
* This can optionally include a path to match: either a string, or a regular
336+
* expression. Path matching always ignores query parameters. To match query
337+
* parameters, use .withQuery({ a: 'b' }) or withExactQuery('?a=b').
334338
*
335339
* @category Mock websockets
336340
*/
337-
forAnyWebSocket(): WebSocketRuleBuilder;
341+
forAnyWebSocket(url?: string | RegExp): WebSocketRuleBuilder;
338342

339343
/**
340344
* Subscribe to hear about request details as soon as the initial request details
@@ -1033,8 +1037,8 @@ export abstract class AbstractMockttp {
10331037
});
10341038
}
10351039

1036-
forAnyWebSocket(): WebSocketRuleBuilder {
1037-
return new WebSocketRuleBuilder(this.addWebSocketRule);
1040+
forAnyWebSocket(url?: string | RegExp): WebSocketRuleBuilder {
1041+
return new WebSocketRuleBuilder(url, this.addWebSocketRule);
10381042
}
10391043

10401044
}

src/rules/requests/request-rule-builder.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ export class RequestRuleBuilder extends BaseRuleBuilder {
8181
this.matchers.push(new RegexPathMatcher(path));
8282
} else if (typeof path === 'string') {
8383
this.matchers.push(new FlexiblePathMatcher(path));
84+
} else if (path === undefined) {
85+
this.matchers.push(new WildcardMatcher());
86+
} else {
87+
throw new Error('Invalid path argument');
8488
}
8589
}
8690

src/rules/websockets/websocket-rule-builder.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from './websocket-step-definitions';
1717

1818
import { BaseRuleBuilder } from "../base-rule-builder";
19-
import { WildcardMatcher } from "../matchers";
19+
import { FlexiblePathMatcher, RegexPathMatcher, WildcardMatcher } from "../matchers";
2020

2121
/**
2222
* @class WebSocketRuleBuilder
@@ -43,12 +43,25 @@ export class WebSocketRuleBuilder extends BaseRuleBuilder {
4343
* using, not directly. You shouldn't ever need to call this constructor.
4444
*/
4545
constructor(
46+
path: string | RegExp | undefined,
4647
private addRule: (rule: WebSocketRuleData) => Promise<MockedEndpoint>
4748
) {
4849
super();
4950

50-
// By default, websockets just match everything:
51-
this.matchers.push(new WildcardMatcher());
51+
if (path) {
52+
if (path instanceof RegExp) {
53+
this.matchers.push(new RegexPathMatcher(path));
54+
} else if (typeof path === 'string') {
55+
this.matchers.push(new FlexiblePathMatcher(path));
56+
} else if (path === undefined) {
57+
this.matchers.push(new WildcardMatcher());
58+
} else {
59+
throw new Error('Invalid path argument');
60+
}
61+
} else {
62+
// By default, websockets just match everything:
63+
this.matchers.push(new WildcardMatcher());
64+
}
5265
}
5366

5467
private steps: Array<WebSocketStepDefinition> = [];

0 commit comments

Comments
 (0)