Skip to content

Commit 3533820

Browse files
parse {placeholders} in request URLs (#64) (#65)
(cherry picked from commit d81ce6b) Co-authored-by: Miguel Grinberg <miguel.grinberg@gmail.com>
1 parent 328b554 commit 3533820

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/parse.ts

+11
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,22 @@ function parseCommand(source: string, options: ParseOptions) {
142142
skip(" ", "\n");
143143
const urlStart = index;
144144
until("{", "\n");
145+
if (source[index] == "{" && source[index + 1].match(/[a-z]/i)) {
146+
// this is a placeholder element inside the URL (as used in doc examples),
147+
// so we continue scanning
148+
index++;
149+
until("{", "\n");
150+
}
145151

146152
data.url = source.slice(urlStart, index).trim();
147153
if (data.url[0] != "/") {
148154
data.url = "/" + data.url;
149155
}
156+
data.url = data.url
157+
// replaces { with %7B (braces in many doc examples are not URIencoded)
158+
.replace(/{/g, "%7B")
159+
// replaces } with %7D
160+
.replace(/}/g, "%7D");
150161
const parsedUrl = new URL(`http://localhost${data.url}`);
151162
data.rawPath =
152163
parsedUrl.pathname != "/"

tests/parse.test.ts

+22-10
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ describe("parse", () => {
4242
});
4343
});
4444

45+
it("parses search POST request given in a single line", async () => {
46+
const req = await parseRequest(`POST /my-index/_search { "size": 5 }`);
47+
expect(req).toMatchObject({
48+
api: "search",
49+
params: { index: "my-index" },
50+
method: "POST",
51+
url: "/my-index/_search",
52+
path: "/my-index/_search",
53+
body: { size: 5 },
54+
});
55+
});
56+
4557
it("parses a complex sequence of requests", async () => {
4658
const reqs = await parseRequests(`PUT /customer/_doc/1?foo=bar
4759
{
@@ -73,11 +85,11 @@ POST /_bulk?foo=bar
7385
{ "name": "John\\nDoe" }
7486
7587
76-
GET /customer/_doc/1
88+
GET /{customer}/_doc/1
7789
7890
POST _nodes/reload_secure_settings\n{\n "reload_secure_settings": "s3cr3t" <1>\n}
7991
80-
GET my_index/_analyze <3>\n{\n "field": "text",\n "text": "The quick Brown Foxes."\n}
92+
GET {my_index}/_analyze <3>\n{\n "field": "text",\n "text": "The quick Brown Foxes."\n}
8193
8294
POST\n_ml/anomaly_detectors/it_ops_new_logs/model_snapshots/1491852978/_update\n{\n "description": "Snapshot 1",\n "retain": true\n}
8395
`);
@@ -152,11 +164,11 @@ POST\n_ml/anomaly_detectors/it_ops_new_logs/model_snapshots/1491852978/_update\n
152164
});
153165
expect(reqs[7]).toMatchObject({
154166
api: "get",
155-
params: { index: "customer", id: "1" },
167+
params: { index: "{customer}", id: "1" },
156168
method: "GET",
157-
url: "/customer/_doc/1",
158-
path: "/customer/_doc/1",
159-
rawPath: "/customer/_doc/1",
169+
url: "/%7Bcustomer%7D/_doc/1",
170+
path: "/{customer}/_doc/1",
171+
rawPath: "/%7Bcustomer%7D/_doc/1",
160172
});
161173
expect(reqs[8]).toMatchObject({
162174
api: "nodes.reload_secure_settings",
@@ -169,11 +181,11 @@ POST\n_ml/anomaly_detectors/it_ops_new_logs/model_snapshots/1491852978/_update\n
169181
});
170182
expect(reqs[9]).toMatchObject({
171183
api: "indices.analyze",
172-
params: { index: "my_index" },
184+
params: { index: "{my_index}" },
173185
method: "GET",
174-
url: "/my_index/_analyze",
175-
path: "/my_index/_analyze",
176-
rawPath: "/my_index/_analyze",
186+
url: "/%7Bmy_index%7D/_analyze",
187+
path: "/{my_index}/_analyze",
188+
rawPath: "/%7Bmy_index%7D/_analyze",
177189
body: { field: "text", text: "The quick Brown Foxes." },
178190
});
179191
expect(reqs[10]).toMatchObject({

0 commit comments

Comments
 (0)