MCPcopy Index your code
hub / github.com/request/request / runTest

Function runTest

tests/test-form-data.js:10–121  ·  view source on GitHub ↗
(t, options)

Source from the content-addressed store, hash-verified

8var tape = require('tape')
9
10function runTest (t, options) {
11 var remoteFile = path.join(__dirname, 'googledoodle.jpg')
12 var localFile = path.join(__dirname, 'unicycle.jpg')
13 var multipartFormData = {}
14
15 var server = http.createServer(function (req, res) {
16 if (req.url === '/file') {
17 res.writeHead(200, {'content-type': 'image/jpg', 'content-length': 7187})
18 res.end(fs.readFileSync(remoteFile), 'binary')
19 return
20 }
21
22 if (options.auth) {
23 if (!req.headers.authorization) {
24 res.writeHead(401, {'www-authenticate': 'Basic realm="Private"'})
25 res.end()
26 return
27 } else {
28 t.ok(req.headers.authorization === 'Basic ' + Buffer.from('user:pass').toString('base64'))
29 }
30 }
31
32 t.ok(/multipart\/form-data; boundary=--------------------------\d+/
33 .test(req.headers['content-type']))
34
35 // temp workaround
36 var data = ''
37 req.setEncoding('utf8')
38
39 req.on('data', function (d) {
40 data += d
41 })
42
43 req.on('end', function () {
44 // check for the fields' traces
45
46 // 1st field : my_field
47 t.ok(data.indexOf('form-data; name="my_field"') !== -1)
48 t.ok(data.indexOf(multipartFormData.my_field) !== -1)
49
50 // 2nd field : my_buffer
51 t.ok(data.indexOf('form-data; name="my_buffer"') !== -1)
52 t.ok(data.indexOf(multipartFormData.my_buffer) !== -1)
53
54 // 3rd field : my_file
55 t.ok(data.indexOf('form-data; name="my_file"') !== -1)
56 t.ok(data.indexOf('; filename="' + path.basename(multipartFormData.my_file.path) + '"') !== -1)
57 // check for unicycle.jpg traces
58 t.ok(data.indexOf('2005:06:21 01:44:12') !== -1)
59 t.ok(data.indexOf('Content-Type: ' + mime.lookup(multipartFormData.my_file.path)) !== -1)
60
61 // 4th field : remote_file
62 t.ok(data.indexOf('form-data; name="remote_file"') !== -1)
63 t.ok(data.indexOf('; filename="' + path.basename(multipartFormData.remote_file.path) + '"') !== -1)
64
65 // 5th field : file with metadata
66 t.ok(data.indexOf('form-data; name="secret_file"') !== -1)
67 t.ok(data.indexOf('Content-Disposition: form-data; name="secret_file"; filename="topsecret.jpg"') !== -1)

Callers 1

test-form-data.jsFile · 0.70

Calls 1

requestFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…