Skip to content

Commit

Permalink
Fix placement of pre-request script (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewturner authored Sep 9, 2024
1 parent ffb82c9 commit e5d3cbf
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@ There is no current support for:
* Mandatory parameters - the sourcePath and targetPath should be mandatory but [ts-command-line-args ](https://www.npmjs.com/package/ts-command-line-args) seems to demand they be set as optional

## Running from the repository
`npm run convert -- --sourcePath=sample.postman_collection.json --targetPath=output`
`npm run import -- --sourcePath=sample.postman_collection.json --targetPath=output`
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"scripts": {
"build": "npx tsc",
"test": "jest",
"convert": "ts-node ./src/app.ts"
"import": "ts-node ./src/app.ts"
},
"keywords": [
"postman",
Expand Down
12 changes: 10 additions & 2 deletions sample.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
"v1",
"comments"
],
"query": [
]
"query": []
}
},
"response": []
Expand All @@ -51,6 +50,15 @@
{
"name": "Get Comment",
"event": [
{
"listen": "prerequest",
"script": {
"exec": [
"console.log('Some pre-request setup');"
],
"type": "text/javascript"
}
},
{
"listen": "test",
"script": {
Expand Down
14 changes: 12 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,17 @@ const args = parse<IOptions>({
headerContentSections: [{ header: 'HttpYac Import', content: 'Converts Postman collections to HttpYac format' }]
});

const sourcePostmanCollectionPath = args.sourcePath.toString();
if (args.sourcePath === undefined) {
console.log('Source path must be supplied with --sourcePath=path');
process.exit(1);
}

if (args.targetPath === undefined) {
console.log('Target path must be supplied with --targetPath=path');
process.exit(2);
}

const sourcePostmanCollectionPath = args.sourcePath;
const sourcePostmanCollection = JSON.parse(readFileSync(sourcePostmanCollectionPath).toString());

const targetPaths = [args.targetPath];
Expand Down Expand Up @@ -70,10 +80,10 @@ function processItem(item: Item) {
.ignoreHeaders(args.ignoreHeaders)
.from(item)
.appendName()
.appendPreRequestScript()
.appendRequest()
.appendHeaders()
.appendBody()
.appendPreRequestScript()
.appendTestScript()
.toString();

Expand Down
89 changes: 81 additions & 8 deletions src/tests/RequestDefinitionBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('Request Definition Builder', () => {
const v1Group = <ItemGroup<Item>>rootGroup.items.all().at(0);
const getRequest = <Item>v1Group.items.all().at(0);

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

const target = new RequestDefinitionBuilder()
Expand All @@ -22,8 +22,9 @@ describe('Request Definition Builder', () => {
expect(actual).toBe('\n\nGET host.com/\n ?color=red');
});

test('Header is added', () => {
test('Adds single header', () => {
getRequest.request.headers.clear();

const header = new Header('option');
header.key = 'SomeHeader';
header.value = 'SomeValue';
Expand All @@ -38,12 +39,14 @@ describe('Request Definition Builder', () => {
expect(actual).toBe('\nSomeHeader: SomeValue');
});

test('Multiple headers are added', () => {
test('Adds multiple headers', () => {
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';
Expand All @@ -58,7 +61,58 @@ describe('Request Definition Builder', () => {
expect(actual).toBe('\nSomeHeader1: SomeValue1\nSomeHeader2: SomeValue2');
});

test('Body is added with default content-type header', () => {
test('Ignores exact header', () => {
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()
.ignoreHeaders(['SomeHeader1'])
.from(getRequest)
.appendHeaders();

const actual = target.toString();

expect(actual).toBe('\nSomeHeader2: SomeValue2');
});

test('Ignores header based on regex pattern', () => {
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 header3 = new Header('option');
header3.key = 'ExactHeader3';
header3.value = 'SomeValue3';
getRequest.request.headers.add(header3);

const target = new RequestDefinitionBuilder()
.ignoreHeaders(['Some.*'])
.from(getRequest)
.appendHeaders();

const actual = target.toString();

expect(actual).toBe('\nExactHeader3: SomeValue3');
});

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

const target = new RequestDefinitionBuilder()
Expand All @@ -70,7 +124,7 @@ describe('Request Definition Builder', () => {
expect(actual).toBe('\nContent-Type: application/json\n\n{ "some": "value" }');
});

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

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

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

const event1 = new Event({
Expand All @@ -113,7 +167,7 @@ describe('Request Definition Builder', () => {
expect(actual).toBe('\n\n?? status == 200');
});

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

const event1 = new Event({
Expand All @@ -140,7 +194,7 @@ describe('Request Definition Builder', () => {
);
});

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

const event1 = new Event({ listen: 'test', script: { exec: ['console.log("something");'] } });
Expand All @@ -158,4 +212,23 @@ describe('Request Definition Builder', () => {
+ '\n}}'
);
});

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

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

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

const actual = target.toString();

expect(actual).toBe('\n\n{{'
+ '\n// TODO: Fixup Postman pre-request script'
+ '\n// console.log("something");'
+ '\n}}'
);
});
});

0 comments on commit e5d3cbf

Please sign in to comment.