-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathindex.js
More file actions
195 lines (137 loc) · 4.94 KB
/
index.js
File metadata and controls
195 lines (137 loc) · 4.94 KB
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
/* global describe, it */
'use strict'
var fixtures = require('./fixtures')
var HTTPSnippet = require('../src')
var should = require('should')
describe('HTTPSnippet', function () {
it('should return false if no matching target', function (done) {
var snippet = new HTTPSnippet(fixtures.requests.short)
snippet.convert(null).should.eql(false)
done()
})
it('should fail validation', function (done) {
var snippet;
/* eslint-disable no-extra-parens */
(function () {
snippet = new HTTPSnippet({yolo: 'foo'})
}).should.throw(Error)
should.not.exist(snippet)
done()
})
it('should parse HAR file with multiple entries', function (done) {
var snippet = new HTTPSnippet(fixtures.har)
snippet.should.have.property('requests').and.be.an.Array()
snippet.requests.length.should.equal(2)
var results = snippet.convert('shell')
results.should.be.an.Array()
results.length.should.equal(2)
done()
})
it('should convert multipart/mixed to multipart/form-data', function (done) {
var req = new HTTPSnippet(fixtures.mimetypes['multipart/mixed']).requests[0]
req.postData.mimeType.should.eql('multipart/form-data')
done()
})
it('should convert multipart/related to multipart/form-data', function (done) {
var req = new HTTPSnippet(fixtures.mimetypes['multipart/related']).requests[0]
req.postData.mimeType.should.eql('multipart/form-data')
done()
})
it('should convert multipart/alternative to multipart/form-data', function (done) {
var req = new HTTPSnippet(fixtures.mimetypes['multipart/alternative']).requests[0]
req.postData.mimeType.should.eql('multipart/form-data')
done()
})
it('should convert text/json to application/json', function (done) {
var req = new HTTPSnippet(fixtures.mimetypes['text/json']).requests[0]
req.postData.mimeType.should.eql('application/json')
done()
})
it('should convert text/x-json to application/json', function (done) {
var req = new HTTPSnippet(fixtures.mimetypes['text/x-json']).requests[0]
req.postData.mimeType.should.eql('application/json')
done()
})
it('should convert application/x-json to application/json', function (done) {
var req = new HTTPSnippet(fixtures.mimetypes['application/x-json']).requests[0]
req.postData.mimeType.should.eql('application/json')
done()
})
it('should gracefully fallback if not able to parse JSON', function (done) {
var req = new HTTPSnippet(fixtures.mimetypes['invalid-json']).requests[0]
req.postData.mimeType.should.eql('text/plain')
done()
})
it('should set postData.text = empty string when postData.params === undefined in application/x-www-form-urlencoded', function (done) {
var req = new HTTPSnippet(fixtures.mimetypes['application/x-www-form-urlencoded']).requests[0]
req.postData.text.should.eql('')
done()
})
it('should add "uriObj" to source object', function (done) {
var req = new HTTPSnippet(fixtures.requests.query).requests[0]
req.uriObj.should.be.an.Object()
should.config.checkProtoEql = false
req.uriObj.should.be.eql({
auth: null,
hash: null,
host: 'mockbin.com',
hostname: 'mockbin.com',
href: 'http://mockbin.com/har?key=value',
path: '/har?foo=bar&foo=baz&baz=abc&key=value',
pathname: '/har',
port: null,
protocol: 'http:',
query: {
baz: 'abc',
key: 'value',
foo: [
'bar',
'baz'
]
},
search: 'foo=bar&foo=baz&baz=abc&key=value',
slashes: true
})
done()
})
it('should add "queryObj" to source object', function (done) {
var req = new HTTPSnippet(fixtures.requests.query).requests[0]
req.queryObj.should.be.an.Object()
req.queryObj.should.eql({
baz: 'abc',
key: 'value',
foo: [
'bar',
'baz'
]
})
done()
})
it('should add "headersObj" to source object', function (done) {
var req = new HTTPSnippet(fixtures.requests.headers).requests[0]
req.headersObj.should.be.an.Object()
req.headersObj.should.eql({
'accept': 'application/json',
'x-foo': 'Bar'
})
done()
})
it('should modify orignal url to strip query string', function (done) {
var req = new HTTPSnippet(fixtures.requests.query).requests[0]
req.url.should.be.a.String()
req.url.should.eql('http://mockbin.com/har')
done()
})
it('should add "fullUrl" to source object', function (done) {
var req = new HTTPSnippet(fixtures.requests.query).requests[0]
req.fullUrl.should.be.a.String()
req.fullUrl.should.eql('http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value')
done()
})
it('should fix "path" property of "uriObj" to match queryString', function (done) {
var req = new HTTPSnippet(fixtures.requests.query).requests[0]
req.uriObj.path.should.be.a.String()
req.uriObj.path.should.eql('/har?foo=bar&foo=baz&baz=abc&key=value')
done()
})
})