How does supertest set session and cookie?

  node.js, question

I see the document setting cookie is written like this.

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);
 })
 })

Here

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

Set-cookie’ refers to the operation, and then’ cookie=hey; Path=/’ This is what cookie values look like, but I look at the api, and there is no such explanation, nor does it say that it can be obtained

expect('hey', done);

Description of cookie value
https://github.com/visionmedia/supertest#api

How should the session be set up?

cookieTheheyIs set here:

app.get('/', function(req, res){
 //cookie is set when accessing and routing
 res.cookie('cookie', 'hey');
 res.send();
 });

expect('hey', done);The reason why this assertion is successful is that according to the writing order of your test cases, you must have passed the previous one first.should save cookiesThe case, thatcookieWhen setting up successfully and then running the second case, the server part:

app.get('/return', function(req, res){
 //req. cookies. cookies have a value, and then return the value to the front end, so in case inside, the return value of /return inside does have hey
 if (req.cookies.cookie) res.send(req.cookies.cookie);
 else res.send(':(')
 });

As for how to set upsession, you first need a corresponding middlewaresession

Add:

supertestThe part of the assertion is relatively clear:

图片描述

The part that I have selected can be used for assertion.cookiePart of the reason why you “turn a blind eye” is because you don’t knowcookieInhttpIn the process of transmissionheaderOf course. (As for foundation, the study you need is study. This need to accumulate, not by “cattle people” inspiration can get)

And “I don’t know if supertest can directly operate cookie and session”, of course not, I just glanced at your test case and found outsupertestIn fact, it is a tool to simulate the client sending requests. Since it is a client, of course it cannot be set up.session.sessionIt is back-end processing, so I just said that your back-end part needs to add middleware processing. (on)sessionWhat it is is still the foundation, this is not a problem, even if I give you a 1000 words, it is not necessarily completely clear, you are more interested in reading it. The foundation is necessary, and none of the “Daniel” in the group suddenly became a god without the foundation.

AboutsessionJust answered a few days agoWhat have express-session and passportjs done to achieve automatic login?