request/tests/test-multipart-encoding.js

Maintainability

73.56

Lines of code

149

Created with Raphaël 2.1.002550751002015-5-18

2015-5-18
Maintainability: 73.56

Created with Raphaël 2.1.00501001502002015-5-18

2015-5-18
Lines of Code: 149

Difficulty

36.05

Estimated Errors

1.49

Function weight

By Complexity

Created with Raphaël 2.1.0<anonymous>3

By SLOC

Created with Raphaël 2.1.0runTest58
1
'use strict'
Column: 0 "Use the function form of "use strict"."
Column: 1 "Missing semicolon."
2
 
3
var http = require('http')
Column: 26 "Bad line breaking before ','."
Column: 12 "'require' is not defined."
4
  , path = require('path')
Column: 3 "Comma warnings can be turned off with 'laxcomma'."
Column: 26 "Bad line breaking before ','."
Column: 12 "'require' is not defined."
5
  , request = require('../index')
Column: 33 "Bad line breaking before ','."
Column: 15 "'require' is not defined."
6
  , fs = require('fs')
Column: 22 "Bad line breaking before ','."
Column: 10 "'require' is not defined."
7
  , tape = require('tape')
Column: 27 "Missing semicolon."
Column: 12 "'require' is not defined."
8
 
9
 
10
var localFile = path.join(__dirname, 'unicycle.jpg')
Column: 53 "Missing semicolon."
Column: 27 "'__dirname' is not defined."
11
var cases = {
12
  // based on body type
13
  '+array -stream': {
14
    options: {
15
      multipart: [{name: 'field', body: 'value'}]
16
    },
17
    expected: {chunked: false}
18
  },
19
  '+array +stream': {
20
    options: {
21
      multipart: [{name: 'file', body: null}]
22
    },
23
    expected: {chunked: true}
24
  },
25
  // encoding overrides body value
26
  '+array +encoding': {
27
    options: {
28
      headers: {'transfer-encoding': 'chunked'},
29
      multipart: [{name: 'field', body: 'value'}]
30
    },
31
    expected: {chunked: true}
32
  },
33
 
34
  // based on body type
35
  '+object -stream': {
36
    options: {
37
      multipart: {data: [{name: 'field', body: 'value'}]}
38
    },
39
    expected: {chunked: false}
40
  },
41
  '+object +stream': {
42
    options: {
43
      multipart: {data: [{name: 'file', body: null}]}
44
    },
45
    expected: {chunked: true}
46
  },
47
  // encoding overrides body value
48
  '+object +encoding': {
49
    options: {
50
      headers: {'transfer-encoding': 'chunked'},
51
      multipart: {data: [{name: 'field', body: 'value'}]}
52
    },
53
    expected: {chunked: true}
54
  },
55
 
56
  // based on body type
57
  '+object -chunked -stream': {
58
    options: {
59
      multipart: {chunked: false, data: [{name: 'field', body: 'value'}]}
60
    },
61
    expected: {chunked: false}
62
  },
63
  '+object -chunked +stream': {
64
    options: {
65
      multipart: {chunked: false, data: [{name: 'file', body: null}]}
66
    },
67
    expected: {chunked: true}
68
  },
69
  // chunked overrides body value
70
  '+object +chunked -stream': {
71
    options: {
72
      multipart: {chunked: true, data: [{name: 'field', body: 'value'}]}
73
    },
74
    expected: {chunked: true}
75
  },
76
  // encoding overrides chunked
77
  '+object +encoding -chunked': {
78
    options: {
79
      headers: {'transfer-encoding': 'chunked'},
80
      multipart: {chunked: false, data: [{name: 'field', body: 'value'}]}
81
    },
82
    expected: {chunked: true}
83
  }
84
}
Column: 2 "Missing semicolon."
85
 
86
function runTest(t, test) {
87
 
88
  var server = http.createServer(function(req, res) {
89
 
90
    t.ok(req.headers['content-type'].match(/^multipart\/related; boundary=[^\s;]+$/))
Column: 86 "Missing semicolon."
91
 
92
    if (test.expected.chunked) {
93
      t.ok(req.headers['transfer-encoding'] === 'chunked')
Column: 59 "Missing semicolon."
94
      t.notOk(req.headers['content-length'])
Column: 45 "Missing semicolon."
95
    } else {
96
      t.ok(req.headers['content-length'])
Column: 42 "Missing semicolon."
97
      t.notOk(req.headers['transfer-encoding'])
Column: 48 "Missing semicolon."
98
    }
99
 
100
    // temp workaround
101
    var data = ''
Column: 18 "Missing semicolon."
102
    req.setEncoding('utf8')
Column: 28 "Missing semicolon."
103
 
104
    req.on('data', function(d) {
105
      data += d
Column: 16 "Missing semicolon."
106
    })
Column: 7 "Missing semicolon."
107
 
108
    req.on('end', function() {
109
      // check for the fields traces
110
      if (test.expected.chunked && data.indexOf('name: file') !== -1) {
111
        // file
112
        t.ok(data.indexOf('name: file') !== -1)
Column: 48 "Missing semicolon."
113
        // check for unicycle.jpg traces
114
        t.ok(data.indexOf('2005:06:21 01:44:12') !== -1)
Column: 57 "Missing semicolon."
115
      } else {
116
        // field
117
        t.ok(data.indexOf('name: field') !== -1)
Column: 49 "Missing semicolon."
118
        var parts = test.options.multipart.data || test.options.multipart
Column: 74 "Missing semicolon."
119
        t.ok(data.indexOf(parts[0].body) !== -1)
Column: 49 "Missing semicolon."
120
      }
121
 
122
      res.writeHead(200)
Column: 25 "Missing semicolon."
123
      res.end()
Column: 16 "Missing semicolon."
124
    })
Column: 7 "Missing semicolon."
125
  })
Column: 5 "Missing semicolon."
126
 
127
  server.listen(6767, function() {
128
    // @NOTE: multipartData properties must be set here
129
    // so that file read stream does not leak in node v0.8
130
    var parts = test.options.multipart.data || test.options.multipart
Column: 70 "Missing semicolon."
131
    if (parts[0].name === 'file') {
132
      parts[0].body = fs.createReadStream(localFile)
Column: 53 "Missing semicolon."
133
    }
134
 
135
    request.post('http://localhost:6767', test.options, function (err, res, body) {
136
      t.equal(err, null)
Column: 25 "Missing semicolon."
137
      t.equal(res.statusCode, 200)
Column: 35 "Missing semicolon."
138
      server.close(function () {
139
        t.end()
Column: 16 "Missing semicolon."
140
      })
Column: 9 "Missing semicolon."
141
    })
Column: 7 "Missing semicolon."
142
  })
Column: 5 "Missing semicolon."
143
}
144
 
145
Object.keys(cases).forEach(function (name) {
146
  tape('multipart-encoding ' + name, function(t) {
147
    runTest(t, cases[name])
Column: 28 "Missing semicolon."
148
  })
Column: 5 "Missing semicolon."
149
})
Column: 3 "Missing semicolon."