From 2c8ab249bfe8c97a139263638da545a8ce5570f1 Mon Sep 17 00:00:00 2001 From: Dung Huynh Duc Date: Sat, 12 Oct 2024 21:56:10 +0800 Subject: [PATCH] test: add unit tests for hurl parse utils --- test/hurl-parser.test.ts | 60 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 test/hurl-parser.test.ts diff --git a/test/hurl-parser.test.ts b/test/hurl-parser.test.ts new file mode 100644 index 0000000..861e2c8 --- /dev/null +++ b/test/hurl-parser.test.ts @@ -0,0 +1,60 @@ +import { describe, it, expect } from "vitest"; +import { parseHurlOutput } from "../src/hurl-parser"; + +describe('parseHurlOutput', () => { + it('should parse a simple GET request', () => { + const stderr = ` +* Executing entry 1 +* Request: +* GET https://example.com/api +* curl 'https://example.com/api' +> GET /api HTTP/1.1 +> Host: example.com +> User-Agent: hurl/1.0 +> Accept: */* +> +< HTTP/1.1 200 OK +< Content-Type: application/json +< Content-Length: 123 +< +* Timings: +* namelookup: 1000 µs +* connect: 2000 µs +* total: 10000 µs +* +`; + const stdout = '{"message": "Hello, World!"}'; + + const result = parseHurlOutput(stderr, stdout); + + expect(result.entries).toHaveLength(1); + expect(result.entries[0]).toEqual({ + requestMethod: 'GET', + requestUrl: '/api', + requestHeaders: { + 'Host': 'example.com', + 'User-Agent': 'hurl/1.0', + 'Accept': '*/*' + }, + response: { + status: 'HTTP/1.1 200 OK', + headers: { + 'Content-Type': 'application/json', + 'Content-Length': '123' + }, + body: '{"message": "Hello, World!"}' + }, + curlCommand: "curl 'https://example.com/api'", + timings: { + namelookup: '1.00 ms', + connect: '2.00 ms', + total: '10.00 ms' + } + }); + }); + + it('should handle empty input', () => { + const result = parseHurlOutput('', ''); + expect(result.entries).toHaveLength(0); + }); +});