supertest
supertest copied to clipboard
set content-type text/plain not working in supertest
asdsad
Does super test work with anything other than JSON?
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: {},
})
})
})