search

Home  >  Q&A  >  body text

node.js - supertest如何设置session和cookie?

我看文档设置cookie 是这么写的

var request = require('supertest')
   , should = require('should')
   , express = require('express')
   , cookieParser = require('cookie-parser');


describe('request.agent(app)', function(){
  var app = express();
  app.use(cookieParser());

  app.get('/', function(req, res){
    res.cookie('cookie', 'hey');
    res.send();
  });

  app.get('/return', function(req, res){
    if (req.cookies.cookie) res.send(req.cookies.cookie);
    else res.send(':(')
  });

  var agent = request.agent(app);

  it('should save cookies', function(done){
    agent
    .get('/')
    .expect('set-cookie', 'cookie=hey; Path=/', done);
  })

  it('should send cookies', function(done){
    agent
    .get('/return')
    .expect('hey', done);
  })
})

这里的

expect('set-cookie', 'cookie=hey; Path=/', done)

'set-cookie'是指操作,然后'cookie=hey; Path=/'这个是cookie值的样子,但是我看api,并没有这种说明,也同样没有说能获取

expect('hey', done); 

cookie值的说明
https://github.com/visionmedia/supertest#api

那么session又该如何设置呢?

PHPzPHPz2782 days ago850

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-17 14:24:38

    The

    of cookiehey is set here:

    app.get('/', function(req, res){
        //当访问跟路由时设置了cookie
        res.cookie('cookie', 'hey');
        res.send();
    });

    expect('hey', done);The reason why this assertion is successful is that according to the order in which your test cases are written, the previous should save cookies case must have been run first, then cookie will be set successfully, and then the second one will be run. In this case, the server part:

    app.get('/return', function(req, res){
        //req.cookies.cookie是有值的了,然后会把值返回给前端,所以在case里,/return的返回值里确实有hey
        if (req.cookies.cookie) res.send(req.cookies.cookie);
        else res.send(':(')
      });

    As for how to set up session, you first need a corresponding middleware session

    Supplement:

    Supertest describes the assertion part relatively clearly:

    The part I selected is the part that can be used to assert cookie. The reason why you "turn a blind eye" is because you don't know that cookie exists in http during the header transmission process. . (Regarding the basics, what you need to learn is learning. This requires accumulation and cannot be obtained by relying on the advice of "great people")

    There is also "I don't know if supertest can directly operate cookies and sessions." Of course not, I took a look at your test case and found that supertest is actually a tool for simulating client requests. Since It is a client, of course it cannot be set session. session is processed by the backend, so I say that your backend part needs to add middleware processing. (About what session is, it is still the basics. This is not a problem. Even if I tell you 1,000 words, I may not be able to explain it completely clearly, and you may not be interested in reading it to the end. The basics are necessary, in the group No "big guy" suddenly becomes a god when the foundation does not exist)

    Regarding session, I just answered a question a few days ago. What did express-session and passportjs do to achieve automatic login?

    reply
    0
  • Cancelreply