Reputation: 1
This is my headers postman:
This is my body form-data in postman:
I am already trying like this
describe('test', function() {
describe('Get User Data', function() {
it.only('bla bla bla', function(done) {
// Send some Form Data
chai
.request(app)
.post('/user/login')
.send({ username: 'ihdisetiawan', password: 'testing123' })
.set('Accept', 'application/json')
.set('x-api-key', '1234567890')
.end(function(err, res) {
expect(res.status).to.equal(responseMessageCode.successOk);
// expect(res.password).to.equal("testing123");
done();
});
});
});
});
the response get 422
Uncaught AssertionError: expected 422 to equal 200
+ expected - actual
-422
+200
Upvotes: 0
Views: 2921
Reputation: 102537
You need to use multer middleware for handling the request payload with multipart/form-data
content type.
Besides, your case only handle a text-only multipart form, you should use the .none()
method of multer
.
E.g.
server.ts
:
import express from 'express';
import multer from 'multer';
const app = express();
const port = 3000;
const upload = multer();
app.post('/user/login', upload.none(), (req, res) => {
console.log('username:', req.body.username);
console.log('password:', req.body.password);
console.log('content-type:', req.get('Content-Type'));
console.log('x-api-key:', req.get('x-api-key'));
res.sendStatus(200);
});
if (require.main === module) {
app.listen(port, () => {
console.log(`HTTP server is listening on http://localhost:${port}`);
});
}
export { app };
server.test.ts
:
import { app } from './server';
import request from 'supertest';
import { expect } from 'chai';
describe('test', () => {
describe('Get User Data', () => {
it('bla bla bla', (done) => {
request(app)
.post('/user/login')
.field({
username: 'ihdisetiawan',
password: 'testing123',
})
.set('x-api-key', '1234567890')
.end((err, res) => {
expect(res.status).to.equal(200);
done();
});
});
});
});
integration test results with coverage report:
test
Get User Data
username: ihdisetiawan
password: testing123
content-type: multipart/form-data; boundary=--------------------------481112684938590474892724
x-api-key: 1234567890
✓ bla bla bla (47ms)
1 passing (66ms)
-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 86.67 | 50 | 50 | 86.67 |
server.ts | 86.67 | 50 | 50 | 86.67 | 16,17
-----------|---------|----------|---------|---------|-------------------
Upvotes: 1