我看文档设置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又该如何设置呢?
The
of
cookie
hey
is set here:expect('hey', done);
The reason why this assertion is successful is that according to the order in which your test cases are written, the previousshould save cookies
case must have been run first, thencookie
will be set successfully, and then the second one will be run. In this case, the server part:As for how to set up
session
, you first need a corresponding middleware sessionSupplement:
Supertest describes the assertion part relatively clearly:
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 setsession
.session
is processed by the backend, so I say that your backend part needs to add middleware processing. (About whatsession
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?