最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set req.params parameters in Node's supertest? - Stack Overflow

programmeradmin2浏览0评论

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.

Share Improve this question edited Mar 1, 2016 at 16:05 BrTkCa 4,7934 gold badges28 silver badges45 bronze badges asked Mar 1, 2016 at 15:26 MatkoMatko 3,4464 gold badges24 silver badges36 bronze badges 1
  • 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
Add a ment  | 

2 Answers 2

Reset to default 2

You 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.

发布评论

评论列表(0)

  1. 暂无评论