Skip to content

Commit d1382f7

Browse files
author
dhwaneetbhatt
committed
Add new unit tests
1 parent 3e524e0 commit d1382f7

File tree

6 files changed

+92
-24
lines changed

6 files changed

+92
-24
lines changed

codegens/nodejs-axios/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Convert function will take three parameters
1818
* `requestTimeout` : Integer denoting time after which the request will bail out in milli-seconds
1919
* `trimRequestBody` : Trim request body fields
2020
* `followRedirect` : Boolean denoting whether to redirect a request
21-
* `async_await_enabled` : Boolean denoting whether to use async/await syntax
21+
* `asyncAwaitEnabled` : Boolean denoting whether to use async/await syntax
2222

2323
* `callback`- callback function with first parameter as error and second parameter as string for code snippet
2424

@@ -28,7 +28,7 @@ var request = new sdk.Request('www.google.com'), //using postman sdk to create
2828
options = {
2929
indentType: 'Space',
3030
indentCount: 2,
31-
async_await_enabled: true
31+
asyncAwaitEnabled: true
3232
};
3333
convert(request, options, function(error, snippet) {
3434
if (error) {

codegens/nodejs-axios/lib/axios.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ function makeSnippet (request, indentString, options) {
112112
if (options.requestTimeout) {
113113
configArray.push(indentString + `timeout: ${options.requestTimeout}`);
114114
}
115-
if (options.followRedirect === false) {
115+
if (_.get(request, 'protocolProfileBehavior.followRedirects', options.followRedirect) === false) {
116116
// setting the maxRedirects to 0 will disable any redirects.
117117
// by default, maxRedirects are set to 5
118118
configArray.push(indentString + 'maxRedirects: 0');
@@ -126,7 +126,7 @@ function makeSnippet (request, indentString, options) {
126126
snippet += configArray.join(',\n') + '\n';
127127
snippet += '};\n\n';
128128

129-
if (options.async_await_enabled) {
129+
if (options.asyncAwaitEnabled) {
130130
snippet += 'async function makeRequest() {\n';
131131
snippet += indentString + 'try {\n';
132132
snippet += indentString.repeat(2) + 'const response = await axios.request(config);\n';
@@ -197,7 +197,7 @@ function getOptions () {
197197
},
198198
{
199199
name: 'Use async/await',
200-
id: 'async_await_enabled',
200+
id: 'asyncAwaitEnabled',
201201
type: 'boolean',
202202
default: false,
203203
description: 'Modifies code snippet to use async/await'

codegens/nodejs-axios/lib/parseRequest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function parseURLEncodedBody (body, trim, indentString) {
1717
dataArray.push(`'${sanitize(data.key, trim)}': '${sanitize(data.value, trim)}'`);
1818
}
1919
});
20-
bodySnippet += `let data = qs.stringify({\n${indentString}${dataArray.join(',\n' + indentString)} \n});`;
20+
bodySnippet += `let data = qs.stringify({\n${indentString}${dataArray.join(',\n' + indentString)} \n});\n`;
2121
return bodySnippet;
2222
}
2323

codegens/nodejs-axios/test/newman/newman.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('Convert for different types of request', function () {
1414
runNewmanTest(convert, options, testConfig);
1515

1616
describe('Convert for request incorporating async/await features', function () {
17-
const options = {indentCount: 2, indentType: 'Space', async_await_enabled: true},
17+
const options = {indentCount: 2, indentType: 'Space', asyncAwaitEnabled: true},
1818
testConfig = {
1919
compileScript: null,
2020
runScript: 'node run.js',

codegens/nodejs-axios/test/unit/snippet.test.js

Lines changed: 82 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,59 @@ describe('nodejs-axios convert function', function () {
8383
});
8484
});
8585

86-
it('should return snippet with maxRedirects property set to ' +
87-
'0 for no follow redirect', function () {
88-
request = new sdk.Request(mainCollection.item[0].request);
89-
options = {
90-
followRedirect: false
91-
};
92-
convert(request, options, function (error, snippet) {
93-
if (error) {
94-
expect.fail(null, null, error);
95-
return;
96-
}
86+
describe('maxRedirects property', function () {
87+
it('should return snippet with maxRedirects property set to ' +
88+
'0 for no follow redirect', function () {
89+
const request = new sdk.Request(mainCollection.item[0].request);
90+
options = {
91+
followRedirect: false
92+
};
93+
convert(request, options, function (error, snippet) {
94+
if (error) {
95+
expect.fail(null, null, error);
96+
}
9797

98-
expect(snippet).to.be.a('string');
99-
expect(snippet).to.include('maxRedirects: 0');
98+
expect(snippet).to.be.a('string');
99+
expect(snippet).to.include('maxRedirects: 0');
100+
});
101+
});
102+
103+
it('should return snippet with maxRedirects property set to ' +
104+
'0 for no follow redirect from request settings', function () {
105+
const request = new sdk.Request(mainCollection.item[0].request),
106+
options = {};
107+
108+
request.protocolProfileBehavior = {
109+
followRedirects: false
110+
};
111+
112+
convert(request, options, function (error, snippet) {
113+
if (error) {
114+
expect.fail(null, null, error);
115+
}
116+
117+
expect(snippet).to.be.a('string');
118+
expect(snippet).to.include('maxRedirects: 0');
119+
});
120+
});
121+
122+
it('should return snippet with no maxRedirects property when ' +
123+
'follow redirect is true from request settings', function () {
124+
const request = new sdk.Request(mainCollection.item[0].request),
125+
options = {};
126+
127+
request.protocolProfileBehavior = {
128+
followRedirects: true
129+
};
130+
131+
convert(request, options, function (error, snippet) {
132+
if (error) {
133+
expect.fail(null, null, error);
134+
}
135+
136+
expect(snippet).to.be.a('string');
137+
expect(snippet).to.not.include('maxRedirects');
138+
});
100139
});
101140
});
102141

@@ -438,6 +477,35 @@ describe('nodejs-axios convert function', function () {
438477
});
439478
});
440479

480+
it('should return snippet with promise based code when async_await is disabled', function () {
481+
const request = new sdk.Request(mainCollection.item[0].request);
482+
483+
convert(request, {}, function (error, snippet) {
484+
if (error) {
485+
expect.fail(null, null, error);
486+
}
487+
expect(snippet).to.be.a('string');
488+
expect(snippet).to.include('axios.request(config)');
489+
expect(snippet).to.include('.then((response) => {');
490+
expect(snippet).to.include('.catch((error) => {');
491+
});
492+
});
493+
494+
it('should return snippet with async/await based code when option is enabled', function () {
495+
const request = new sdk.Request(mainCollection.item[0].request);
496+
497+
convert(request, { asyncAwaitEnabled: true }, function (error, snippet) {
498+
if (error) {
499+
expect.fail(null, null, error);
500+
}
501+
expect(snippet).to.be.a('string');
502+
expect(snippet).to.include('async function makeRequest() {');
503+
expect(snippet).to.include('const response = await axios.request(config);');
504+
expect(snippet).to.include('catch (error) {');
505+
expect(snippet).to.include('makeRequest();');
506+
});
507+
});
508+
441509
describe('getOptions function', function () {
442510

443511
it('should return an array of specific options', function () {
@@ -450,7 +518,7 @@ describe('nodejs-axios convert function', function () {
450518
expect(getOptions()[2]).to.have.property('id', 'requestTimeout');
451519
expect(getOptions()[3]).to.have.property('id', 'followRedirect');
452520
expect(getOptions()[4]).to.have.property('id', 'trimRequestBody');
453-
// expect(getOptions()[5]).to.have.property('id', 'AsyncAwait_enabled');
521+
expect(getOptions()[5]).to.have.property('id', 'asyncAwaitEnabled');
454522
});
455523
});
456524

test/codegen/structure.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ const expectedOptions = {
8181
default: false,
8282
description: 'Modifies code snippet to incorporate ES6 (EcmaScript) features'
8383
},
84-
async_await_enabled: {
84+
asyncAwaitEnabled: {
8585
name: 'Use async/await',
86-
id: 'async_await_enabled',
86+
id: 'asyncAwaitEnabled',
8787
type: 'boolean',
8888
default: false,
8989
description: 'Modifies code snippet to use async/await'
@@ -113,7 +113,7 @@ const expectedOptions = {
113113
'protocol',
114114
'useMimeType',
115115
'ES6_enabled',
116-
'async_await_enabled',
116+
'asyncAwaitEnabled',
117117
'quoteType',
118118
'asyncType',
119119
'ignoreWarnings'

0 commit comments

Comments
 (0)