|
1 | 1 | var expect = require('chai').expect,
|
2 |
| - fs = require('fs'), |
3 | 2 | sdk = require('postman-collection'),
|
4 |
| - exec = require('shelljs').exec, |
5 |
| - newman = require('newman'), |
6 |
| - parallel = require('async').parallel, |
7 |
| - |
| 3 | + async = require('async'), |
| 4 | + newmanTestUtil = require('../../../../test/codegen/newman/newmanTestUtil'), |
8 | 5 | sanitize = require('../../lib/util').sanitize,
|
9 | 6 | parseBody = require('../../lib/parseRequest').parseBody,
|
10 | 7 | getOptions = require('../../lib/index').getOptions,
|
11 | 8 | convert = require('../../lib/index').convert,
|
12 | 9 | mainCollection = require('./fixtures/testcollection/collection.json');
|
13 | 10 |
|
14 |
| -/** |
15 |
| - * compiles and runs codesnippet then compare it with newman output |
16 |
| - * |
17 |
| - * @param {String} codeSnippet - code snippet that needed to run using java |
18 |
| - * @param {Object} collection - collection which will be run using newman |
19 |
| - * @param {Function} done - callback for async call from mocha |
20 |
| - */ |
21 |
| -function runSnippet (codeSnippet, collection, done) { |
22 |
| - fs.writeFile('run.js', codeSnippet, function (err) { |
23 |
| - if (err) { |
24 |
| - expect.fail(null, null, err); |
25 |
| - return done(); |
26 |
| - } |
27 |
| - |
28 |
| - var run = 'node run.js'; |
29 |
| - |
30 |
| - // step by step process for compile, run code snippet, then comparing its output with newman |
31 |
| - parallel([ |
32 |
| - function (callback) { |
33 |
| - exec(run, function (err, stdout, stderr) { |
34 |
| - if (err) { |
35 |
| - return callback(err); |
36 |
| - } |
37 |
| - if (stderr) { |
38 |
| - return callback(stderr); |
39 |
| - } |
40 |
| - |
41 |
| - try { |
42 |
| - stdout = JSON.parse(stdout); |
43 |
| - } |
44 |
| - catch (e) { |
45 |
| - console.error(e); |
46 |
| - } |
47 |
| - return callback(null, stdout); |
48 |
| - }); |
49 |
| - }, |
50 |
| - function (callback) { |
51 |
| - newman.run({ |
52 |
| - collection: collection |
53 |
| - }).on('request', function (err, summary) { |
54 |
| - if (err) { |
55 |
| - return callback(err); |
56 |
| - } |
57 |
| - |
58 |
| - var stdout = summary.response.stream.toString(); |
59 |
| - try { |
60 |
| - stdout = JSON.parse(stdout); |
61 |
| - } |
62 |
| - catch (e) { |
63 |
| - console.error(e); |
64 |
| - } |
65 |
| - return callback(null, stdout); |
66 |
| - }); |
67 |
| - } |
68 |
| - ], function (err, result) { |
69 |
| - if (err) { |
70 |
| - console.error(err); |
71 |
| - expect.fail(null, null, err); |
72 |
| - } |
73 |
| - else if (typeof result[1] !== 'object' || typeof result[0] !== 'object') { |
74 |
| - expect(result[0].trim()).to.equal(result[1].trim()); |
75 |
| - } |
76 |
| - else { |
77 |
| - const propertiesTodelete = ['cookies', 'headersSize', 'startedDateTime', 'clientIPAddress'], |
78 |
| - headersTodelete = [ |
79 |
| - 'accept-encoding', |
80 |
| - 'user-agent', |
81 |
| - 'cf-ray', |
82 |
| - 'x-request-id', |
83 |
| - 'x-request-start', |
84 |
| - 'connect-time', |
85 |
| - 'x-forwarded-for', |
86 |
| - 'content-type', |
87 |
| - 'content-length', |
88 |
| - 'accept', |
89 |
| - 'total-route-time', |
90 |
| - 'cookie', |
91 |
| - 'kong-cloud-request-id', |
92 |
| - 'cache-control', |
93 |
| - 'postman-token', |
94 |
| - 'x-real-ip' |
95 |
| - ]; |
96 |
| - if (result[0]) { |
97 |
| - propertiesTodelete.forEach(function (property) { |
98 |
| - delete result[0][property]; |
99 |
| - }); |
100 |
| - if (result[0].headers) { |
101 |
| - headersTodelete.forEach(function (property) { |
102 |
| - delete result[0].headers[property]; |
103 |
| - }); |
104 |
| - } |
105 |
| - } |
106 |
| - if (result[1]) { |
107 |
| - propertiesTodelete.forEach(function (property) { |
108 |
| - delete result[1][property]; |
109 |
| - }); |
110 |
| - if (result[1].headers) { |
111 |
| - headersTodelete.forEach(function (property) { |
112 |
| - delete result[1].headers[property]; |
113 |
| - }); |
114 |
| - } |
115 |
| - } |
116 |
| - expect(result[0]).deep.equal(result[1]); |
117 |
| - } |
118 |
| - return done(); |
119 |
| - }); |
120 |
| - }); |
121 |
| -} |
122 |
| - |
123 | 11 | describe('nodejs-request convert function', function () {
|
124 |
| - mainCollection.item.forEach(function (item) { |
125 |
| - it(item.name, function (done) { |
126 |
| - var request = new sdk.Request(item.request), |
127 |
| - collection = { |
128 |
| - item: [ |
129 |
| - { |
130 |
| - request: request.toJSON() |
131 |
| - } |
132 |
| - ] |
133 |
| - }; |
134 |
| - convert(request, {indentCount: 3, indentType: 'Space'}, function (error, snippet) { |
| 12 | + var options = {indentCount: 2, indentType: 'Space'}, |
| 13 | + testConfig = {compileScript: null, runScript: 'node run.js', fileName: 'run.js'}, |
| 14 | + header = '/* eslint-disable */\n'; |
| 15 | + |
| 16 | + async.waterfall([ |
| 17 | + function (next) { |
| 18 | + newmanTestUtil.generateSnippet(convert, options, function (error, snippets) { |
135 | 19 | if (error) {
|
136 | 20 | expect.fail(null, null, error);
|
137 |
| - return; |
| 21 | + return next(error); |
138 | 22 | }
|
139 |
| - // disabling eslint for test file |
140 |
| - snippet = '/* eslint-disable */\n' + snippet; |
141 | 23 |
|
142 |
| - runSnippet(snippet, collection, done); |
| 24 | + return next(null, snippets); |
143 | 25 | });
|
144 |
| - }); |
145 |
| - }); |
| 26 | + }, |
| 27 | + function (snippets, next) { |
| 28 | + snippets.forEach((item, index) => { |
| 29 | + it(item.name, function (done) { |
| 30 | + newmanTestUtil.runSnippet(header + item.snippet, index, testConfig, |
| 31 | + function (err, result) { |
| 32 | + if (err) { |
| 33 | + expect.fail(null, null, err); |
| 34 | + } |
| 35 | + if (typeof result[1] !== 'object' || typeof result[0] !== 'object') { |
| 36 | + expect(result[0].toString().trim()).to.include(result[1].toString().trim()); |
| 37 | + } |
| 38 | + |
| 39 | + expect(result[0]).deep.equal(result[1]); |
| 40 | + return done(null); |
| 41 | + }); |
| 42 | + }); |
| 43 | + }); |
| 44 | + return next(null); |
| 45 | + } |
| 46 | + ]); |
146 | 47 |
|
147 | 48 | describe('Convert function', function () {
|
148 | 49 | var request,
|
|
0 commit comments