Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 8.16] parse {placeholders} in request URLs #65

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,22 @@ function parseCommand(source: string, options: ParseOptions) {
skip(" ", "\n");
const urlStart = index;
until("{", "\n");
if (source[index] == "{" && source[index + 1].match(/[a-z]/i)) {
// this is a placeholder element inside the URL (as used in doc examples),
// so we continue scanning
index++;
until("{", "\n");
}

data.url = source.slice(urlStart, index).trim();
if (data.url[0] != "/") {
data.url = "/" + data.url;
}
data.url = data.url
// replaces { with %7B (braces in many doc examples are not URIencoded)
.replace(/{/g, "%7B")
// replaces } with %7D
.replace(/}/g, "%7D");
const parsedUrl = new URL(`http://localhost${data.url}`);
data.rawPath =
parsedUrl.pathname != "/"
Expand Down
32 changes: 22 additions & 10 deletions tests/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ describe("parse", () => {
});
});

it("parses search POST request given in a single line", async () => {
const req = await parseRequest(`POST /my-index/_search { "size": 5 }`);
expect(req).toMatchObject({
api: "search",
params: { index: "my-index" },
method: "POST",
url: "/my-index/_search",
path: "/my-index/_search",
body: { size: 5 },
});
});

it("parses a complex sequence of requests", async () => {
const reqs = await parseRequests(`PUT /customer/_doc/1?foo=bar
{
Expand Down Expand Up @@ -73,11 +85,11 @@ POST /_bulk?foo=bar
{ "name": "John\\nDoe" }


GET /customer/_doc/1
GET /{customer}/_doc/1

POST _nodes/reload_secure_settings\n{\n "reload_secure_settings": "s3cr3t" <1>\n}

GET my_index/_analyze <3>\n{\n "field": "text",\n "text": "The quick Brown Foxes."\n}
GET {my_index}/_analyze <3>\n{\n "field": "text",\n "text": "The quick Brown Foxes."\n}

POST\n_ml/anomaly_detectors/it_ops_new_logs/model_snapshots/1491852978/_update\n{\n "description": "Snapshot 1",\n "retain": true\n}
`);
Expand Down Expand Up @@ -152,11 +164,11 @@ POST\n_ml/anomaly_detectors/it_ops_new_logs/model_snapshots/1491852978/_update\n
});
expect(reqs[7]).toMatchObject({
api: "get",
params: { index: "customer", id: "1" },
params: { index: "{customer}", id: "1" },
method: "GET",
url: "/customer/_doc/1",
path: "/customer/_doc/1",
rawPath: "/customer/_doc/1",
url: "/%7Bcustomer%7D/_doc/1",
path: "/{customer}/_doc/1",
rawPath: "/%7Bcustomer%7D/_doc/1",
});
expect(reqs[8]).toMatchObject({
api: "nodes.reload_secure_settings",
Expand All @@ -169,11 +181,11 @@ POST\n_ml/anomaly_detectors/it_ops_new_logs/model_snapshots/1491852978/_update\n
});
expect(reqs[9]).toMatchObject({
api: "indices.analyze",
params: { index: "my_index" },
params: { index: "{my_index}" },
method: "GET",
url: "/my_index/_analyze",
path: "/my_index/_analyze",
rawPath: "/my_index/_analyze",
url: "/%7Bmy_index%7D/_analyze",
path: "/{my_index}/_analyze",
rawPath: "/%7Bmy_index%7D/_analyze",
body: { field: "text", text: "The quick Brown Foxes." },
});
expect(reqs[10]).toMatchObject({
Expand Down
Loading