Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
3 tasks done
katchengli opened this issue Apr 6, 2023 · 1 comment
Open
3 tasks done

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

katchengli opened this issue Apr 6, 2023 · 1 comment
Labels

Comments

@katchengli
Copy link

katchengli commented Apr 6, 2023

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.
@katchengli katchengli added the bug label Apr 6, 2023
@xqin
Copy link

xqin commented Apr 21, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants