-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
59 lines (53 loc) · 1.26 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const app = require('./app');
const request = require('supertest').agent(app.listen());
let token;
let cookie;
describe('csrf', () => {
describe('GET /token', () => {
it('should get token', (done) => {
request
.get('/token')
.expect(200)
.end((err, res) => {
token = res.text;
token.should.be.String;
cookie = res.headers['set-cookie'].join(';');
done(err);
});
});
});
describe('POST /post', () => {
it('should 403 without token', (done) => {
request
.post('/post')
.send({foo: 'bar'})
.expect(403);
done();
});
it('should 403 with wrong token', (done) => {
request
.post('/post')
.send({foo: 'bar'})
.set('x-csrf-token', 'wrong token')
.expect(403);
done();
});
it('should 200 with token in head', (done) => {
request
.post('/post')
.set('Cookie', cookie)
.set('x-csrf-token', token)
.send({foo: 'bar'})
.expect(200);
done();
});
it('should 200 with token in body', (done) => {
request
.post('/post')
.set('Cookie', cookie)
.send({_csrf: token})
.expect(200);
done();
});
});
});