From 70bae264a047303a718a195622c9be6b5de2cd85 Mon Sep 17 00:00:00 2001 From: Dung Huynh Duc Date: Wed, 23 Oct 2024 21:09:50 +0800 Subject: [PATCH] feat(parser): add support for parsing captures in Hurl output --- src/hurl-parser.ts | 16 ++++++++++++++++ test/hurl-parser.test.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/hurl-parser.ts b/src/hurl-parser.ts index a934ad6..38f6c3f 100644 --- a/src/hurl-parser.ts +++ b/src/hurl-parser.ts @@ -9,6 +9,7 @@ interface ParsedEntry { }; curlCommand?: string; timings?: Record; + captures?: Record; } export interface ParsedHurlOutput { @@ -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")) { @@ -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) { @@ -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; } } diff --git a/test/hurl-parser.test.ts b/test/hurl-parser.test.ts index 5f5d509..cf0d5bd 100644 --- a/test/hurl-parser.test.ts +++ b/test/hurl-parser.test.ts @@ -50,6 +50,7 @@ describe("parseHurlOutput", () => { connect: "2.00 ms", total: "10.00 ms", }, + captures: {}, }); }); @@ -57,4 +58,31 @@ describe("parseHurlOutput", () => { 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' + }); + }); });