My OAuth middleware expects to get parameters in req.params.
request(app)
.post('/api/token')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send({grant_type:'password'})
.expect(200)
.send({grant_type:'password'})
doesn't help. I don't know what it is used for.
My OAuth middleware expects to get parameters in req.params.
request(app)
.post('/api/token')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send({grant_type:'password'})
.expect(200)
.send({grant_type:'password'})
doesn't help. I don't know what it is used for.
-
you might have to pass in the actual
app
object you're testing against so the request object knows about your the dynamic param values – hellatan Commented Apr 8, 2017 at 20:46
2 Answers
Reset to default 2You can use a supertest like this for the same requirement
const app = require('../server'),
chai = require('chai'),
should = require('should'),
request = require('supertest');
describe("/get /:userId", () => {
it("should fetch user by id successfully", (done) => {
request(app)
.get("/users/" + userId)
// .query({ userId: userId })
.set('Accept', 'application/json')
.set({ 'x-access-token': token })
.expect('Content-Type', /json/)
.end((err, res) => {
res.status.should.equal(200);
// console.log("res.body.data.users = ",res.body)
// res.body.should.a("object");
res.body.data.should.have.property("users");
done();
});
});
});
express puts stuff req.params
automatically. if you define a route something/:name
then it should be on req.params.name
. From the documentation:
This property is an object containing properties mapped to the named route “parameters”. For example, if you have the route /user/:name, then the “name” property is available as req.params.name. This object defaults to {}.
So if you do the supertest example
request(app)
.post('/api/token')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send({grant_type:'password'})
.expect(200)
the .send({grant_type:'password'})
part populates the req.body
in express not the req.params
.