har2postman
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -1,3 +0,3 @@ | ||
var HarToPostman = require('./lib/har-to-postman.js'); | ||
const HarToPostman = require('./lib/har-to-postman.js'); | ||
module.exports = HarToPostman; | ||
module.exports = HarToPostman; |
{ | ||
"spec_dir": "test", | ||
"spec_files": [ | ||
"**/*.js" | ||
"**/test.js" | ||
], | ||
@@ -6,0 +6,0 @@ "stopSpecOnExpectationFailure": false, |
@@ -1,93 +0,141 @@ | ||
function HarToPostman() { }; | ||
const HarToPostman = () => { }; | ||
HarToPostman.createPostmanCollection = function (harContent, generateTest = false) { | ||
try { | ||
var harContent = JSON.parse(harContent); | ||
} catch (e) { | ||
return 'invalid json'; | ||
} | ||
var postmanContent = {}; | ||
postmanContent.info = generateInfo(); | ||
postmanContent.item = generateItem(harContent, generateTest); | ||
return JSON.stringify(postmanContent); | ||
HarToPostman.createPostmanCollection = (harContent, includeTest = false) => { | ||
try { | ||
harContent = JSON.parse(harContent); | ||
} catch (e) { | ||
return 'invalid json'; | ||
} | ||
const postmanContent = {}; | ||
postmanContent.info = generateInfo(); | ||
postmanContent.item = generateItem(harContent, includeTest); | ||
return JSON.stringify(postmanContent); | ||
}; | ||
var generateInfo = function () { | ||
return { | ||
name: 'Har2Postman', | ||
schema: 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json' | ||
}; | ||
} | ||
const generateInfo = () => { | ||
return { | ||
name: 'Har2Postman', | ||
schema: 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json', | ||
}; | ||
}; | ||
var generateItem = function (harContent, generateTest) { | ||
var harRequest = harContent.log.entries[0].request; | ||
var harRequestUrl = new URL(harRequest.url); | ||
var harPathnameArray = harRequestUrl.pathname.split('/'); | ||
var harResponse = harContent.log.entries[0].response; | ||
var item = [{ | ||
name: generateItemName(harRequest.method, harRequestUrl.pathname, harResponse.status, generateTest), | ||
event: generateItemEvent(harResponse, harRequestUrl), | ||
request: { | ||
method: harRequest.method, | ||
url: { | ||
raw: harRequestUrl.toString(), | ||
protocol: harRequestUrl.protocol.slice(0, -1), | ||
host: harRequestUrl.hostname.split('.'), | ||
path: harPathnameArray.slice(1) | ||
} | ||
} | ||
}]; | ||
if (!generateTest) { | ||
delete item[0].event; | ||
} | ||
return item; | ||
} | ||
const generateItem = (harContent, includeTest) => { | ||
const harRequest = harContent.log.entries[0].request; | ||
const harRequestUrl = new URL(harRequest.url); | ||
const harResponse = harContent.log.entries[0].response; | ||
const item = [{ | ||
name: generateItemName(harRequest.method, harRequestUrl.pathname, harResponse.status, includeTest), | ||
event: generateItemEvent(harResponse, harRequestUrl), | ||
request: generateItemRequest(harRequest), | ||
}]; | ||
if (!includeTest) { | ||
delete item[0].event; | ||
} | ||
if (harRequest.queryString.length == 0) { | ||
delete item[0].request.url.query; | ||
} | ||
return item; | ||
}; | ||
var generateItemName = function (method, path, responseCode, generateTest) { | ||
var status = ''; | ||
switch (responseCode) { | ||
case 200: | ||
status = 'successfully'; | ||
break; | ||
} | ||
var itemName = method + ' ' + path; | ||
if (generateTest) { | ||
itemName += ' ' + status; | ||
} | ||
return itemName; | ||
} | ||
const generateItemRequest = (harRequest) => { | ||
const harRequestUrl = new URL(harRequest.url); | ||
return { | ||
method: harRequest.method, | ||
url: generateItemRequestUrl(harRequestUrl, harRequest.queryString), | ||
}; | ||
}; | ||
var generateItemEvent = function (response, harRequestUrl) { | ||
var scriptExec = generateStatusCodeAssertion(response.status); | ||
var harPathnameArray = harRequestUrl.pathname.split('/'); | ||
var lastPathObject = harPathnameArray[harPathnameArray.length - 1]; | ||
if (responseContainsId(response, parseInt(lastPathObject))) { | ||
scriptExec = scriptExec.concat(generateIdAssertion(lastPathObject)); | ||
} | ||
var script = { | ||
exec: scriptExec, | ||
type: 'text/javascript' | ||
}; | ||
return [{ | ||
listen: 'test', | ||
script: script | ||
}]; | ||
} | ||
const generateItemRequestUrl = (harRequestUrl, queryString) => { | ||
const harPathnameArray = harRequestUrl.pathname.split('/'); | ||
return { | ||
raw: harRequestUrl.toString(), | ||
protocol: harRequestUrl.protocol.slice(0, -1), | ||
host: harRequestUrl.hostname.split('.'), | ||
path: harPathnameArray.slice(1), | ||
query: generateQueryParams(queryString), | ||
}; | ||
}; | ||
var generateIdAssertion = function (id) { | ||
return ['pm.test("Fetched object should be the expected one", function () {', | ||
' var jsonData = pm.response.json();', ' pm.expect(jsonData.id).to.eql(' + id + ');', | ||
'});']; | ||
} | ||
var generateStatusCodeAssertion = function (status) { | ||
return ['pm.test("Status code is ' + status + '", function () {', | ||
const generateItemName = (method, path, responseCode, includeTest) => { | ||
let status = ''; | ||
switch (responseCode) { | ||
case 200: | ||
status = 'successfully'; | ||
break; | ||
} | ||
let itemName = method + ' ' + path; | ||
if (includeTest) { | ||
itemName += ' ' + status; | ||
} | ||
return itemName; | ||
}; | ||
const generateItemEvent = (response, harRequestUrl) => { | ||
return [{ | ||
listen: 'test', | ||
script: generateScript(response, harRequestUrl), | ||
}]; | ||
}; | ||
const generateScript = (response, harRequestUrl) => { | ||
let exec = []; | ||
exec = exec.concat(generateStatusCodeAssertion(response.status)); | ||
if (responseContainsId(response, harRequestUrl)) { | ||
exec = exec.concat(generateIdAssertion(harRequestUrl)); | ||
} | ||
if (responseIsArray(response)) { | ||
exec = exec.concat(generateArrayAssertion()); | ||
} | ||
return { | ||
exec: exec, | ||
type: 'text/javascript'}; | ||
}; | ||
const generateQueryParams = (queryString) => { | ||
queryString.forEach((queryParam) => { | ||
queryParam.key = queryParam.name; | ||
delete queryParam.name; | ||
}); | ||
return queryString; | ||
}; | ||
const generateIdAssertion = (harRequestUrl) => { | ||
const id = getIdFromUrl(harRequestUrl); | ||
return ['pm.test("Fetched object should be the expected one", function () {', | ||
' var jsonData = pm.response.json();', ' pm.expect(jsonData.id).to.eql(' + id + ');', | ||
'});']; | ||
}; | ||
const generateStatusCodeAssertion = (status) => { | ||
return ['pm.test("Status code is ' + status + '", function () {', | ||
' pm.response.to.have.status(' + status + ');', | ||
'});']; | ||
} | ||
'});']; | ||
}; | ||
var responseContainsId = function (response, id) { | ||
return response.content != null && 'text' in response.content && parseInt(JSON.parse(response.content.text).id) === id; | ||
} | ||
const generateArrayAssertion = () => { | ||
return ['pm.test("Response is an array", function() {', | ||
' pm.expect(pm.response.json()).to.be.an("array");', | ||
'});']; | ||
}; | ||
const responseContainsId = (response, harRequestUrl) => { | ||
const id = getIdFromUrl(harRequestUrl); | ||
return response.content != null && 'text' in response.content && parseInt(JSON.parse(response.content.text).id) === id; | ||
}; | ||
const responseIsArray = (response) => { | ||
return response.content != null && 'text' in response.content && Array.isArray(JSON.parse(response.content.text)); | ||
}; | ||
const getIdFromUrl = (harRequestUrl) => { | ||
const harPathnameArray = harRequestUrl.pathname.split('/'); | ||
const lastPathObject = harPathnameArray[harPathnameArray.length - 1]; | ||
return parseInt(lastPathObject); | ||
}; | ||
module.exports = HarToPostman; |
{ | ||
"name": "har2postman", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Har to postman converter", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "jasmine JASMINE_CONFIG_PATH=./jasmine.json" | ||
"test": "jasmine JASMINE_CONFIG_PATH=./jasmine.json", | ||
"format:check": "npx eslint ./", | ||
"format:fix": "npx eslint ./ --fix" | ||
}, | ||
@@ -25,2 +27,4 @@ "repository": { | ||
"devDependencies": { | ||
"eslint": "^6.8.0", | ||
"eslint-config-google": "^0.14.0", | ||
"jasmine": "^3.5.0" | ||
@@ -27,0 +31,0 @@ }, |
@@ -56,2 +56,6 @@ # Har2Postman | ||
* A GET request might include multiple `query params`; those should also be mapped from the har file to the postman collection. Evaluate whether some of them (FK?) should be included as part of the test assertions. | ||
* Evaluate if response is an array, if so, generate test assertion. | ||
* Evaluate if response is not a json file, if so generate only status code assertion. | ||
* CI pipeline should also include integration tests on tag release: using the just released version of the lib, generate a postman collection using the version input, and run it with newman so it checks the lib output works out of the box. | ||
* Include ESLint, with some format scripts in the package and check the linting from the pipeline too | ||
@@ -62,2 +66,5 @@ ### v0.4.0 - Support multiple requests within one har file | ||
### v0.4.5 - Any status code must be supported | ||
* Status code such as 200, 204, 400, 401, 403 or 404 must have specific assertions. | ||
### v0.5.0 - POST, PUT and DELETE methods should also be supported | ||
@@ -75,5 +82,50 @@ * Even though the method is already picked up by he lib, some methods such POST or PUT might include a body. | ||
* when creating an object, might be interesting to save it's `id` if contained in response as env variable for future requests over same entity (GET, PUT or DELETE) | ||
* Make sure every function generates only one type of data structure. F.e. this should be avoided: | ||
```javascript | ||
var generateItem = function(){ | ||
var item = [{ | ||
name: generateItemName(harRequest.method, harRequestUrl.pathname, harResponse.status, generateTest), | ||
event: generateItemEvent(harResponse, harRequestUrl), | ||
request: { | ||
method: harRequest.method, | ||
url: { | ||
raw: harRequestUrl.toString(), | ||
protocol: harRequestUrl.protocol.slice(0, -1), | ||
host: harRequestUrl.hostname.split('.'), | ||
path: harPathnameArray.slice(1) | ||
} | ||
} | ||
}]; | ||
} | ||
``` | ||
The right way for this function would be: | ||
```javascript | ||
var generateItem = function(){ | ||
return [{ | ||
name: generateItemName(harRequest.method, harRequestUrl.pathname, harResponse.status, generateTest), | ||
event: generateItemEvent(harResponse, harRequestUrl), | ||
request: generateRequest(harRequest, harRequestUrl); | ||
}]; | ||
} | ||
var generateRequest = function(){ | ||
return { | ||
method: harRequest.method, | ||
url: generateUrl(harRequestUrl); | ||
} | ||
} | ||
//var generateUrl = function() ... | ||
``` | ||
**Also important** have a look at the functions returning arrays instead of objects, this will have to change in the future if those arrays need more than one element. | ||
## License | ||
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details |
@@ -1,15 +0,15 @@ | ||
describe('Har2Postman', function () { | ||
var harToPostman = require('../../lib/har-to-postman.js'); | ||
var fs = require('fs'); | ||
var harFile = JSON.parse(fs.readFileSync(__dirname + '/input.json', 'utf8')); | ||
var expectedPostmanCollection = JSON.parse(fs.readFileSync(__dirname + '/output.json', 'utf8')); | ||
describe('Har2Postman', () => { | ||
const harToPostman = require('../../lib/har-to-postman.js'); | ||
const fs = require('fs'); | ||
const harFile = JSON.parse(fs.readFileSync(__dirname + '/input.json', 'utf8')); | ||
const expectedPostmanCollection = JSON.parse(fs.readFileSync(__dirname + '/output.json', 'utf8')); | ||
const postmanContent = harToPostman.createPostmanCollection(JSON.stringify(harFile)); | ||
const postmanCollection = JSON.parse(postmanContent); | ||
it('Throws an error given a non JSON string as entry', function () { | ||
var postmanContent = harToPostman.createPostmanCollection('any non json argument'); | ||
it('Throws an error given a non JSON string as entry', () => { | ||
const postmanContent = harToPostman.createPostmanCollection('any non json argument'); | ||
expect(postmanContent).toEqual('invalid json'); | ||
}); | ||
it('Generates info section for postman collection', function () { | ||
var postmanContent = harToPostman.createPostmanCollection(JSON.stringify(harFile)); | ||
var postmanCollection = JSON.parse(postmanContent); | ||
it('Generates info section for postman collection', () => { | ||
expect(postmanCollection.info.name).toEqual('Har2Postman'); | ||
@@ -19,21 +19,21 @@ expect(postmanCollection.info.schema).toEqual('https://schema.getpostman.com/json/collection/v2.1.0/collection.json'); | ||
it('Generates an item in the output with a name composed out of method + url pathname', function () { | ||
var postmanContent = harToPostman.createPostmanCollection(JSON.stringify(harFile)); | ||
var postmanCollection = JSON.parse(postmanContent); | ||
it('Generates an item in the output with a name composed out of method + url pathname', () => { | ||
expect(postmanCollection.item[0].name).toEqual('GET /posts/1'); | ||
}); | ||
it('Generates an item in the output with a request containing GET method and a valid url object', function () { | ||
var postmanContent = harToPostman.createPostmanCollection(JSON.stringify(harFile)); | ||
var postmanCollection = JSON.parse(postmanContent); | ||
it('Generates an item in the output with a request containing GET method and a valid url object', () => { | ||
expect(postmanCollection.item[0].request.method).toEqual('GET'); | ||
expect(postmanCollection.item[0].request.url).toEqual({ raw: "https://jsonplaceholder.typicode.com/posts/1", protocol: "https", host: ["jsonplaceholder", "typicode", "com"], path: ["posts", "1"] }); | ||
expect(postmanCollection.item[0].request.url).toEqual( | ||
{ | ||
raw: 'https://jsonplaceholder.typicode.com/posts/1', | ||
protocol: 'https', | ||
host: ['jsonplaceholder', 'typicode', 'com'], | ||
path: ['posts', '1'], | ||
}, | ||
); | ||
}); | ||
it("Generates 0.1.0 output json (postman) given the 0.1.0 input json (har)", function () { | ||
var postmanContent = harToPostman.createPostmanCollection(JSON.stringify(harFile)); | ||
var postmanCollection = JSON.parse(postmanContent); | ||
it('Generates 0.1.0 output json (postman) given the 0.1.0 input json (har)', () => { | ||
expect(expectedPostmanCollection).toEqual(postmanCollection); | ||
}); | ||
}); |
@@ -1,46 +0,38 @@ | ||
describe('Har2Postman', function () { | ||
var harToPostman = require('../../lib/har-to-postman.js'); | ||
var fs = require('fs'); | ||
var harFile = JSON.parse(fs.readFileSync(__dirname + '/input.json', 'utf8')); | ||
var expectedPostmanCollection = JSON.parse(fs.readFileSync(__dirname + '/output.json', 'utf8')); | ||
const harToPostman = require('../../lib/har-to-postman.js'); | ||
const fs = require('fs'); | ||
const harFile = JSON.parse(fs.readFileSync(__dirname + '/input.json', 'utf8')); | ||
const expectedPostmanCollection = JSON.parse(fs.readFileSync(__dirname + '/output.json', 'utf8')); | ||
const postmanContent = harToPostman.createPostmanCollection(JSON.stringify(harFile), true); | ||
const postmanCollection = JSON.parse(postmanContent); | ||
it('should include item event', function() { | ||
var postmanContent = harToPostman.createPostmanCollection(JSON.stringify(harFile), true); | ||
var postmanCollection = JSON.parse(postmanContent); | ||
expect(postmanCollection.item.length).toEqual(1); | ||
expect(postmanCollection.item[0].event.length).toEqual(1); | ||
}); | ||
describe('Har2Postman', () => { | ||
it('should include item event', () => { | ||
expect(postmanCollection.item.length).toEqual(1); | ||
expect(postmanCollection.item[0].event.length).toEqual(1); | ||
}); | ||
it('should generate postman test that asserts call with response -200-', function() { | ||
var postmanContent = harToPostman.createPostmanCollection(JSON.stringify(harFile), true); | ||
var postmanCollection = JSON.parse(postmanContent); | ||
var itemEvent = postmanCollection.item[0].event[0]; | ||
expect(itemEvent.listen).toEqual("test"); | ||
expect(itemEvent.script.type).toEqual("text/javascript"); | ||
expect(itemEvent.script).toBeDefined(); | ||
itemEvent.script.exec.map((e,i) => { | ||
expect(e).toEqual(expectedPostmanCollection.item[0].event[0].script.exec[i]); | ||
}); | ||
it('should generate postman test that asserts call with response -200-', () => { | ||
const itemEvent = postmanCollection.item[0].event[0]; | ||
expect(itemEvent.listen).toEqual('test'); | ||
expect(itemEvent.script.type).toEqual('text/javascript'); | ||
expect(itemEvent.script).toBeDefined(); | ||
itemEvent.script.exec.map((e, i) => { | ||
expect(e).toEqual(expectedPostmanCollection.item[0].event[0].script.exec[i]); | ||
}); | ||
}); | ||
it('should also generate postman test that asserts the fetched object is correct', function() { | ||
var postmanContent = harToPostman.createPostmanCollection(JSON.stringify(harFile), true); | ||
var postmanCollection = JSON.parse(postmanContent); | ||
var itemEvent = postmanCollection.item[0].event[0]; | ||
expect(itemEvent.listen).toEqual("test"); | ||
expect(itemEvent.script.type).toEqual("text/javascript"); | ||
expect(itemEvent.script).toBeDefined(); | ||
expect(itemEvent.script.exec.length).toEqual(7); | ||
itemEvent.script.exec.map((e,i) => { | ||
expect(e).toEqual(expectedPostmanCollection.item[0].event[0].script.exec[i]); | ||
}); | ||
it('should also generate postman test that asserts the fetched object is correct', () => { | ||
const itemEvent = postmanCollection.item[0].event[0]; | ||
expect(itemEvent.listen).toEqual('test'); | ||
expect(itemEvent.script.type).toEqual('text/javascript'); | ||
expect(itemEvent.script).toBeDefined(); | ||
expect(itemEvent.script.exec.length).toEqual(7); | ||
itemEvent.script.exec.map((e, i) => { | ||
expect(e).toEqual(expectedPostmanCollection.item[0].event[0].script.exec[i]); | ||
}); | ||
}); | ||
it("Generates 0.2.0 output json (postman) given the 0.2.0 input json (har)", function () { | ||
var postmanContent = harToPostman.createPostmanCollection(JSON.stringify(harFile), true); | ||
var postmanCollection = JSON.parse(postmanContent); | ||
expect(expectedPostmanCollection).toEqual(postmanCollection); | ||
}); | ||
it('Generates 0.2.0 output json (postman) given the 0.2.0 input json (har)', () => { | ||
expect(expectedPostmanCollection).toEqual(postmanCollection); | ||
}); | ||
}); |
@@ -12,6 +12,9 @@ { | ||
"listen": "test", | ||
"script": { | ||
"script": { | ||
"exec": [ | ||
"pm.test(\"Status code is 200\", function () {", | ||
" pm.response.to.have.status(200);", | ||
"});", | ||
"pm.test(\"Response is an array\", function() {", | ||
" pm.expect(pm.response.json()).to.be.an(\"array\");", | ||
"});" | ||
@@ -50,2 +53,2 @@ ], | ||
] | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
163006
22
4548
129
0
3
4