【问题标题】:Mocha test error CSRF token mismatchMocha 测试错误 CSRF 令牌不匹配
【发布时间】:2016-09-03 15:44:22
【问题描述】:

我最近在学习如何使用 mocha 和 supertest 编写测试。 当我尝试测试帖子网址时,它需要 _csrf 属性,所以我检查了这个 [如何使用 CSRF 测试 express form post?

]1

并输入以下代码

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

终端注意到 错误:CSRF 令牌不匹配 错误:预期 302“找到”,得到 403“禁止”

我的代码模仿用户行为,在 /rear 路由器渲染以获取 csrf 然后将其和其他信息发布到 /user/signup 的页面上,我不知道哪里出了问题以及如何修复它。如果你找到原因,请提醒我,非常感谢。

【问题讨论】:

    标签: mocha.js csrf bdd supertest


    【解决方案1】:

    我找出麻烦所在。我得到了 csrf,其中中间件 lusca 在 input(name='_csrf') 中将表单服务器公开到首页,但我忽略了在测试环境中它也应该保留一个请求及其 cookie,我再次检查 supertest doc 发现它可以调用 .代理方法来实现它。

    var agent = request.agent(app);
    
    describe('User', function () {
    
        it('should create a admin', function (done) {
            agent
                .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();
                        });
                });
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-03
      • 2022-08-19
      • 2017-06-06
      • 1970-01-01
      • 2021-08-18
      • 2021-10-22
      • 2021-03-16
      • 1970-01-01
      相关资源
      最近更新 更多