Skip to content

Commit b572efa

Browse files
authored
Ignore query in Stoplight endpoint URLs (#20)
* Ignore query in Stoplight endpoint URLs * test: mocking
1 parent b927a11 commit b572efa

File tree

4 files changed

+59
-11
lines changed

4 files changed

+59
-11
lines changed

lib/bundle/stoplight/generator.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,22 @@ StoplightKeyGenerator.prototype.processPath = function (path) {
5454

5555
StoplightKeyGenerator.prototype.extractFilepathFromUrl = function (url) {
5656
let { endpointUrl } = this._opts;
57-
let { href, query } = parse(url, true);
57+
let { pathname, href, query } = parse(url, true);
58+
59+
if (pathname === "/" || pathname === null) {
60+
return url;
61+
}
5862

5963
let mid = query.mid ? `_m${query.mid}` : "";
6064

61-
let [, filepath] = href.split(endpointUrl);
65+
let filepath;
66+
67+
if (endpointUrl instanceof RegExp) {
68+
[, filepath] = pathname.split(endpointUrl);
69+
}
70+
else if (href.indexOf(endpointUrl) === 0) {
71+
filepath = href.slice(endpointUrl.length);
72+
}
6273

6374
if (filepath) {
6475
return filepath + mid;

test/specs/custom-bundling-roots-new-stoplight/custom-bundling-roots-new-stoplight.spec.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ describe("Stoplight-specific defaults", () => {
1616
"http://jakub.stoplight-local.com:8080/api/v1/projects/jakub/my-project/nodes/reference/Book.v1.json": {
1717
title: "Book"
1818
},
19+
"http://jakub.stoplight-local.com:8080/api/v1/projects/jakub/my-project/nodes/reference/Book.v1.json?deref=bundle&mid=1": {
20+
title: "Book mid 1"
21+
},
1922
"http://marc.stoplight-local.com:8080/api/v1/projects/marc/my-project/nodes/models/Street.v1.json": {
20-
title: "Book"
23+
title: "Street"
24+
},
25+
"http://marc.stoplight-local.com:8080/api/v1/projects/marc/my-project/nodes/models/Street.v1.json?mid=1": {
26+
title: "Street mid 1"
2127
},
2228
});
2329

@@ -29,14 +35,19 @@ describe("Stoplight-specific defaults", () => {
2935
book: {
3036
$ref: "http://jakub.stoplight-local.com:8080/api/v1/projects/jakub/my-project/nodes/reference/Book.v1.json"
3137
},
38+
"masked-book": {
39+
$ref: "http://jakub.stoplight-local.com:8080/api/v1/projects/jakub/my-project/nodes/reference/Book.v1.json?deref=bundle&mid=1"
40+
},
3241
street: {
3342
$ref: "http://marc.stoplight-local.com:8080/api/v1/projects/marc/my-project/nodes/models/Street.v1.json"
3443
},
44+
"masked-street": {
45+
$ref: "http://marc.stoplight-local.com:8080/api/v1/projects/marc/my-project/nodes/models/Street.v1.json?mid=1"
46+
},
3547
}
3648
};
3749
});
3850

39-
4051
it("given explicit endpointUrl, should recognize platform references pointing at that entity", async () => {
4152
let defaults = createStoplightDefaults({
4253
cwd,
@@ -54,17 +65,26 @@ describe("Stoplight-specific defaults", () => {
5465
definitions: {
5566
"Book.v1": {
5667
title: "Book"
68+
},
69+
"Book.v1_m1": {
70+
title: "Book mid 1"
5771
}
5872
},
5973
properties: {
6074
book: {
6175
$ref: "#/definitions/Book.v1"
6276
},
77+
"masked-book": {
78+
$ref: "#/definitions/Book.v1_m1"
79+
},
6380
id: {
6481
type: "string"
6582
},
6683
street: {
67-
title: "Book"
84+
title: "Street"
85+
},
86+
"masked-street": {
87+
title: "Street mid 1"
6888
}
6989
}
7090
});
@@ -87,19 +107,31 @@ describe("Stoplight-specific defaults", () => {
87107
"Book.v1": {
88108
title: "Book"
89109
},
110+
"Book.v1_m1": {
111+
title: "Book mid 1"
112+
},
90113
"Street.v1": {
91-
title: "Book"
114+
title: "Street"
115+
},
116+
"Street.v1_m1": {
117+
title: "Street mid 1"
92118
}
93119
},
94120
properties: {
95121
book: {
96122
$ref: "#/definitions/Book.v1"
97123
},
124+
"masked-book": {
125+
$ref: "#/definitions/Book.v1_m1"
126+
},
98127
id: {
99128
type: "string"
100129
},
101130
street: {
102131
$ref: "#/definitions/Street.v1"
132+
},
133+
"masked-street": {
134+
$ref: "#/definitions/Street.v1_m1"
103135
}
104136
}
105137
});

test/utils/setup-http-mocks-karma.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ afterEach(() => {
2222
module.exports = function (mocks) {
2323
fetchMock.config.warnOnFallback = true;
2424
for (const [url, body] of Object.entries(mocks)) {
25-
fetchMock.mock(url.replace(/([^\/])\?/, "$1/?"), {
26-
status: 200,
27-
body,
28-
});
25+
for (const actualUrl of new Set([url.replace(/([^\/])\?/, "$1/?"), url.replace(/([^\/])\?/, "$1?")])) {
26+
fetchMock.mock(actualUrl, {
27+
status: 200,
28+
body,
29+
});
30+
}
2931
}
3032
};

test/utils/setup-http-mocks-node.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ module.exports = function (mocks) {
2020
.persist(true);
2121

2222
if (Object.keys(query).length > 0) {
23-
scope = scope.get(pathname.replace(/\/$/, "") + "/").query(query);
23+
scope = scope.get(
24+
new RegExp(pathname.replace(/\/$/, "").replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&") + "\\/?$")
25+
)
26+
.query(query);
2427
}
2528
else {
2629
scope = scope.get(pathname);

0 commit comments

Comments
 (0)