首页 > mocha 测试 post 路由,传递了_csrf, 但终端报错误 CSRF mismatch

mocha 测试 post 路由,传递了_csrf, 但终端报错误 CSRF mismatch

我最近在练习写测试,在测试一条post路由的时候,需要传递CSRF,于是我参考了stackoverflow上的一个答案
How to test express form post with CSRF?

我参照这个答案,写了如下代码

var request = require('supertest');
var should = require('should');
var app = require('../app');
var $ = require('jquery')(require("jsdom").jsdom().parentWindow);


describe('User', function () {

    it('should create a admin', function (done) {
        request(app)
            .get('/rear')
            .expect(200)
            .end(function (err, res) {
                if (err) return done(err);
                should.not.exist(err);
                var $html = $(res.text);
                var csrf = $html.find('input[name=_csrf]').val();
                console.log(csrf);
                should.exist(csrf);
                request(app)
                    .post('/user/signup')
                    .send({
                        _csrf: csrf,
                        name: 'admin',
                        mobile: '12345678901',
                        password: '123456',
                        repassword: '123456',
                        gender: '0'
                    })
                    .expect(302)
                    .end(function (err, res) {
                        if (err) return done(err);
                        should.not.exist(err);
                        res.header.location.should.include('/rear');
                        done();
                    });
            });
    });
});

但是终端报错
Error: CSRF token mismatch
Error: expected 302 "Found", got 403 "Forbidden"

我不知道要如何处理这个问题,按照我的思路,我的测试模仿用户行为,先在/rear 这个路由的页面中拿到csrf,然后post到/user/signup这个路由,不知道是哪里出了错误。


我找到了问题原因, 虽然我抓到了从后端expose到前端input(name='_csrf')的csrf, 但我忽视了一点, 就是测试环境中也要持久化request and cookies, 后来我又去看了下supertest的文档, supertest 有一个.agent方法 可以 persist a request and its cookies

var agent = request.agent(app);

agent

.post('/user/signup')
.send({
    _csrf: csrf,
    name: 'admin',
    mobile: '12345678901',
    password: '123456',
    repassword: '123456',
    gender: '0'
})

这样测试就通过了
【热门文章】
【热门文章】