From 69a81babb8dc0919705be9ab6ee461b756d85706 Mon Sep 17 00:00:00 2001 From: Matt Turner Date: Sat, 7 Sep 2024 14:03:38 +0100 Subject: [PATCH] Add more tests (#8) --- tests/RequestDefinitionBuilder.test.ts | 50 +++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/tests/RequestDefinitionBuilder.test.ts b/tests/RequestDefinitionBuilder.test.ts index d5ee71b..4a2d789 100644 --- a/tests/RequestDefinitionBuilder.test.ts +++ b/tests/RequestDefinitionBuilder.test.ts @@ -1,5 +1,5 @@ import { RequestDefinitionBuilder } from '../src/RequestDefinitionBuilder'; -import { Item, ItemGroup, Collection, Url } from 'postman-collection'; +import { Item, ItemGroup, Collection, Url, Header, RequestBody } from 'postman-collection'; import { readFileSync } from 'fs'; describe('Request Definition Builder', () => { @@ -21,4 +21,52 @@ describe('Request Definition Builder', () => { expect(actual).toBe('\n\nGET host.com/\n ?color=red'); }); + + test('Header is added', () => { + getRequest.request.headers.clear(); + const header = new Header('option'); + header.key = 'SomeHeader'; + header.value = 'SomeValue'; + getRequest.request.headers.add(header); + + const target = new RequestDefinitionBuilder() + .from(getRequest) + .appendHeaders(); + + const actual = target.toString(); + + expect(actual).toBe('\nSomeHeader: SomeValue'); + }); + + test('Multiple headers are added', () => { + getRequest.request.headers.clear(); + const header1 = new Header('option'); + header1.key = 'SomeHeader1'; + header1.value = 'SomeValue1'; + getRequest.request.headers.add(header1); + const header2 = new Header('option'); + header2.key = 'SomeHeader2'; + header2.value = 'SomeValue2'; + getRequest.request.headers.add(header2); + + const target = new RequestDefinitionBuilder() + .from(getRequest) + .appendHeaders(); + + const actual = target.toString(); + + expect(actual).toBe('\nSomeHeader1: SomeValue1\nSomeHeader2: SomeValue2'); + }); + + test('Body is added with default content-type header', () => { + getRequest.request.body = new RequestBody({ mode: 'json', raw: '{ "some": "value" }' }); + + const target = new RequestDefinitionBuilder() + .from(getRequest) + .appendBody(); + + const actual = target.toString(); + + expect(actual).toBe('\nContent-Type: application/json\n\n{ "some": "value" }'); + }); }); \ No newline at end of file