Skip to content

Commit

Permalink
feat(parser): add support for parsing captures in Hurl output
Browse files Browse the repository at this point in the history
  • Loading branch information
jellydn committed Oct 23, 2024
1 parent 22cfc6f commit 70bae26
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/hurl-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface ParsedEntry {
};
curlCommand?: string;
timings?: Record<string, string>;
captures?: Record<string, string>;
}

export interface ParsedHurlOutput {
Expand Down Expand Up @@ -56,6 +57,7 @@ export function parseHurlOutput(
let currentEntry: ParsedEntry | null = null;
let isResponseHeader = false;
let isTimings = false;
let isCaptures = false;

for (const line of lines) {
if (line.startsWith("* Executing entry")) {
Expand All @@ -72,9 +74,11 @@ export function parseHurlOutput(
body: "",
},
timings: {},
captures: {},
};
isResponseHeader = false;
isTimings = false;
isCaptures = false;
} else if (line.startsWith("* Request:")) {
const match = line.match(/\* Request:\s*\* (\w+) (.*)/);
if (match && currentEntry) {
Expand Down Expand Up @@ -131,6 +135,18 @@ export function parseHurlOutput(
if (currentEntry?.timings) {
currentEntry.timings = formatTimings(currentEntry.timings);
}
} else if (line.startsWith("* Captures:")) {
isCaptures = true;
if (currentEntry && !currentEntry.captures) {
currentEntry.captures = {};
}
} else if (isCaptures && line.trim() !== "") {
const [key, value] = line.slice(2).split(":").map((s) => s.trim());
if (currentEntry?.captures && key && value) {
currentEntry.captures[key] = value;
}
} else if (isCaptures && line.trim() === "") {
isCaptures = false;
}
}

Expand Down
28 changes: 28 additions & 0 deletions test/hurl-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,39 @@ describe("parseHurlOutput", () => {
connect: "2.00 ms",
total: "10.00 ms",
},
captures: {},
});
});

it("should handle empty input", () => {
const result = parseHurlOutput("", "");
expect(result.entries).toHaveLength(0);
});

it("should parse captures", () => {
const stderr = `
* ------------------------------------------------------------------------------
* Executing entry 1
* Request:
* GET https://example.com/api
* Response:
< HTTP/1.1 200 OK
< Content-Type: application/json
* Response body:
{"id": "12345", "name": "Example"}
* Captures:
* id: 12345
* name: Example
* ------------------------------------------------------------------------------
`;
const stdout = '{"id": "12345", "name": "Example"}';

const result = parseHurlOutput(stderr, stdout);

expect(result.entries).toHaveLength(1);
expect(result.entries[0].captures).toEqual({
id: '12345',
name: 'Example'
});
});
});

0 comments on commit 70bae26

Please sign in to comment.