supertest
supertest copied to clipboard
[fix] Do no replace invalid body with an empty JSON object
Describe the bug
Node.js version: v18.8.0
OS version:
Description: SuperTest doesn't seem to pass the body as is. In particular, if it is invalid, it sends an empty JSON object to the endpoint.
Is this intended? It seems like it would be useful to accept invalid bodies to test the behaviour in those cases.
Actual behavior
ie.
let res = await request.post('/hello/').send('bye');
Endpoint receives a request that has a body of {}
Expected behavior
let res = await request.post('/hello/').send('bye');
Endpoint receives a request that has a body of 'bye'
, even if it is technically invalid.
Code to reproduce
Behaviour seems similar to what was reported here too: https://github.com/ladjs/supertest/issues/189#issuecomment-826861224
It may be of note that the endpoint receives an empty JSON object when the request body is being parsed by a middleware (both body-parser
or express
' parser).
Checklist
- [x] I have searched through GitHub issues for similar issues.
- [x] I have completely read through the README and documentation.
- [x] I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.
tldr: set 'Content-Type' before you send data 'bye'.
const request = require('supertest')
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
app.use(bodyParser.text())
app.post('/hello', (req, res) => {
res.end(req.body)
})
describe('/hello', function(){
it('Send Normal Text', function(done) {
request(app).post('/hello').set('Content-Type', 'text/plain').send('bye').expect(200, 'bye', done)
})
})

ref: https://github.com/ladjs/superagent/blob/master/src/request-base.js#L646-L692