Skip to content

Commit e0b0e10

Browse files
authored
Added body -> payload aliases tog match the rest of Fastify APIs (#28)
1 parent ae33c68 commit e0b0e10

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Injects a fake request into an HTTP server.
6060
- `headers` - an optional object containing request headers.
6161
- `remoteAddress` - an optional string specifying the client remote address. Defaults to `'127.0.0.1'`.
6262
- `payload` - an optional request payload. Can be a string, Buffer, Stream or object.
63+
- `body` - alias for payload.
6364
- `simulate` - an object containing flags to simulate various conditions:
6465
- `end` - indicates whether the request will fire an `end` event. Defaults to `undefined`, meaning an `end` event will fire.
6566
- `split` - indicates whether the request payload will be split into chunks. Defaults to `undefined`, meaning payload will not be chunked.
@@ -77,6 +78,7 @@ Injects a fake request into an HTTP server.
7778
- `statusCode` - the HTTP status code.
7879
- `statusMessage` - the HTTP status message.
7980
- `payload` - the payload as a UTF-8 encoded string.
81+
- `body` - alias for payload.
8082
- `rawPayload` - the raw payload as a Buffer.
8183
- `trailers` - an object containing the response trailers.
8284

lib/request.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ function Request (options) {
4444
remoteAddress: options.remoteAddress || '127.0.0.1'
4545
}
4646

47-
var payload = options.payload || null
47+
// we keep both payload and body for compatibility reasons
48+
var payload = options.payload || options.body || null
4849
if (payload && typeof payload !== 'string' && !(typeof payload.resume === 'function') && !Buffer.isBuffer(payload)) {
4950
payload = JSON.stringify(payload)
5051
this.headers['content-type'] = this.headers['content-type'] || 'application/json'

lib/response.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ function generatePayload (response) {
101101
// Prepare payload and trailers
102102
const rawBuffer = Buffer.concat(response._lightMyRequest.payloadChunks)
103103
res.rawPayload = rawBuffer
104+
105+
// we keep both of them for compatibility reasons
104106
res.payload = rawBuffer.toString()
107+
res.body = res.payload
105108
res.trailers = response._lightMyRequest.trailers
106109

107110
return res

test/test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,20 @@ test('echos object payload', (t) => {
384384
})
385385
})
386386

387+
test('supports body option in Request and property in Response', (t) => {
388+
t.plan(3)
389+
const dispatch = function (req, res) {
390+
res.writeHead(200, { 'content-type': req.headers['content-type'] })
391+
req.pipe(res)
392+
}
393+
394+
inject(dispatch, { method: 'POST', url: '/test', body: { a: 1 } }, (err, res) => {
395+
t.error(err)
396+
t.equal(res.headers['content-type'], 'application/json')
397+
t.equal(res.body, '{"a":1}')
398+
})
399+
})
400+
387401
test('echos buffer payload', (t) => {
388402
t.plan(2)
389403
const dispatch = function (req, res) {

0 commit comments

Comments
 (0)