admin管理员组文章数量:1023857
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
.
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
.
本文标签: javascriptHow to set reqparams parameters in Node39s supertestStack Overflow
版权声明:本文标题:javascript - How to set req.params parameters in Node's supertest? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745587481a2157685.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论