Thanks @shawnzhu, you comment on cookies helped me figure out what I needed to do. I had an idea that I was over complicating it. Here is what I came up with:
https://github.com/socketwiz/swblog/blob/master/test/contact.js
it('should not post just a name', function(done) {
request(mock)
.get('/contact')
.end(function(err, res){
var $html = jQuery(res.text);
var csrf = $html.find('input[name=_csrf]').val();
request(mock)
.post('/api/contact.json')
.set('cookie', res.headers['set-cookie'])
.send({
_csrf: csrf,
name: 'Mary Jane'
})
.expect(function(res){
assert.equal(undefined, res.body.name);
assert.equal('You must provide a valid email address', res.body.email);
assert.equal('You must provide a message', res.body.message);
})
.expect(500, done);
});
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…