Skip to content

Commit

Permalink
Fix path rendering (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewturner authored Sep 14, 2024
1 parent e421a19 commit 89ad4be
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ dist
node_modules
solaris
output
coverage

example.postman_collection.json
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "httpyac-import",
"version": "0.5.0",
"version": "0.5.1",
"description": "CLI to convert Postman collection to httpyac files",
"homepage": "https://github.com/matthewturner/httpyac-import",
"repository": {
Expand All @@ -17,6 +17,7 @@
"scripts": {
"build": "npx tsc",
"test": "jest",
"coverage": "jest --coverage",
"import": "ts-node ./src/app.ts"
},
"keywords": [
Expand Down
11 changes: 10 additions & 1 deletion src/RequestDefinitionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ export class RequestDefinitionBuilder {

if (statement.indexOf('pm.response.to.have.status') >= 0) {
statusCode = this.statusCodeFor(statement);
if (statusCode == null) {
handled = false;
}
continue;
}

Expand Down Expand Up @@ -180,7 +183,13 @@ export class RequestDefinitionBuilder {
appendRequest(): RequestDefinitionBuilder {
this._definition += '\n\n';

this._definition += `${this._item.request.method} ${this._item.request.url.getHost()}${this._item.request.url.getPath()}`;
this._definition += `${this._item.request.method} ${this._item.request.url.getHost()}`;

const path = this._item.request.url.getPath();

if (path.length > 1) {
this._definition += path;
}

let paramCount = 0;
for (let x of this._item.request.url.query.all()) {
Expand Down
176 changes: 168 additions & 8 deletions src/tests/RequestDefinitionBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,68 @@ import { Item, ItemGroup, Collection, Url, Header, RequestBody, Event } from 'po
import { readFileSync } from 'fs';

describe('Request Definition Builder', () => {
const sourcePostmanCollection = JSON.parse(readFileSync('sample.postman_collection.json').toString());
const sourceCollection = new Collection(sourcePostmanCollection);
let getRequest: Item;

const rootGroup = <ItemGroup<Item>>sourceCollection.items.all().at(0);
const v1Group = <ItemGroup<Item>>rootGroup.items.all().at(0);
const getRequest = <Item>v1Group.items.all().at(0);
beforeEach(() => {
const sourcePostmanCollection = JSON.parse(readFileSync('sample.postman_collection.json').toString());
const sourceCollection = new Collection(sourcePostmanCollection);

const rootGroup = <ItemGroup<Item>>sourceCollection.items.all().at(0);
const v1Group = <ItemGroup<Item>>rootGroup.items.all().at(0);

getRequest = <Item>v1Group.items.all().at(0);
});

test('Splits request line', () => {
getRequest.request.url = new Url('http://host.com?color=red')
getRequest.request.url = new Url('http://host.com?color=red');

const target = new RequestDefinitionBuilder()
.from(getRequest)
.appendRequest();

const actual = target.toString();

expect(actual).toBe('\n\nGET host.com'
+ '\n ?color=red');
});

test('Splits request line with path', () => {
getRequest.request.url = new Url('http://host.com/items?color=red');

const target = new RequestDefinitionBuilder()
.from(getRequest)
.appendRequest();

const actual = target.toString();

expect(actual).toBe('\n\nGET host.com/\n ?color=red');
expect(actual).toBe('\n\nGET host.com/items'
+ '\n ?color=red');
});

test('Splits request line with multiple query parameters', () => {
getRequest.request.url = new Url('http://host.com?color=red&flavor=sweet');

const target = new RequestDefinitionBuilder()
.from(getRequest)
.appendRequest();

const actual = target.toString();

expect(actual).toBe('\n\nGET host.com'
+ '\n ?color=red'
+ '\n &flavor=sweet');
});

test('Handles missing headers', () => {
getRequest.request.headers = undefined;

const target = new RequestDefinitionBuilder()
.from(getRequest)
.appendHeaders();

const actual = target.toString();

expect(actual).toBe('');
});

test('Adds single header', () => {
Expand Down Expand Up @@ -112,6 +157,18 @@ describe('Request Definition Builder', () => {
expect(actual).toBe('\nExactHeader3: SomeValue3');
});

test('Handles missing body', () => {
getRequest.request.body = undefined;

const target = new RequestDefinitionBuilder()
.from(getRequest)
.appendBody();

const actual = target.toString();

expect(actual).toBe('');
});

test('Adds body with default content-type header', () => {
getRequest.request.body = new RequestBody({ mode: 'json', raw: '{ "some": "value" }' });

Expand Down Expand Up @@ -143,6 +200,18 @@ describe('Request Definition Builder', () => {
expect(actual).toBe('\nContent-Type: application/json\n\n{ "some": "value" }');
});

test('Handles missing test script', () => {
getRequest.events.clear();

const target = new RequestDefinitionBuilder()
.from(getRequest)
.appendTestScript();

const actual = target.toString();

expect(actual).toBe('');
});

test('Converts simple status code test script to concise format', () => {
getRequest.events.clear();

Expand All @@ -167,6 +236,35 @@ describe('Request Definition Builder', () => {
expect(actual).toBe('\n\n?? status == 200');
});

test('Handles invalid status code in test script', () => {
getRequest.events.clear();

const event1 = new Event({
listen: 'test', script: {
exec:
[
"pm.test(\"Status test\", function () {\r",
" pm.response.to.have.status(xxx);\r",
"});"
]
}
});
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// pm.test("Status test", function () {'
+ '\r\n// pm.response.to.have.status(xxx);'
+ '\r\n// });\n'
+ '}}');
});

test('Sets id from response as global variable', () => {
getRequest.events.clear();

Expand Down Expand Up @@ -194,6 +292,33 @@ describe('Request Definition Builder', () => {
);
});

test('Sets etag from response as global variable', () => {
getRequest.events.clear();

const event1 = new Event({
listen: 'test', script: {
exec:
[
"pm.test(\"Status test\", function () {\r",
" pm.environment.set(\"someEtag\", pm.response.headers.get(\"Etag\"));\r",
"});"
]
}
});
getRequest.events.add(event1);

const target = new RequestDefinitionBuilder()
.from(getRequest)
.appendTestScript();

const actual = target.toString();

expect(actual).toBe('\n\n{{'
+ '\n $global.someEtag = response.headers.etag;'
+ '\n}}'
);
});

test('Adds unknown test script commented out', () => {
getRequest.events.clear();

Expand All @@ -213,6 +338,33 @@ describe('Request Definition Builder', () => {
);
});

test('Handles missing pre-request script', () => {
getRequest.events.clear();

const target = new RequestDefinitionBuilder()
.from(getRequest)
.appendPreRequestScript();

const actual = target.toString();

expect(actual).toBe('');
});

test('Ignores empty pre-request script', () => {
getRequest.events.clear();

const event1 = new Event({ listen: 'prerequest', script: { exec: [''] } });
getRequest.events.add(event1);

const target = new RequestDefinitionBuilder()
.from(getRequest)
.appendPreRequestScript();

const actual = target.toString();

expect(actual).toBe('');
});

test('Adds unknown pre-request script commented out', () => {
getRequest.events.clear();

Expand Down Expand Up @@ -254,9 +406,17 @@ describe('Request Definition Builder', () => {
});

test('Builds output in order', () => {
getRequest.events.clear();

const event1 = new Event({ listen: 'test', script: { exec: ['console.log("something");'] } });
getRequest.events.add(event1);

const event2 = new Event({ listen: 'prerequest', script: { exec: ['console.log("something");'] } });
getRequest.events.add(event2);

getRequest.request.url = new Url('http://host.com?color=red');
getRequest.request.body = new RequestBody({ mode: 'json', raw: '{ "some": "value" }' });

const target = new RequestDefinitionBuilder()
.from(getRequest)
.build();
Expand All @@ -270,7 +430,7 @@ describe('Request Definition Builder', () => {
+ '\n// console.log("something");'
+ '\n}}'
+ '\n'
+ '\nGET host.com/'
+ '\nGET host.com'
+ '\n ?color=red'
+ '\nContent-Type: application/json'
+ '\n'
Expand Down

0 comments on commit 89ad4be

Please sign in to comment.