-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathconvert.test.js
221 lines (205 loc) · 6.84 KB
/
convert.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
var expect = require('chai').expect,
sdk = require('postman-collection'),
convert = require('../../index').convert,
getOptions = require('../../index').getOptions,
getUrlStringfromUrlObject = require('../../lib/util').getUrlStringfromUrlObject,
sanitize = require('../../lib/util').sanitize,
mainCollection = require('../../../../test/codegen/newman/fixtures/basicCollection.json');
describe('libcurl convert function', function () {
describe('convert function', function () {
it('should throw an error if callback is not a function', function () {
var request = null,
options = {
indentCount: 1,
indentType: 'Tab',
requestTimeout: 200,
trimRequestBody: true
},
callback = null;
expect(function () { convert(request, options, callback); }).to.throw(Error);
});
it('should set CURLOPT_TIMEOUT_MS parameter when requestTimeout is set to non zero value', function () {
var request = new sdk.Request(mainCollection.item[0].request),
options = { requestTimeout: 3000 };
convert(request, options, function (error, snippet) {
if (error) {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.include('curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 3000L);');
});
});
it('should trim header keys and not trim header values', function () {
var request = new sdk.Request({
'method': 'GET',
'header': [
{
'key': ' key_containing_whitespaces ',
'value': ' value_containing_whitespaces '
}
],
'url': {
'raw': 'https://google.com',
'protocol': 'https',
'host': [
'google',
'com'
]
}
});
convert(request, {}, function (error, snippet) {
if (error) {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.include('headers, "key_containing_whitespaces: ' +
' value_containing_whitespaces "');
});
});
it('should add content type if formdata field contains a content-type', function () {
var request = new sdk.Request({
'method': 'POST',
'body': {
'mode': 'formdata',
'formdata': [
{
'key': 'json',
'value': '{"hello": "world"}',
'contentType': 'application/json',
'type': 'text'
}
]
},
'url': {
'raw': 'http://postman-echo.com/post',
'host': [
'postman-echo',
'com'
],
'path': [
'post'
]
}
});
convert(request, {}, function (error, snippet) {
if (error) {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.contain('curl_mime_type(part, "application/json");');
});
});
it('should generate snippets for no files in form data', function () {
var request = new sdk.Request({
'method': 'POST',
'header': [],
'body': {
'mode': 'formdata',
'formdata': [
{
'key': 'no file',
'value': '',
'type': 'file',
'src': []
},
{
'key': 'no src',
'value': '',
'type': 'file'
},
{
'key': 'invalid src',
'value': '',
'type': 'file',
'src': {}
}
]
},
'url': {
'raw': 'https://postman-echo.com/post',
'protocol': 'https',
'host': [
'postman-echo',
'com'
],
'path': [
'post'
]
}
});
convert(request, {}, function (error, snippet) {
if (error) {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.include('curl_mime_name(part, "no file");');
expect(snippet).to.include('curl_mime_filedata(part, "/path/to/file");');
expect(snippet).to.include('curl_mime_name(part, "no src");');
expect(snippet).to.include('curl_mime_name(part, "invalid src");');
});
});
it('should free up headers list after request is sent', function () {
var request = new sdk.Request({
'method': 'GET',
'header': [
{
'key': 'Accept',
'value': 'application/json'
},
{
'key': 'Content-Type',
'value': 'application/json'
}
],
'url': {
'raw': 'https://google.com',
'protocol': 'https',
'host': [
'google',
'com'
]
}
});
convert(request, {}, function (error, snippet) {
if (error) {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.include('curl_slist_free_all(headers)');
});
});
});
describe('getOptions function', function () {
var options = getOptions();
it('should return an array of specific options', function () {
expect(options).to.be.an('array');
expect(options[0]).to.have.property('id', 'includeBoilerplate');
expect(options[1]).to.have.property('id', 'protocol');
expect(options[2]).to.have.property('id', 'indentCount');
expect(options[3]).to.have.property('id', 'indentType');
expect(options[4]).to.have.property('id', 'followRedirect');
expect(options[5]).to.have.property('id', 'trimRequestBody');
expect(options[6]).to.have.property('id', 'useMimeType');
});
});
describe('Sanitize function', function () {
it('should return empty string when input is not a string type', function () {
expect(sanitize(123, false)).to.equal('');
expect(sanitize(null, false)).to.equal('');
expect(sanitize({}, false)).to.equal('');
expect(sanitize([], false)).to.equal('');
});
it('should trim input string when needed', function () {
expect(sanitize('inputString ', true)).to.equal('inputString');
});
it('should not encode unresolved query params and ' +
'encode every other query param, both present together', function () {
let rawUrl = 'https://postman-echo.com/get?key1={{value}}&key2=\'a b+c\'',
urlObject = new sdk.Url(rawUrl),
outputUrlString = getUrlStringfromUrlObject(urlObject);
expect(outputUrlString).to.not.include('key1=%7B%7Bvalue%7B%7B');
expect(outputUrlString).to.not.include('key2=\'a b+c\'');
expect(outputUrlString).to.equal('https://postman-echo.com/get?key1={{value}}&key2=%27a%20b+c%27');
});
});
});