forked from request/request
/
test-multipart-encoding.js
149 lines (133 loc) · 3.72 KB
/
test-multipart-encoding.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
'use strict'
var http = require('http')
, path = require('path')
, request = require('../index')
, fs = require('fs')
, tape = require('tape')
var localFile = path.join(__dirname, 'unicycle.jpg')
var cases = {
// based on body type
'+array -stream': {
options: {
multipart: [{name: 'field', body: 'value'}]
},
expected: {chunked: false}
},
'+array +stream': {
options: {
multipart: [{name: 'file', body: null}]
},
expected: {chunked: true}
},
// encoding overrides body value
'+array +encoding': {
options: {
headers: {'transfer-encoding': 'chunked'},
multipart: [{name: 'field', body: 'value'}]
},
expected: {chunked: true}
},
// based on body type
'+object -stream': {
options: {
multipart: {data: [{name: 'field', body: 'value'}]}
},
expected: {chunked: false}
},
'+object +stream': {
options: {
multipart: {data: [{name: 'file', body: null}]}
},
expected: {chunked: true}
},
// encoding overrides body value
'+object +encoding': {
options: {
headers: {'transfer-encoding': 'chunked'},
multipart: {data: [{name: 'field', body: 'value'}]}
},
expected: {chunked: true}
},
// based on body type
'+object -chunked -stream': {
options: {
multipart: {chunked: false, data: [{name: 'field', body: 'value'}]}
},
expected: {chunked: false}
},
'+object -chunked +stream': {
options: {
multipart: {chunked: false, data: [{name: 'file', body: null}]}
},
expected: {chunked: true}
},
// chunked overrides body value
'+object +chunked -stream': {
options: {
multipart: {chunked: true, data: [{name: 'field', body: 'value'}]}
},
expected: {chunked: true}
},
// encoding overrides chunked
'+object +encoding -chunked': {
options: {
headers: {'transfer-encoding': 'chunked'},
multipart: {chunked: false, data: [{name: 'field', body: 'value'}]}
},
expected: {chunked: true}
}
}
function runTest(t, test) {
var server = http.createServer(function(req, res) {
t.ok(req.headers['content-type'].match(/^multipart\/related; boundary=[^\s;]+$/))
if (test.expected.chunked) {
t.ok(req.headers['transfer-encoding'] === 'chunked')
t.notOk(req.headers['content-length'])
} else {
t.ok(req.headers['content-length'])
t.notOk(req.headers['transfer-encoding'])
}
// temp workaround
var data = ''
req.setEncoding('utf8')
req.on('data', function(d) {
data += d
})
req.on('end', function() {
// check for the fields traces
if (test.expected.chunked && data.indexOf('name: file') !== -1) {
// file
t.ok(data.indexOf('name: file') !== -1)
// check for unicycle.jpg traces
t.ok(data.indexOf('2005:06:21 01:44:12') !== -1)
} else {
// field
t.ok(data.indexOf('name: field') !== -1)
var parts = test.options.multipart.data || test.options.multipart
t.ok(data.indexOf(parts[0].body) !== -1)
}
res.writeHead(200)
res.end()
})
})
server.listen(6767, function() {
// @NOTE: multipartData properties must be set here
// so that file read stream does not leak in node v0.8
var parts = test.options.multipart.data || test.options.multipart
if (parts[0].name === 'file') {
parts[0].body = fs.createReadStream(localFile)
}
request.post('http://localhost:6767', test.options, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
server.close(function () {
t.end()
})
})
})
}
Object.keys(cases).forEach(function (name) {
tape('multipart-encoding ' + name, function(t) {
runTest(t, cases[name])
})
})