-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathconverter.test.js
156 lines (149 loc) · 4.59 KB
/
converter.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
var expect = require('chai').expect,
sdk = require('postman-collection'),
mainCollection = require('./fixtures/sample_collection.json'),
convert = require('../../lib/index').convert;
describe('Python- Requests converter', function () {
it('should throw an error when callback is not function', function () {
expect(function () { convert({}, {}); })
.to.throw('Python-Requests~convert: Callback is not a function');
});
it('should not have allow_redirects=False twice in generated snippet when' +
' followRedirect option is set as false', function () {
var request = new sdk.Request(mainCollection.item[0].request),
options = { followRedirect: false, requestTimeout: 0 };
convert(request, options, function (err, snippet) {
if (err) {
expect.fail(null, null, err);
}
expect(snippet).to.be.a('string');
expect(snippet).to.not.include('allow_redirects=False, allow_redirects=false');
});
});
it('should have correct boolean value for allow_redirects(False, uppercased F) in generated snippet when' +
' followRedirect option is set as false', function () {
var request = new sdk.Request(mainCollection.item[0].request),
options = { followRedirect: false };
convert(request, options, function (err, snippet) {
if (err) {
expect.fail(null, null, err);
}
expect(snippet).to.be.a('string');
expect(snippet).to.include('allow_redirects=False');
expect(snippet).to.not.include('allow_redirects=false');
});
});
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('\'key_containing_whitespaces\': \' value_containing_whitespaces \'');
});
});
it('should convert JSON tokens into appropriate python tokens', function () {
var request = new sdk.Request({
'method': 'POST',
'header': [
{
'key': 'Content-Type',
'value': 'application/json',
'type': 'text'
}
],
'body': {
'mode': 'raw',
'raw': `{
"true": true,
"false": false,
"null": null
}`
},
'url': {
'raw': 'https://example.com',
'protocol': 'https',
'host': [
'example',
'com'
]
}
});
convert(request, {}, function (error, snippet) {
if (error) {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.include('"true": True');
expect(snippet).to.include('"false": False');
expect(snippet).to.include('"null": None');
});
});
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');
// eslint-disable-next-line max-len
expect(snippet).to.include('(\'no file\',(\'file\',open(\'/path/to/file\',\'rb\'),\'application/octet-stream\'))');
// eslint-disable-next-line max-len
expect(snippet).to.include('(\'no src\',(\'file\',open(\'/path/to/file\',\'rb\'),\'application/octet-stream\'))');
// eslint-disable-next-line max-len
expect(snippet).to.include('(\'invalid src\',(\'file\',open(\'/path/to/file\',\'rb\'),\'application/octet-stream\'))');
});
});
});