Skip to content

Commit e9ea5e4

Browse files
committed
Rename SimplePathMatcher & SimpleStep (previously SimpleHandler)
This is a breaking change only if you're manually building rule structures yourself - there's no change to the chained rule building API (forGet().thenReply(...)) These names sort-of made sense when we had few options, but no longer really do. These are now called FlexiblePathMatcher (give it a path, it will flexibly match it against the relative or absolute or protocol-less URL automatically) and FixedResponseStep(Definition) which does exactly what it says on the tin.
1 parent 8e84cb8 commit e9ea5e4

File tree

5 files changed

+31
-31
lines changed

5 files changed

+31
-31
lines changed

src/rules/matchers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export class PortMatcher extends Serializable implements RequestMatcher {
189189
}
190190
}
191191

192-
export class SimplePathMatcher extends Serializable implements RequestMatcher {
192+
export class FlexiblePathMatcher extends Serializable implements RequestMatcher {
193193
readonly type = 'simple-path';
194194

195195
constructor(
@@ -619,7 +619,7 @@ export const MatcherLookup = {
619619
'host': HostMatcher,
620620
'hostname': HostnameMatcher,
621621
'port': PortMatcher,
622-
'simple-path': SimplePathMatcher,
622+
'simple-path': FlexiblePathMatcher,
623623
'regex-path': RegexPathMatcher,
624624
'regex-url': RegexUrlMatcher,
625625
'header': HeaderMatcher,

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Headers, CompletedRequest, Method, MockedEndpoint, Trailers } from "../
66
import type { RequestRuleData } from "./request-rule";
77

88
import {
9-
SimpleStepDefinition,
9+
FixedResponseStepDefinition,
1010
PassThroughStepDefinition,
1111
CallbackStepDefinition,
1212
CallbackResponseResult,
@@ -23,7 +23,7 @@ import {
2323
} from "./request-step-definitions";
2424
import { byteLength } from "../../util/util";
2525
import { BaseRuleBuilder } from "../base-rule-builder";
26-
import { MethodMatcher, RegexPathMatcher, SimplePathMatcher, WildcardMatcher } from "../matchers";
26+
import { MethodMatcher, RegexPathMatcher, FlexiblePathMatcher, WildcardMatcher } from "../matchers";
2727

2828
/**
2929
* @class RequestRuleBuilder
@@ -76,7 +76,7 @@ export class RequestRuleBuilder extends BaseRuleBuilder {
7676
if (path instanceof RegExp) {
7777
this.matchers.push(new RegexPathMatcher(path));
7878
} else if (typeof path === 'string') {
79-
this.matchers.push(new SimplePathMatcher(path));
79+
this.matchers.push(new FlexiblePathMatcher(path));
8080
}
8181
}
8282

@@ -151,7 +151,7 @@ export class RequestRuleBuilder extends BaseRuleBuilder {
151151
trailers = headersOrTrailers as Trailers | undefined;
152152
}
153153

154-
this.steps.push(new SimpleStepDefinition(
154+
this.steps.push(new FixedResponseStepDefinition(
155155
status,
156156
statusMessage,
157157
data,
@@ -196,7 +196,7 @@ export class RequestRuleBuilder extends BaseRuleBuilder {
196196
// connection after the response is sent, which can confuse clients.
197197
}, headers);
198198

199-
this.steps.push(new SimpleStepDefinition(status, undefined, jsonData, headers));
199+
this.steps.push(new FixedResponseStepDefinition(status, undefined, jsonData, headers));
200200

201201
const rule: RequestRuleData = {
202202
...this.buildBaseRuleData(),

src/rules/requests/request-step-definitions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ function validateCustomHeaders(
251251
}
252252
}
253253

254-
export class SimpleStepDefinition extends Serializable implements RequestStepDefinition {
254+
export class FixedResponseStepDefinition extends Serializable implements RequestStepDefinition {
255255

256256
readonly type = 'simple';
257257
static readonly isFinal = true;
@@ -1058,7 +1058,7 @@ export class DelayStepDefinition extends Serializable implements RequestStepDefi
10581058
}
10591059

10601060
export const StepDefinitionLookup = {
1061-
'simple': SimpleStepDefinition,
1061+
'simple': FixedResponseStepDefinition,
10621062
'callback': CallbackStepDefinition,
10631063
'stream': StreamStepDefinition,
10641064
'file': FileStepDefinition,

src/rules/requests/request-steps.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ import {
113113
SerializedPassThroughData,
114114
SerializedStreamStepData,
115115
SERIALIZED_OMIT,
116-
SimpleStepDefinition,
116+
FixedResponseStepDefinition,
117117
StreamStepDefinition,
118118
TimeoutStepDefinition,
119119
DelayStepDefinition
@@ -165,7 +165,7 @@ export interface RequestStepOptions {
165165
emitEventCallback?: (type: string, event: unknown) => void;
166166
}
167167

168-
export class SimpleStep extends SimpleStepDefinition {
168+
export class FixedResponseStep extends FixedResponseStepDefinition {
169169
async handle(_request: OngoingRequest, response: OngoingResponse) {
170170
if (this.headers) dropDefaultHeaders(response);
171171
writeHead(response, this.status, this.statusMessage, this.headers);
@@ -1405,7 +1405,7 @@ export class DelayStep extends DelayStepDefinition {
14051405
}
14061406

14071407
export const StepLookup: typeof StepDefinitionLookup = {
1408-
'simple': SimpleStep,
1408+
'simple': FixedResponseStep,
14091409
'callback': CallbackStep,
14101410
'stream': StreamStep,
14111411
'file': FileStep,

test/integration/manual-rule-building.spec.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ describe("Mockttp rule building", function () {
1212

1313
it("should allow manually adding a single rule", async () => {
1414
await server.addRequestRules({
15-
matchers: [new matchers.SimplePathMatcher('/endpoint')],
16-
steps: [new requestSteps.SimpleStepDefinition(200, '', 'mock response')]
15+
matchers: [new matchers.FlexiblePathMatcher('/endpoint')],
16+
steps: [new requestSteps.FixedResponseStepDefinition(200, '', 'mock response')]
1717
});
1818

1919
let response = await fetch(server.urlFor('/endpoint'));
@@ -27,21 +27,21 @@ describe("Mockttp rule building", function () {
2727

2828
const rule = await server.addRequestRules({
2929
id: manualId,
30-
matchers: [new matchers.SimplePathMatcher('/endpoint')],
31-
steps: [new requestSteps.SimpleStepDefinition(200, '', 'mock response')]
30+
matchers: [new matchers.FlexiblePathMatcher('/endpoint')],
31+
steps: [new requestSteps.FixedResponseStepDefinition(200, '', 'mock response')]
3232
});
3333

3434
expect(rule[0].id).to.equal(manualId);
3535
});
3636

3737
it("should allow repeatedly adding rules", async () => {
3838
await server.addRequestRules({
39-
matchers: [new matchers.SimplePathMatcher('/endpoint')],
40-
steps: [new requestSteps.SimpleStepDefinition(200, '', 'first mock response')]
39+
matchers: [new matchers.FlexiblePathMatcher('/endpoint')],
40+
steps: [new requestSteps.FixedResponseStepDefinition(200, '', 'first mock response')]
4141
});
4242
await server.addRequestRules({
43-
matchers: [new matchers.SimplePathMatcher('/endpoint')],
44-
steps: [new requestSteps.SimpleStepDefinition(200, '', 'second mock response')]
43+
matchers: [new matchers.FlexiblePathMatcher('/endpoint')],
44+
steps: [new requestSteps.FixedResponseStepDefinition(200, '', 'second mock response')]
4545
});
4646

4747
let firstResponse = await fetch(server.urlFor('/endpoint'));
@@ -55,12 +55,12 @@ describe("Mockttp rule building", function () {
5555

5656
it("should allow completely replacing rules", async () => {
5757
await server.addRequestRules({
58-
matchers: [new matchers.SimplePathMatcher('/endpoint')],
59-
steps: [new requestSteps.SimpleStepDefinition(200, '', 'original mock response')]
58+
matchers: [new matchers.FlexiblePathMatcher('/endpoint')],
59+
steps: [new requestSteps.FixedResponseStepDefinition(200, '', 'original mock response')]
6060
});
6161
await server.setRequestRules({
62-
matchers: [new matchers.SimplePathMatcher('/endpoint')],
63-
steps: [new requestSteps.SimpleStepDefinition(200, '', 'replacement mock response')]
62+
matchers: [new matchers.FlexiblePathMatcher('/endpoint')],
63+
steps: [new requestSteps.FixedResponseStepDefinition(200, '', 'replacement mock response')]
6464
});
6565

6666
let firstResponse = await fetch(server.urlFor('/endpoint'));
@@ -95,10 +95,10 @@ describe("Mockttp rule building", function () {
9595

9696
it("should allow manually adding a multi-step rule", async () => {
9797
await server.addRequestRules({
98-
matchers: [new matchers.SimplePathMatcher('/endpoint')],
98+
matchers: [new matchers.FlexiblePathMatcher('/endpoint')],
9999
steps: [
100100
new requestSteps.DelayStepDefinition(10),
101-
new requestSteps.SimpleStepDefinition(200, '', 'mock response')
101+
new requestSteps.FixedResponseStepDefinition(200, '', 'mock response')
102102
]
103103
});
104104

@@ -112,15 +112,15 @@ describe("Mockttp rule building", function () {
112112
return expect((async () => { // Funky setup to handle sync & async failure for node & browser
113113
await server.addRequestRules({
114114
matchers: [],
115-
steps: [new requestSteps.SimpleStepDefinition(200, 'mock response')]
115+
steps: [new requestSteps.FixedResponseStepDefinition(200, 'mock response')]
116116
})
117117
})()).to.be.rejectedWith('Cannot create a rule without at least one matcher');
118118
});
119119

120120
it("should reject rules with no steps value", async () => {
121121
return expect((async () => { // Funky setup to handle sync & async failure for node & browser
122122
await server.addRequestRules({
123-
matchers: [new matchers.SimplePathMatcher('/')],
123+
matchers: [new matchers.FlexiblePathMatcher('/')],
124124
steps: null as any
125125
})
126126
})()).to.be.rejectedWith('Cannot create a rule with no steps');
@@ -129,7 +129,7 @@ describe("Mockttp rule building", function () {
129129
it("should reject rules with an empty steps list", async () => {
130130
return expect((async () => { // Funky setup to handle sync & async failure for node & browser
131131
await server.addRequestRules({
132-
matchers: [new matchers.SimplePathMatcher('/')],
132+
matchers: [new matchers.FlexiblePathMatcher('/')],
133133
steps: []
134134
})
135135
})()).to.be.rejectedWith('Cannot create a rule with no steps');
@@ -138,9 +138,9 @@ describe("Mockttp rule building", function () {
138138
it("should reject rules with non-final final-only steps", async () => {
139139
return expect((async () => { // Funky setup to handle sync & async failure for node & browser
140140
await server.addRequestRules({
141-
matchers: [new matchers.SimplePathMatcher('/endpoint')],
141+
matchers: [new matchers.FlexiblePathMatcher('/endpoint')],
142142
steps: [
143-
new requestSteps.SimpleStepDefinition(200),
143+
new requestSteps.FixedResponseStepDefinition(200),
144144
new requestSteps.DelayStepDefinition(100)
145145
]
146146
});

0 commit comments

Comments
 (0)