supertest icon indicating copy to clipboard operation
supertest copied to clipboard

set content-type text/plain not working in supertest

Open kedar9444 opened this issue 5 years ago • 2 comments

asdsad

kedar9444 avatar Nov 27 '19 06:11 kedar9444

Does super test work with anything other than JSON?

john-doherty avatar Jul 10 '20 19:07 john-doherty

The first is to confirm that the request interface supports text/plain parameter requests, and the second is to set the request type to text/plain in the request set

Here is the nest.js test case, the verification is successful.

import { Test, TestingModule } from '@nestjs/testing'
import { INestApplication } from '@nestjs/common'
import * as request from 'supertest'
import { AppModule } from '../src/app.module'
import * as bodyParser from 'body-parser'

describe('AppController (e2e)', () => {
  let app: INestApplication

  beforeEach(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [AppModule.forRoot()],
    }).compile()

    app = moduleFixture.createNestApplication()
    app.use(bodyParser.text()) // This line will let bodyParser parse the contents of the text/plain type
    await app.init()
  })
  it('/test/txt_plain (POST)', () => {
    return request(app.getHttpServer())
      .post('/test/txt_plain')
      .set('Content-Type', 'text/plain')
      .send('\n纯文本测试\n')
      .expect(201)
      .expect({
        success: true,
        data: {},
      })
  })
})


mowatermelon avatar Aug 25 '23 13:08 mowatermelon