Skip to content

Commit 58dcb7e

Browse files
committed
fix: handle missing leading slash in path
1 parent df6a773 commit 58dcb7e

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

src/__tests__/fixtures/request-parameters-path/baseline.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"mockDetails": {
66
"interactionDescription": "should error when path does not match integer spec",
77
"interactionState": "[none]",
8-
"location": "[root].interactions[3].request.path",
8+
"location": "[root].interactions[4].request.path",
99
"value": "/integer/abc"
1010
},
1111
"specDetails": {
@@ -90,7 +90,7 @@
9090
"mockDetails": {
9191
"interactionDescription": "should error when path does not match boolean spec",
9292
"interactionState": "[none]",
93-
"location": "[root].interactions[4].request.path",
93+
"location": "[root].interactions[5].request.path",
9494
"value": "/boolean/abc"
9595
},
9696
"specDetails": {
@@ -175,7 +175,7 @@
175175
"mockDetails": {
176176
"interactionDescription": "should error when path and method does not exist in spec",
177177
"interactionState": "[none]",
178-
"location": "[root].interactions[7].request.path",
178+
"location": "[root].interactions[8].request.path",
179179
"value": "/does-not-exist"
180180
},
181181
"specDetails": {
@@ -260,7 +260,7 @@
260260
"mockDetails": {
261261
"interactionDescription": "should error when path does not exist in spec",
262262
"interactionState": "[none]",
263-
"location": "[root].interactions[8].request.path",
263+
"location": "[root].interactions[9].request.path",
264264
"value": "/integer/1"
265265
},
266266
"specDetails": {

src/__tests__/fixtures/request-parameters-path/pact.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@
3232
"status": 200
3333
}
3434
},
35+
{
36+
"description": "should pass when path does not start with /",
37+
"request": {
38+
"method": "GET",
39+
"path": "integer/1"
40+
},
41+
"response": {
42+
"status": 200
43+
}
44+
},
3545
{
3646
"description": "should error when path does not match integer spec",
3747
"request": {

src/__tests__/fixtures/request-parameters-path/results.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"mockDetails": {
66
"interactionDescription": "should error when path does not match integer spec",
77
"interactionState": "[none]",
8-
"location": "[root].interactions[3].request.path",
8+
"location": "[root].interactions[4].request.path",
99
"value": "/integer/abc"
1010
},
1111
"specDetails": {
@@ -38,7 +38,7 @@
3838
"mockDetails": {
3939
"interactionDescription": "should error when path does not match boolean spec",
4040
"interactionState": "[none]",
41-
"location": "[root].interactions[4].request.path",
41+
"location": "[root].interactions[5].request.path",
4242
"value": "/boolean/abc"
4343
},
4444
"specDetails": {
@@ -71,7 +71,7 @@
7171
"mockDetails": {
7272
"interactionDescription": "should error when path does not match object spec",
7373
"interactionState": "[none]",
74-
"location": "[root].interactions[5].request.path",
74+
"location": "[root].interactions[6].request.path",
7575
"value": "/exploded-object/string=abc,number=abc"
7676
},
7777
"specDetails": {
@@ -114,7 +114,7 @@
114114
"mockDetails": {
115115
"interactionDescription": "should error when path does not match object spec",
116116
"interactionState": "[none]",
117-
"location": "[root].interactions[6].request.path",
117+
"location": "[root].interactions[7].request.path",
118118
"value": "/exploded-object/string=abc,number=abc"
119119
},
120120
"specDetails": {
@@ -157,7 +157,7 @@
157157
"mockDetails": {
158158
"interactionDescription": "should error when path and method does not exist in spec",
159159
"interactionState": "[none]",
160-
"location": "[root].interactions[7].request.path",
160+
"location": "[root].interactions[8].request.path",
161161
"value": "/does-not-exist"
162162
},
163163
"specDetails": {
@@ -173,7 +173,7 @@
173173
"mockDetails": {
174174
"interactionDescription": "should error when path does not exist in spec",
175175
"interactionState": "[none]",
176-
"location": "[root].interactions[8].request.path",
176+
"location": "[root].interactions[9].request.path",
177177
"value": "/integer/1"
178178
},
179179
"specDetails": {

src/compare/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export class Comparator {
6060

6161
for (const [index, interaction] of parsedPact.interactions.entries()) {
6262
const { method, path, query } = interaction.request;
63+
const pathWithLeadingSlash = path.startsWith("/") ? path : `/${path}`;
6364
// in pact, query is either a string or an object of only one level deep
6465
const stringQuery =
6566
typeof query === "string"
@@ -75,7 +76,7 @@ export class Comparator {
7576
.substring(1);
7677
const route = this.#router.find(
7778
method.toUpperCase() as HTTPMethod,
78-
[path, stringQuery].filter(Boolean).join("?"),
79+
[pathWithLeadingSlash, stringQuery].filter(Boolean).join("?"),
7980
);
8081

8182
if (!route) {

0 commit comments

Comments
 (0)