File: test-body.js

package info (click to toggle)
node-request 2.88.1-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 960 kB
  • sloc: javascript: 9,687; sh: 46; makefile: 2
file content (154 lines) | stat: -rw-r--r-- 3,952 bytes parent folder | download | duplicates (3)
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
'use strict'

var server = require('./server')
var request = require('../index')
var tape = require('tape')
var http = require('http')

var s = server.createServer()

tape('setup', function (t) {
  s.listen(0, function () {
    t.end()
  })
})

function addTest (name, data) {
  tape('test ' + name, function (t) {
    s.on('/' + name, data.resp)
    data.uri = s.url + '/' + name
    request(data, function (err, resp, body) {
      t.equal(err, null)
      if (data.expectBody && Buffer.isBuffer(data.expectBody)) {
        t.deepEqual(data.expectBody.toString(), body.toString())
      } else if (data.expectBody) {
        t.deepEqual(data.expectBody, body)
      }
      t.end()
    })
  })
}

addTest('testGet', {
  resp: server.createGetResponse('TESTING!'), expectBody: 'TESTING!'
})

addTest('testGetChunkBreak', {
  resp: server.createChunkResponse(
    [ Buffer.from([239]),
      Buffer.from([163]),
      Buffer.from([191]),
      Buffer.from([206]),
      Buffer.from([169]),
      Buffer.from([226]),
      Buffer.from([152]),
      Buffer.from([131])
    ]),
  expectBody: '\uF8FF\u03A9\u2603'
})

addTest('testGetBuffer', {
  resp: server.createGetResponse(Buffer.from('TESTING!')), encoding: null, expectBody: Buffer.from('TESTING!')
})

addTest('testGetEncoding', {
  resp: server.createGetResponse(Buffer.from('efa3bfcea9e29883', 'hex')), encoding: 'hex', expectBody: 'efa3bfcea9e29883'
})

addTest('testGetUTF', {
  resp: server.createGetResponse(Buffer.from([0xEF, 0xBB, 0xBF, 226, 152, 131])), encoding: 'utf8', expectBody: '\u2603'
})

addTest('testGetJSON', {
  resp: server.createGetResponse('{"test":true}', 'application/json'), json: true, expectBody: {'test': true}
})

addTest('testPutString', {
  resp: server.createPostValidator('PUTTINGDATA'), method: 'PUT', body: 'PUTTINGDATA'
})

addTest('testPutBuffer', {
  resp: server.createPostValidator('PUTTINGDATA'), method: 'PUT', body: Buffer.from('PUTTINGDATA')
})

addTest('testPutJSON', {
  resp: server.createPostValidator(JSON.stringify({foo: 'bar'})), method: 'PUT', json: {foo: 'bar'}
})

addTest('testPutMultipart', {
  resp: server.createPostValidator(
    '--__BOUNDARY__\r\n' +
    'content-type: text/html\r\n' +
    '\r\n' +
    '<html><body>Oh hi.</body></html>' +
    '\r\n--__BOUNDARY__\r\n\r\n' +
    'Oh hi.' +
    '\r\n--__BOUNDARY__--'
  ),
  method: 'PUT',
  multipart: [ {'content-type': 'text/html', 'body': '<html><body>Oh hi.</body></html>'},
    {'body': 'Oh hi.'}
  ]
})

addTest('testPutMultipartPreambleCRLF', {
  resp: server.createPostValidator(
    '\r\n--__BOUNDARY__\r\n' +
    'content-type: text/html\r\n' +
    '\r\n' +
    '<html><body>Oh hi.</body></html>' +
    '\r\n--__BOUNDARY__\r\n\r\n' +
    'Oh hi.' +
    '\r\n--__BOUNDARY__--'
  ),
  method: 'PUT',
  preambleCRLF: true,
  multipart: [ {'content-type': 'text/html', 'body': '<html><body>Oh hi.</body></html>'},
    {'body': 'Oh hi.'}
  ]
})

addTest('testPutMultipartPostambleCRLF', {
  resp: server.createPostValidator(
    '\r\n--__BOUNDARY__\r\n' +
    'content-type: text/html\r\n' +
    '\r\n' +
    '<html><body>Oh hi.</body></html>' +
    '\r\n--__BOUNDARY__\r\n\r\n' +
    'Oh hi.' +
    '\r\n--__BOUNDARY__--' +
    '\r\n'
  ),
  method: 'PUT',
  preambleCRLF: true,
  postambleCRLF: true,
  multipart: [ {'content-type': 'text/html', 'body': '<html><body>Oh hi.</body></html>'},
    {'body': 'Oh hi.'}
  ]
})

tape('typed array', function (t) {
  var server = http.createServer()
  server.on('request', function (req, res) {
    req.pipe(res)
  })
  server.listen(0, function () {
    var data = new Uint8Array([1, 2, 3])
    request({
      uri: 'http://localhost:' + this.address().port,
      method: 'POST',
      body: data,
      encoding: null
    }, function (err, res, body) {
      t.error(err)
      t.deepEqual(Buffer.from(data), body)
      server.close(t.end)
    })
  })
})

tape('cleanup', function (t) {
  s.close(function () {
    t.end()
  })
})