-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathconvert.test.js
260 lines (243 loc) · 8.66 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
var expect = require('chai').expect,
sdk = require('postman-collection'),
convert = require('../../index').convert,
getOptions = require('../../index').getOptions,
sanitize = require('../../lib/util').sanitize,
getUrlStringfromUrlObject = require('../../lib/util').getUrlStringfromUrlObject,
mainCollection = require('./fixtures/testcollection/collection.json');
describe('Ocaml unit tests', function () {
describe('convert function', function () {
var request = new sdk.Request(mainCollection.item[0].request),
snippetArray;
const SINGLE_SPACE = ' ';
it('should generate snippet with default options given no options', function () {
convert(request, function (error, snippet) {
if (error) {
expect.fail(null, null, error);
return;
}
snippetArray = snippet.split('\n');
for (var i = 0; i < snippetArray.length; i++) {
if (snippetArray[i].startsWith('let reqBody = ')) {
expect(snippetArray[i + 1].substr(0, 2)).to.equal(SINGLE_SPACE.repeat(2));
expect(snippetArray[i + 1].charAt(2)).to.not.equal(SINGLE_SPACE);
}
}
});
});
it('should generate snippet with Space as an indent type with exact indent count', function () {
convert(request, { indentType: 'Space', indentCount: 2 }, function (error, snippet) {
if (error) {
expect.fail(null, null, error);
return;
}
snippetArray = snippet.split('\n');
for (var i = 0; i < snippetArray.length; i++) {
if (snippetArray[i].startsWith('let reqBody = ')) {
expect(snippetArray[i + 1].substr(0, 2)).to.equal(SINGLE_SPACE.repeat(2));
expect(snippetArray[i + 1].charAt(2)).to.not.equal(SINGLE_SPACE);
}
}
});
});
// By default takes 2 spaces as indentCount and indentType
it('should generate snippet with default indent count for absent option indentCount', function () {
convert(request, { indentType: 'Space' }, function (error, snippet) {
if (error) {
expect.fail(null, null, error);
return;
}
snippetArray = snippet.split('\n');
for (var i = 0; i < snippetArray.length; i++) {
if (snippetArray[i].startsWith('let reqBody = ')) {
expect(snippetArray[i + 1].charAt(0)).to.equal(' ');
expect(snippetArray[i + 1].charAt(1)).to.equal(' ');
}
}
});
});
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('Header.add h "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('[| ("name", "json"); ("value", "{\\"hello\\": \\"world\\"}"); ("contentType", "application/json") |]'); // eslint-disable-line max-len
expect(snippet).to.contain('postData := if Array.length parameters.(x) == 3 then (');
expect(snippet).to.contain('let (_, contentType) = parameters.(x).(2) in');
expect(snippet).to.contain('!postData ^ accum ^ "\\r\\n" ^ "Content-Type: " ^ contentType');
});
});
it('should include graphql body in the snippet', function () {
var request = new sdk.Request({
'method': 'POST',
'header': [],
'body': {
'mode': 'graphql',
'graphql': {
'query': '{ body { graphql } }',
'variables': '{"variable_key": "variable_value"}'
}
},
'url': {
'raw': 'http://postman-echo.com/post',
'protocol': 'http',
'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('\\"query\\":\\"{ body { graphql } }\\"');
expect(snippet).to.include('\\"variables\\":{\\"variable_key\\":\\"variable_value\\"}');
});
});
it('should generate snippets(not error out) for requests with multiple/no file in formdata', function () {
var request = new sdk.Request({
'method': 'POST',
'header': [],
'body': {
'mode': 'formdata',
'formdata': [
{
'key': 'no file',
'value': '',
'type': 'file',
'src': []
},
{
'key': 'single file',
'value': '',
'type': 'file',
'src': '/test1.txt'
},
{
'key': 'multiple files',
'value': '',
'type': 'file',
'src': ['/test2.txt',
'/test3.txt']
},
{
'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('("name", "no file"); ("fileName", "/path/to/file")');
expect(snippet).to.include('("name", "single file"); ("fileName", "/test1.txt")');
expect(snippet).to.include('("name", "multiple files"); ("fileName", "/test2.txt")');
expect(snippet).to.include('("name", "multiple files"); ("fileName", "/test3.txt")');
expect(snippet).to.include('("name", "no src"); ("fileName", "/path/to/file")');
expect(snippet).to.include('("name", "invalid src"); ("fileName", "/path/to/file")');
});
});
});
describe('getOptions function', function () {
it('should return array of options for swift-urlsession converter', function () {
expect(getOptions()).to.be.an('array');
});
it('should return all the valid options', function () {
expect(getOptions()[0]).to.have.property('id', 'indentCount');
expect(getOptions()[1]).to.have.property('id', 'indentType');
expect(getOptions()[2]).to.have.property('id', 'trimRequestBody');
});
});
describe('sanitize function', function () {
it('should handle invalid parameters', function () {
expect(sanitize(123, 'raw', false)).to.equal('');
expect(sanitize('inputString', 123, 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');
});
});
});