Skip to content
Permalink
Browse files

Common newman test functions. Moved newman tests out for curl, csharp…

…-restsharp, java-okhttp, libcurl, and nodejs-native
  • Loading branch information
umeshp7 committed Aug 23, 2019
1 parent 33ed22c commit f0e3d72fa1b5e54f04e376980c98f9de8a3eb354
@@ -64,4 +64,6 @@ typings/
# next.js build output
.next

out/
out/

newmanResponses.json
@@ -1,142 +1,16 @@
var expect = require('chai').expect,
fs = require('fs'),
path = require('path'),
sdk = require('postman-collection'),
exec = require('shelljs').exec,
newman = require('newman'),
parallel = require('async').parallel,
runSnippet = require('../../../../test/codegen/newman/newman.test').runSnippet,

convert = require('../../lib/index').convert,
mainCollection = require('./fixtures/testcollection/collection.json'),
mainCollection = require('../../../../test/codegen/newman/fixtures/testCollection2.json'),
testCollection = require('./fixtures/testcollection/collectionForEdge.json'),
getOptions = require('../../lib/index').getOptions,
testResponse = require('./fixtures/testresponse.json'),
sanitize = require('../../lib/util').sanitize,
sanitizeOptions = require('../../lib/util').sanitizeOptions;

/**
* compiles and runs codesnippet then compare it with newman output
*
* @param {String} codeSnippet - code snippet that needed to run using C#
* @param {Object} collection - collection which will be run using newman
* @param {Function} done - callback for async calls
*/
function runSnippet (codeSnippet, collection, done) {
const depedenciesPath = 'test/unit/fixtures/dependencies';

fs.writeFile(`${depedenciesPath}/main.cs`, codeSnippet, function (err) {
if (err) {
expect.fail(null, null, err);
return done();
}

// bash command string for compiling C#
var compile = `mcs -reference:${depedenciesPath}/RestSharp.dll` +
` -out:${depedenciesPath}/main.exe ${depedenciesPath}/main.cs`,

// bash command stirng for run compiled C# file
run = `mono ${depedenciesPath}/main.exe`;

// step by step process for compile, run code snippet, then comparing its output with newman
parallel([
function (callback) {
exec(compile, function (err, stdout, stderr) {
if (err) {
return callback(err);
}
if (stderr) {
return callback(stderr);
}
return exec(run, function (err, stdout, stderr) {
if (err) {
return callback(err);
}
if (stderr) {
return callback(stderr);
}
try {
stdout = JSON.parse(stdout);
}
catch (e) {
console.error();
}
return callback(null, stdout);
});
});
},
function (callback) {
newman.run({
collection: collection
}).on('request', function (err, summary) {
if (err) {
return callback(err);
}

var stdout = summary.response.stream.toString();
try {
stdout = JSON.parse(stdout);
}
catch (e) {
console.error();
}
return callback(null, stdout);
});
}
], function (err, result) {
if (err) {
expect.fail(null, null, err);
}
else if (typeof result[1] !== 'object' || typeof result[0] !== 'object') {
expect(result[0].trim()).to.equal(result[1].trim());
}
else {
const propertiesTodelete = ['cookies', 'headersSize', 'startedDateTime', 'clientIPAddress'],
headersTodelete = [
'accept-encoding',
'user-agent',
'cf-ray',
'kong-cloud-request-id', // random ID generated by mockbin
'x-real-ip',
'x-request-id',
'x-request-start',
'connect-time',
'x-forwarded-for',
'cache-control',
'content-type',
'content-length',
'accept',
'accept-language',
'total-route-time',
'cookie',
'postman-token'
];
if (result[0]) {
propertiesTodelete.forEach(function (property) {
delete result[0][property];
});
if (result[0].headers) {
headersTodelete.forEach(function (property) {
delete result[0].headers[property];
});
}
}
if (result[1]) {
propertiesTodelete.forEach(function (property) {
delete result[1][property];
});
if (result[1].headers) {
headersTodelete.forEach(function (property) {
delete result[1].headers[property];
});
}
}

expect(result[0]).deep.equal(result[1]);
}
return done();
});
});
}

describe('csharp restsharp function', function () {
describe('convert for different request types', function () {
var headerSnippet = 'using System;\n' +
@@ -146,15 +20,15 @@ describe('csharp restsharp function', function () {
'static void Main(string[] args) {\n',
footerSnippet = '}\n}\n}\n';

mainCollection.item.forEach(function (item) {
mainCollection.item.forEach(function (item, index) {
it(item.name, function (done) {
var request = new sdk.Request(item.request),
collection = {
item: [
{
request: request.toJSON()
}
]
depedenciesPath = path.resolve(__dirname, 'fixtures/dependencies'),
testConfig = {
compileScript: `mcs -reference:${depedenciesPath}/RestSharp.dll` +
` -out:${depedenciesPath}/main.exe ${depedenciesPath}/main.cs`,
runScript: `mono ${depedenciesPath}/main.exe`,
fileName: `${depedenciesPath}/main.cs`
},
options = {
indentCount: 1,
@@ -168,7 +42,17 @@ describe('csharp restsharp function', function () {
expect.fail(null, null, error);
return;
}
runSnippet(headerSnippet + snippet + footerSnippet, collection, done);
runSnippet(headerSnippet + snippet + footerSnippet, index, testConfig, function (err, result) {
if (err) {
expect.fail(null, null, err);
}
if (typeof result[1] !== 'object' || typeof result[0] !== 'object') {
expect(result[0].toString().trim()).to.include(result[1].toString().trim());
}

expect(result[0]).deep.equal(result[1]);
return done();
});
});
});
return false;
@@ -1,130 +1,14 @@
var expect = require('chai').expect,
sdk = require('postman-collection'),
exec = require('shelljs').exec,
newman = require('newman'),
parallel = require('async').parallel,

runSnippet = require('../../../../test/codegen/newman/newman.test').runSnippet,
convert = require('../../index').convert,
mainCollection = require('./fixtures/testcollection/collection.json');

/**
* runs codesnippet then compare it with newman output
*
* @param {String} codeSnippet - code snippet that needed to run using java
* @param {Object} collection - collection which will be run using newman
* @param {Function} done - callback for async calls
*/
function runSnippet (codeSnippet, collection, done) {

// step by step process for compile, run code snippet, then comparing its output with newman
parallel([
function (callback) {
return exec(codeSnippet, function (err, stdout, stderr) {
if (err) {
return callback(err);
}
if (stderr) {
return callback(stderr);
}
try {
stdout = JSON.parse(stdout);
}
catch (e) {
console.error(e);
}
return callback(null, stdout);
});
},
function (callback) {
newman.run({
collection: collection
}).on('request', function (err, summary) {
if (err) {
return callback(err);
}

var stdout = summary.response.stream.toString();
if (summary.request.method === 'HEAD') {
stdout = summary.response.code.toString();
return callback(null, stdout);
}
try {
stdout = JSON.parse(stdout);
}
catch (e) {
console.error(e);
}
return callback(null, stdout);
});
}
], function (err, result) {
if (err) {
expect.fail(null, null, err);
}
else if (typeof result[1] !== 'object' || typeof result[0] !== 'object') {
expect(result[0].trim()).to.include(result[1].trim());
}
else {
const propertiesTodelete = ['cookies', 'headersSize', 'startedDateTime', 'clientIPAddress'],
headersTodelete = [
'accept-encoding',
'user-agent',
'cf-ray',
'x-real-ip',
'x-request-id',
'kong-request-id',
'x-request-start',
'connect-time',
'x-forwarded-for',
'content-type',
'content-length',
'accept',
'total-route-time',
'cookie',
'kong-cloud-request-id',
'cache-control',
'postman-token'
];
if (result[0]) {
propertiesTodelete.forEach(function (property) {
delete result[0][property];
});
if (result[0].headers) {
headersTodelete.forEach(function (property) {
delete result[0].headers[property];
});
}
}
if (result[1]) {
propertiesTodelete.forEach(function (property) {
delete result[1][property];
});
if (result[1].headers) {
headersTodelete.forEach(function (property) {
delete result[1].headers[property];
});
}
}

expect(result[0]).deep.equal(result[1]);
}
return done();
});
}
mainCollection = require('../../../../test/codegen/newman/fixtures/testCollection.json');

describe('curl convert function', function () {
describe('convert for different request types', function () {

mainCollection.item.forEach(function (item) {
mainCollection.item.forEach(function (item, index) {
it(item.name, function (done) {
var request = new sdk.Request(item.request),
collection = {
item: [
{
request: request.toJSON()
}
]
},
options = {
indentCount: 3,
indentType: 'Space',
@@ -140,7 +24,18 @@ describe('curl convert function', function () {
expect.fail(null, null, error);
return;
}
runSnippet(snippet, collection, done);
runSnippet(snippet, index, {compileScript: null, runScript: null, fileName: null}, function (err, result) {
if (err) {
expect.fail(null, null, err);
}
if (typeof result[1] !== 'object' || typeof result[0] !== 'object') {
expect(result[0].toString().trim()).to.include(result[1].toString().trim());
}

expect(result[0]).deep.equal(result[1]);
return done();
});

});
});
});

0 comments on commit f0e3d72

Please sign in to comment.
You can’t perform that action at this time.