Skip to content

Commit

Permalink
Fix test script and duplicate header (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewturner authored Sep 8, 2024
1 parent f079fc1 commit 01158b8
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
12 changes: 7 additions & 5 deletions src/RequestDefinitionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 != '') {
Expand Down Expand Up @@ -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();

Expand Down
64 changes: 63 additions & 1 deletion src/tests/RequestDefinitionBuilder.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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}}'
);
});
});

0 comments on commit 01158b8

Please sign in to comment.