Skip to content

Commit a207126

Browse files
committed
add more tests
1 parent 9c0f0f4 commit a207126

9 files changed

Lines changed: 294 additions & 27 deletions

File tree

test/fixtures/har.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"log": {
3+
"version": "1.2",
4+
"creator": {
5+
"name": "HTTPSnippet",
6+
"version": "1.0.0"
7+
},
8+
"entries": [
9+
{
10+
"request": {
11+
"method": "GET",
12+
"url": "http://mockbin.com/har"
13+
}
14+
},
15+
{
16+
"request": {
17+
"method": "POST",
18+
"url": "http://mockbin.com/har"
19+
}
20+
}
21+
]
22+
}
23+
}

test/fixtures/mimetypes.json

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"multipart/mixed": {
3+
"method": "POST",
4+
"url": "http://mockbin.com/har",
5+
"postData": {
6+
"mimeType": "multipart/mixed"
7+
}
8+
},
9+
10+
"multipart/related": {
11+
"method": "POST",
12+
"url": "http://mockbin.com/har",
13+
"postData": {
14+
"mimeType": "multipart/related"
15+
}
16+
},
17+
18+
"multipart/form-data": {
19+
"method": "POST",
20+
"url": "http://mockbin.com/har",
21+
"postData": {
22+
"mimeType": "multipart/form-data"
23+
}
24+
},
25+
26+
"multipart/alternative": {
27+
"method": "POST",
28+
"url": "http://mockbin.com/har",
29+
"postData": {
30+
"mimeType": "multipart/alternative"
31+
}
32+
},
33+
34+
"application/x-www-form-urlencoded": {
35+
"method": "POST",
36+
"url": "http://mockbin.com/har",
37+
"postData": {
38+
"mimeType": "application/x-www-form-urlencoded"
39+
}
40+
},
41+
42+
"text/json": {
43+
"method": "POST",
44+
"url": "http://mockbin.com/har",
45+
"postData": {
46+
"mimeType": "text/json"
47+
}
48+
},
49+
50+
"text/x-json": {
51+
"method": "POST",
52+
"url": "http://mockbin.com/har",
53+
"postData": {
54+
"mimeType": "text/x-json"
55+
}
56+
},
57+
58+
"application/x-json": {
59+
"method": "POST",
60+
"url": "http://mockbin.com/har",
61+
"postData": {
62+
"mimeType": "application/x-json"
63+
}
64+
},
65+
66+
"invalid-json": {
67+
"method": "POST",
68+
"url": "http://mockbin.com/har",
69+
"postData": {
70+
"mimeType": "application/json",
71+
"text": "foo/bar"
72+
}
73+
}
74+
}

test/fixtures/output/php/http1/multipart-data.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
'content-type' => 'multipart/form-data; boundary=---011000010111000001101001'
99
));
1010

11-
$request->setBody('-----011000010111000001101001
12-
Content-Disposition: form-data; name="foo"; filename="hello.txt"
13-
Content-Type: text/plain
14-
15-
Hello World
11+
$request->setBody('-----011000010111000001101001
12+
Content-Disposition: form-data; name="foo"; filename="hello.txt"
13+
Content-Type: text/plain
14+
15+
Hello World
1616
-----011000010111000001101001--');
1717

1818
try {

test/fixtures/output/php/http1/multipart-file.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
'content-type' => 'multipart/form-data; boundary=---011000010111000001101001'
99
));
1010

11-
$request->setBody('-----011000010111000001101001
12-
Content-Disposition: form-data; name="foo"; filename="hello.txt"
13-
Content-Type: text/plain
14-
15-
11+
$request->setBody('-----011000010111000001101001
12+
Content-Disposition: form-data; name="foo"; filename="hello.txt"
13+
Content-Type: text/plain
14+
15+
1616
-----011000010111000001101001--');
1717

1818
try {

test/fixtures/output/php/http1/multipart-form-data.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
'content-type' => 'multipart/form-data; boundary=---011000010111000001101001'
99
));
1010

11-
$request->setBody('-----011000010111000001101001
12-
Content-Disposition: form-data; name="foo"
13-
14-
bar
11+
$request->setBody('-----011000010111000001101001
12+
Content-Disposition: form-data; name="foo"
13+
14+
bar
1515
-----011000010111000001101001--');
1616

1717
try {

test/index.js

Lines changed: 106 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,110 @@
55
var fixtures = require('./fixtures')
66
var HTTPSnippet = require('../src')
77

8-
require('should')
8+
var should = require('should')
99

1010
describe('HTTPSnippet', function () {
11+
it('should return false if no matching target', function (done) {
12+
var snippet = new HTTPSnippet(fixtures.requests.short)
13+
14+
snippet.convert(null).should.eql(false)
15+
16+
done()
17+
})
18+
19+
it('should fail validation', function (done) {
20+
var snippet;
21+
22+
/*eslint-disable no-wrap-func */
23+
(function () {
24+
snippet = new HTTPSnippet({yolo: 'foo'})
25+
}).should.throw(Array)
26+
27+
should.not.exist(snippet)
28+
29+
done()
30+
})
31+
32+
it('should parse HAR file with multiple entries', function (done) {
33+
var snippet = new HTTPSnippet(fixtures.har)
34+
35+
snippet.should.have.property('requests').and.be.an.Array
36+
snippet.requests.length.should.equal(2)
37+
38+
var results = snippet.convert('shell')
39+
40+
results.should.be.an.Array
41+
results.length.should.equal(2)
42+
43+
done()
44+
})
45+
46+
it('should convert multipart/mixed to multipart/form-data', function (done) {
47+
var req = new HTTPSnippet(fixtures.mimetypes['multipart/mixed']).requests[0]
48+
49+
req.postData.mimeType.should.eql('multipart/form-data')
50+
51+
done()
52+
})
53+
54+
it('should convert multipart/related to multipart/form-data', function (done) {
55+
var req = new HTTPSnippet(fixtures.mimetypes['multipart/related']).requests[0]
56+
57+
req.postData.mimeType.should.eql('multipart/form-data')
58+
59+
done()
60+
})
61+
62+
it('should convert multipart/alternative to multipart/form-data', function (done) {
63+
var req = new HTTPSnippet(fixtures.mimetypes['multipart/alternative']).requests[0]
64+
65+
req.postData.mimeType.should.eql('multipart/form-data')
66+
67+
done()
68+
})
69+
70+
it('should convert text/json to application/json', function (done) {
71+
var req = new HTTPSnippet(fixtures.mimetypes['text/json']).requests[0]
72+
73+
req.postData.mimeType.should.eql('application/json')
74+
75+
done()
76+
})
77+
78+
it('should convert text/x-json to application/json', function (done) {
79+
var req = new HTTPSnippet(fixtures.mimetypes['text/x-json']).requests[0]
80+
81+
req.postData.mimeType.should.eql('application/json')
82+
83+
done()
84+
})
85+
86+
it('should convert application/x-json to application/json', function (done) {
87+
var req = new HTTPSnippet(fixtures.mimetypes['application/x-json']).requests[0]
88+
89+
req.postData.mimeType.should.eql('application/json')
90+
91+
done()
92+
})
93+
94+
it('should gracefully fallback if not able to parse JSON', function (done) {
95+
var req = new HTTPSnippet(fixtures.mimetypes['invalid-json']).requests[0]
96+
97+
req.postData.mimeType.should.eql('text/plain')
98+
99+
done()
100+
})
101+
102+
it('should set postData.text = empty string when postData.params === undefined in application/x-www-form-urlencoded', function (done) {
103+
var req = new HTTPSnippet(fixtures.mimetypes['application/x-www-form-urlencoded']).requests[0]
104+
105+
req.postData.text.should.eql('')
106+
107+
done()
108+
})
109+
11110
it('should add "uriObj" to source object', function (done) {
12-
var req = new HTTPSnippet(fixtures.requests.query).source
111+
var req = new HTTPSnippet(fixtures.requests.query).requests[0]
13112

14113
req.uriObj.should.be.an.Object
15114
req.uriObj.should.eql({
@@ -38,7 +137,7 @@ describe('HTTPSnippet', function () {
38137
})
39138

40139
it('should add "queryObj" to source object', function (done) {
41-
var req = new HTTPSnippet(fixtures.requests.query).source
140+
var req = new HTTPSnippet(fixtures.requests.query).requests[0]
42141

43142
req.queryObj.should.be.an.Object
44143
req.queryObj.should.eql({
@@ -54,7 +153,7 @@ describe('HTTPSnippet', function () {
54153
})
55154

56155
it('should add "headersObj" to source object', function (done) {
57-
var req = new HTTPSnippet(fixtures.requests.headers).source
156+
var req = new HTTPSnippet(fixtures.requests.headers).requests[0]
58157

59158
req.headersObj.should.be.an.Object
60159
req.headersObj.should.eql({
@@ -66,7 +165,7 @@ describe('HTTPSnippet', function () {
66165
})
67166

68167
it('should modify orignal url to strip query string', function (done) {
69-
var req = new HTTPSnippet(fixtures.requests.query).source
168+
var req = new HTTPSnippet(fixtures.requests.query).requests[0]
70169

71170
req.url.should.be.a.String
72171
req.url.should.eql('http://mockbin.com/har')
@@ -75,7 +174,7 @@ describe('HTTPSnippet', function () {
75174
})
76175

77176
it('should add "fullUrl" to source object', function (done) {
78-
var req = new HTTPSnippet(fixtures.requests.query).source
177+
var req = new HTTPSnippet(fixtures.requests.query).requests[0]
79178

80179
req.fullUrl.should.be.a.String
81180
req.fullUrl.should.eql('http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value')
@@ -84,7 +183,7 @@ describe('HTTPSnippet', function () {
84183
})
85184

86185
it('should fix "path" property of "uriObj" to match queryString', function (done) {
87-
var req = new HTTPSnippet(fixtures.requests.query).source
186+
var req = new HTTPSnippet(fixtures.requests.query).requests[0]
88187

89188
req.uriObj.path.should.be.a.String
90189
req.uriObj.path.should.eql('/har?foo=bar&foo=baz&baz=abc&key=value')

test/tests/javascript/xhr.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/* global it */
2+
13
'use strict'
24

3-
module.exports = function (snippet, fixtures) {}
5+
require('should')
6+
7+
module.exports = function (HTTPSnippet, fixtures) {
8+
it('should not use cors', function () {
9+
var result = new HTTPSnippet(fixtures.requests.short).convert('javascript', 'xhr', {
10+
cors: false
11+
})
12+
13+
result.should.be.a.String
14+
result.replace(/\n/g, '').should.eql('var data = null;var xhr = new XMLHttpRequest();xhr.addEventListener("readystatechange", function () { if (this.readyState === this.DONE) { console.log(this.responseText); }});xhr.open("GET", "http://mockbin.com/har");xhr.send(data);')
15+
})
16+
}

0 commit comments

Comments
 (0)