Skip to content

Commit 823c86a

Browse files
committed
Add test scenarios
Add unit test for options
1 parent 2b9fc9a commit 823c86a

File tree

6 files changed

+265
-7
lines changed

6 files changed

+265
-7
lines changed

codegens/php-guzzle/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Convert function takes three parameters
1717
* `indentCount` - The number of indentation characters to add per code level
1818
* `trimRequestBody` - Whether or not request body fields should be trimmed
1919
* `asyncType` - String denoting call types. eg: 'async', 'sync'
20+
* `followRedirect` : Boolean denoting whether to redirect a request (default: true)
21+
* `requestTimeout` : The number of milliseconds the request should wait for a response before timing out (use 0 for infinity)
2022
* `includeBoilerplate` - Include class definition and import statements in snippet
2123

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

codegens/php-guzzle/test/newman/newman.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var runNewmanTest = require('../../../../test/codegen/newman/newmanTestUtil').ru
44

55
describe('PHP-Guzzle Converter', function () {
66

7-
describe('Convert for different types of request', function () {
7+
describe('Convert for different types of request for sync', function () {
88
var testConfig = {
99
runScript: 'php codesnippet.php',
1010
fileName: 'codesnippet.php',
@@ -18,7 +18,7 @@ describe('PHP-Guzzle Converter', function () {
1818
};
1919
runNewmanTest(convert, options, testConfig);
2020
});
21-
describe('Convert for different types of request', function () {
21+
describe('Convert for different types of request for async', function () {
2222
var testConfig = {
2323
runScript: 'php codesnippet.php',
2424
fileName: 'codesnippet.php',

codegens/php-guzzle/test/unit/convert.test.js

Lines changed: 151 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,126 @@ var expect = require('chai').expect,
33
sdk = require('postman-collection'),
44
fs = require('fs'),
55
path = require('path');
6-
// collectionsPath = './fixtures';
76

87
describe('convert function', function () {
8+
const collection = new sdk.Collection(JSON.parse(
9+
fs.readFileSync(path.resolve(__dirname, './fixtures/sample_collection.json').toString())));
910

10-
it('should convert a simple get request', function (done) {
11-
const collection = new sdk.Collection(JSON.parse(
12-
fs.readFileSync(path.resolve(__dirname, './fixtures/sample_collection.json').toString())));
11+
it('should convert requests with asyncType option as async', function (done) {
12+
collection.items.members.forEach((item) => {
13+
convert(item.request, { asyncType: 'async' }, function (err, snippet) {
14+
if (err) {
15+
console.error(err);
16+
}
17+
expect(snippet).to.not.be.empty;
18+
expect(snippet).to.include('$client->sendAsync(');
19+
});
20+
});
21+
done();
22+
});
23+
24+
it('should convert requests with asyncType option not present', function (done) {
25+
collection.items.members.forEach((item) => {
26+
convert(item.request, { }, function (err, snippet) {
27+
if (err) {
28+
console.error(err);
29+
}
30+
expect(snippet).to.not.be.empty;
31+
expect(snippet).to.include('$client->sendAsync(');
32+
});
33+
});
34+
done();
35+
});
36+
37+
it('should convert requests with sync option as sync', function (done) {
1338
collection.items.members.forEach((item) => {
1439
convert(item.request, { asyncType: 'sync' }, function (err, snippet) {
1540
if (err) {
1641
console.error(err);
1742
}
1843
expect(snippet).to.not.be.empty;
44+
expect(snippet).to.include('$client->send(');
45+
});
46+
});
47+
done();
48+
});
49+
50+
it('should convert requests with includeBoilerplate option as true', function (done) {
51+
collection.items.members.forEach((item) => {
52+
convert(item.request, { includeBoilerplate: true }, function (err, snippet) {
53+
if (err) {
54+
console.error(err);
55+
}
56+
expect(snippet).to.not.be.empty;
57+
expect(snippet).to.include('<?php\n' +
58+
'$composerHome = substr(shell_exec(\'composer config home -g\'), 0, -1).\'/vendor/autoload.php\';\n');
59+
});
60+
});
61+
done();
62+
});
63+
64+
it('should convert requests with includeBoilerplate option not present', function (done) {
65+
collection.items.members.forEach((item) => {
66+
convert(item.request, { }, function (err, snippet) {
67+
if (err) {
68+
console.error(err);
69+
}
70+
expect(snippet).to.not.be.empty;
71+
expect(snippet).to.not.include('<?php\n' +
72+
'$composerHome = substr(shell_exec(\'composer config home -g\'), 0, -1).\'/vendor/autoload.php\';\n');
73+
});
74+
});
75+
done();
76+
});
77+
78+
it('should convert requests with includeBoilerplate option as false', function (done) {
79+
collection.items.members.forEach((item) => {
80+
convert(item.request, { includeBoilerplate: false }, function (err, snippet) {
81+
if (err) {
82+
console.error(err);
83+
}
84+
expect(snippet).to.not.be.empty;
85+
expect(snippet).to.not.include('<?php\n' +
86+
'$composerHome = substr(shell_exec(\'composer config home -g\'), 0, -1).\'/vendor/autoload.php\';\n');
87+
});
88+
});
89+
done();
90+
});
91+
92+
it('should convert requests with followRedirect option not present', function (done) {
93+
collection.items.members.forEach((item) => {
94+
convert(item.request, { }, function (err, snippet) {
95+
if (err) {
96+
console.error(err);
97+
}
98+
expect(snippet).to.not.be.empty;
99+
expect(snippet).to.not.include('\'allow_redirects\' => false');
100+
});
101+
});
102+
done();
103+
});
104+
105+
it('should convert requests with followRedirect option as true', function (done) {
106+
collection.items.members.forEach((item) => {
107+
convert(item.request, { followRedirect: true }, function (err, snippet) {
108+
if (err) {
109+
console.error(err);
110+
}
111+
expect(snippet).to.not.be.empty;
112+
expect(snippet).to.not.include('\'allow_redirects\' => false');
113+
});
114+
});
115+
done();
116+
});
117+
118+
it('should convert requests with followRedirect option as false', function (done) {
119+
collection.items.members.forEach((item) => {
120+
convert(item.request, { followRedirect: false }, function (err, snippet) {
121+
if (err) {
122+
console.error(err);
123+
}
124+
expect(snippet).to.not.be.empty;
125+
expect(snippet).to.include('\'allow_redirects\' => false');
19126
});
20127
});
21128
done();
@@ -25,4 +132,44 @@ describe('convert function', function () {
25132
expect(function () { convert({}, {}); })
26133
.to.throw('Php-Guzzle~convert: Callback is not a function');
27134
});
135+
136+
it('should convert requests with requestTimeout option set as 500', function (done) {
137+
collection.items.members.forEach((item) => {
138+
convert(item.request, { requestTimeout: 500 }, function (err, snippet) {
139+
if (err) {
140+
console.error(err);
141+
}
142+
expect(snippet).to.not.be.empty;
143+
expect(snippet).to.include('\'timeout\' => 500');
144+
});
145+
});
146+
done();
147+
});
148+
149+
it('should convert requests with requestTimeout option not present', function (done) {
150+
collection.items.members.forEach((item) => {
151+
convert(item.request, { requestTimeout: 0 }, function (err, snippet) {
152+
if (err) {
153+
console.error(err);
154+
}
155+
expect(snippet).to.not.be.empty;
156+
expect(snippet).to.not.include('\'timeout\'');
157+
});
158+
});
159+
done();
160+
});
161+
162+
it('should convert requests with requestTimeout option set as 0', function (done) {
163+
collection.items.members.forEach((item) => {
164+
convert(item.request, { requestTimeout: 0 }, function (err, snippet) {
165+
if (err) {
166+
console.error(err);
167+
}
168+
expect(snippet).to.not.be.empty;
169+
expect(snippet).to.not.include('\'timeout\' => 0');
170+
});
171+
});
172+
done();
173+
});
174+
28175
});

codegens/php-guzzle/test/unit/options.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ var expect = require('chai').expect,
1414
},
1515
{
1616
4: 'followRedirect'
17-
}, {
17+
},
18+
{
1819
5: 'asyncType'
1920
}
2021
];

codegens/php-guzzle/test/unit/phpGuzzle.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,30 @@ describe('getSnippetHeaders function', function () {
119119
it('should return an empty string when headers is an empty array', function () {
120120
expect(getSnippetHeaders([], ' ')).to.equal('');
121121
});
122+
123+
it('should return an string representing the headers trim only values', function () {
124+
const headersArray =
125+
[
126+
{
127+
key: 'my-sample-header ',
128+
value: 'Lorem ipsum dolor sit amet '
129+
},
130+
{
131+
key: 'testing',
132+
value: '\'singlequotes\''
133+
},
134+
{
135+
key: 'TEST',
136+
value: '"doublequotes"'
137+
}
138+
],
139+
expectedString = '$headers = [\n' +
140+
' \'my-sample-header\' => \'Lorem ipsum dolor sit amet \',' +
141+
'\n \'testing\' => \'\\\'singlequotes\\\'\',' +
142+
'\n \'TEST\' => \'"doublequotes"\'' +
143+
'\n];\n';
144+
expect(getSnippetHeaders(headersArray, ' ')).to.equal(expectedString);
145+
});
122146
});
123147

124148
describe('getURL function', function () {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
const expect = require('chai').expect,
2+
{ sanitizeString, sanitizeOptions } = require('../../lib/util/sanitize'),
3+
{ getOptions } = require('../../lib/options');
4+
5+
describe('Sanitize function', function () {
6+
it('should return empty string when input is not a string type', function () {
7+
expect(sanitizeString(123, false)).to.equal('');
8+
expect(sanitizeString(null, false)).to.equal('');
9+
expect(sanitizeString({}, false)).to.equal('');
10+
expect(sanitizeString([], false)).to.equal('');
11+
});
12+
13+
it('should trim input string when needed', function () {
14+
expect(sanitizeString('inputString ', true)).to.equal('inputString');
15+
});
16+
});
17+
18+
describe('sanitizeOptions function', function () {
19+
let defaultOptions = {},
20+
testOptions = {},
21+
sanitizedOptions;
22+
23+
getOptions().forEach((option) => {
24+
defaultOptions[option.id] = {
25+
default: option.default,
26+
type: option.type
27+
};
28+
if (option.type === 'enum') {
29+
defaultOptions[option.id].availableOptions = option.availableOptions;
30+
}
31+
});
32+
33+
it('should remove option not supported by module', function () {
34+
testOptions.randomName = 'random value';
35+
sanitizedOptions = sanitizeOptions(testOptions, getOptions());
36+
expect(sanitizedOptions).to.not.have.property('randomName');
37+
});
38+
39+
it('should use defaults when option value type does not match with expected type', function () {
40+
testOptions = {};
41+
testOptions.indentCount = '5';
42+
testOptions.trimRequestBody = 'true';
43+
testOptions.indentType = 'tabSpace';
44+
sanitizedOptions = sanitizeOptions(testOptions, getOptions());
45+
expect(sanitizedOptions.indentCount).to.equal(defaultOptions.indentCount.default);
46+
expect(sanitizedOptions.indentType).to.equal(defaultOptions.indentType.default);
47+
expect(sanitizedOptions.trimRequestBody).to.equal(defaultOptions.trimRequestBody.default);
48+
});
49+
50+
it('should use defaults when option type is valid but value is invalid', function () {
51+
testOptions = {};
52+
testOptions.indentCount = -1;
53+
testOptions.indentType = 'spaceTab';
54+
testOptions.requestTimeout = -3000;
55+
sanitizedOptions = sanitizeOptions(testOptions, getOptions());
56+
expect(sanitizedOptions.indentCount).to.equal(defaultOptions.indentCount.default);
57+
expect(sanitizedOptions.indentType).to.equal(defaultOptions.indentType.default);
58+
expect(sanitizedOptions.requestTimeout).to.equal(defaultOptions.requestTimeout.default);
59+
});
60+
61+
it('should return the same object when default options are provided', function () {
62+
for (var id in defaultOptions) {
63+
if (defaultOptions.hasOwnProperty(id)) {
64+
testOptions[id] = defaultOptions[id].default;
65+
}
66+
}
67+
sanitizedOptions = sanitizeOptions(testOptions, getOptions());
68+
expect(sanitizedOptions).to.deep.equal(testOptions);
69+
});
70+
71+
it('should return the same object when valid (but not necessarily defaults) options are provided', function () {
72+
testOptions = {
73+
indentType: 'Tab',
74+
indentCount: 3,
75+
requestTimeout: 3000,
76+
trimRequestBody: true,
77+
asyncType: 'async',
78+
followRedirect: false,
79+
includeBoilerplate: true
80+
};
81+
sanitizedOptions = sanitizeOptions(testOptions, getOptions());
82+
expect(sanitizedOptions).to.deep.equal(testOptions);
83+
});
84+
});

0 commit comments

Comments
 (0)