From 01158b81f78a2edb96554a6a245d32e0e74e09a2 Mon Sep 17 00:00:00 2001 From: Matt Turner Date: Sun, 8 Sep 2024 14:07:43 +0100 Subject: [PATCH] Fix test script and duplicate header (#16) --- package.json | 2 +- src/RequestDefinitionBuilder.ts | 12 ++-- src/tests/RequestDefinitionBuilder.test.ts | 64 +++++++++++++++++++++- 3 files changed, 71 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 18e636b..fe4661e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "httpyac-import", - "version": "0.3.5", + "version": "0.3.6", "description": "CLI to convert Postman collection to httpyac files", "homepage": "https://github.com/matthewturner/httpyac-import", "repository": { diff --git a/src/RequestDefinitionBuilder.ts b/src/RequestDefinitionBuilder.ts index 56066df..6609da9 100644 --- a/src/RequestDefinitionBuilder.ts +++ b/src/RequestDefinitionBuilder.ts @@ -31,7 +31,7 @@ export class RequestDefinitionBuilder { return this; } - appendPreRequestScript() : RequestDefinitionBuilder { + appendPreRequestScript(): RequestDefinitionBuilder { const preRequestTest = this._item.events.find(e => e.listen == 'prerequest', null); if (preRequestTest === undefined) { @@ -73,7 +73,7 @@ export class RequestDefinitionBuilder { continue; } - if (statement.indexOf('pm.response.to.have.status') >= 0 ){ + if (statement.indexOf('pm.response.to.have.status') >= 0) { statusCode = this.statusCodeFor(statement); continue; } @@ -116,7 +116,6 @@ export class RequestDefinitionBuilder { this._definition += '\n'; this._definition += '// TODO: Fixup Postman test script\n'; this._definition += `// ${test.script.exec.join('\n//')}`; - this._definition += '\n}}'; } if (!handled || idProperty != '' || etagProperty != '') { @@ -159,8 +158,11 @@ export class RequestDefinitionBuilder { return this; } - this._definition += '\n'; - this._definition += 'Content-Type: application/json'; + if (!this._item.request.headers.all().find(x => x.key == 'Content-Type')) { + this._definition += '\n'; + this._definition += 'Content-Type: application/json'; + } + this._definition += '\n\n'; this._definition += this._item.request.body.toString(); diff --git a/src/tests/RequestDefinitionBuilder.test.ts b/src/tests/RequestDefinitionBuilder.test.ts index 33fd182..2e0c422 100644 --- a/src/tests/RequestDefinitionBuilder.test.ts +++ b/src/tests/RequestDefinitionBuilder.test.ts @@ -1,5 +1,5 @@ import { RequestDefinitionBuilder } from '../RequestDefinitionBuilder'; -import { Item, ItemGroup, Collection, Url, Header, RequestBody } from 'postman-collection'; +import { Item, ItemGroup, Collection, Url, Header, RequestBody, Event } from 'postman-collection'; import { readFileSync } from 'fs'; describe('Request Definition Builder', () => { @@ -69,4 +69,66 @@ describe('Request Definition Builder', () => { expect(actual).toBe('\nContent-Type: application/json\n\n{ "some": "value" }'); }); + + test('Body is added with single content-type header', () => { + getRequest.request.body = new RequestBody({ mode: 'json', raw: '{ "some": "value" }' }); + getRequest.request.headers.clear(); + + const header1 = new Header('option'); + header1.key = 'Content-Type'; + header1.value = 'application/json'; + getRequest.request.headers.add(header1); + + const target = new RequestDefinitionBuilder() + .from(getRequest) + .appendHeaders() + .appendBody(); + + const actual = target.toString(); + + expect(actual).toBe('\nContent-Type: application/json\n\n{ "some": "value" }'); + }); + + test('Simple status code test script is converted to concise format', () => { + getRequest.events.clear(); + + const event1 = new Event({ + listen: 'test', script: { + exec: + [ + "pm.test(\"Status test\", function () {\r", + " pm.response.to.have.status(200);\r", + "});" + ] + } + }); + getRequest.events.add(event1); + + const target = new RequestDefinitionBuilder() + .from(getRequest) + .appendTestScript(); + + const actual = target.toString(); + + expect(actual).toBe('\n\n?? status == 200'); + }); + + test('Unknown test script is added commented out', () => { + getRequest.events.clear(); + + const event1 = new Event({ listen: 'test', script: { exec: ['console.log("something");'] } }); + getRequest.events.add(event1); + + const target = new RequestDefinitionBuilder() + .from(getRequest) + .appendTestScript(); + + const actual = target.toString(); + + expect(actual).toBe('\n\n{{' + + '\n// TODO: Fixup Postman test script' + + '\n// console.log("something");' + + '\n}}' + ); + }); }); \ No newline at end of file