ladjs / supertest

🕷 Super-agent driven library for testing node.js HTTP servers using a fluent API. Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[fix] Do no replace invalid body with an empty JSON object

katchengli opened this issue · comments

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: #189 (comment)
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

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.
commented

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)
  })
})

image

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