-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
agency.js
171 lines (151 loc) · 4.63 KB
/
agency.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
var express = require('express')
, app = express()
, request = require('../../')
, assert = require('assert')
, should = require('should');
app.use(express.cookieParser());
app.use(express.session({ secret: 'secret' }));
app.post('/signin', function(req, res) {
req.session.user = 'hunter@hunterloftis.com';
res.redirect('/dashboard');
});
app.get('/dashboard', function(req, res) {
if (req.session.user) return res.send(200, 'dashboard');
res.send(401, 'dashboard');
});
app.all('/signout', function(req, res) {
req.session.regenerate(function() {
res.send(200, 'signout');
});
});
app.get('/', function(req, res) {
if (req.session.user) return res.redirect('/dashboard');
res.send(200, 'home');
});
app.post('/redirect', function(req, res) {
res.redirect('/simple');
});
app.get('/simple', function(req, res) {
res.send(200, 'simple');
});
app.listen(4000);
describe('request', function() {
describe('persistent agent', function() {
var agent1 = request.agent();
var agent2 = request.agent();
var agent3 = request.agent();
it('should gain a session on POST', function(done) {
agent3
.post('http://localhost:4000/signin')
.end(function(err, res) {
should.not.exist(err);
res.should.have.status(200);
should.not.exist(res.headers['set-cookie']);
res.text.should.include('dashboard');
done();
});
});
it('should start with empty session (set cookies)', function(done) {
agent1
.get('http://localhost:4000/dashboard')
.end(function(err, res) {
should.not.exist(err);
res.should.have.status(401);
should.exist(res.headers['set-cookie']);
done();
});
});
it('should gain a session (cookies already set)', function(done) {
agent1
.post('http://localhost:4000/signin')
.end(function(err, res) {
should.not.exist(err);
res.should.have.status(200);
should.not.exist(res.headers['set-cookie']);
res.text.should.include('dashboard');
done();
});
});
it('should persist cookies across requests', function(done) {
agent1
.get('http://localhost:4000/dashboard')
.end(function(err, res) {
should.not.exist(err);
res.should.have.status(200);
done();
});
});
it('should not share cookies', function(done) {
agent2
.get('http://localhost:4000/dashboard')
.end(function(err, res) {
should.not.exist(err);
res.should.have.status(401);
done();
});
});
it('should not lose cookies between agents', function(done) {
agent1
.get('http://localhost:4000/dashboard')
.end(function(err, res) {
should.not.exist(err);
res.should.have.status(200);
done();
});
});
it('should be able to follow redirects', function(done) {
agent1
.get('http://localhost:4000/')
.end(function(err, res) {
should.not.exist(err);
res.should.have.status(200);
res.text.should.include('dashboard');
done();
});
});
it('should be able to post redirects', function(done) {
agent1
.post('http://localhost:4000/redirect')
.send({ foo: 'bar', baz: 'blaaah' })
.end(function(err, res) {
should.not.exist(err);
res.should.have.status(200);
res.text.should.include('simple');
res.redirects.should.eql(['http://localhost:4000/simple']);
done();
});
});
it('should be able to limit redirects', function(done) {
agent1
.get('http://localhost:4000/')
.redirects(0)
.end(function(err, res) {
should.not.exist(err);
res.should.have.status(302);
res.redirects.should.eql([]);
res.header.location.should.equal('/dashboard');
done();
});
});
it('should be able to create a new session (clear cookie)', function(done) {
agent1
.post('http://localhost:4000/signout')
.end(function(err, res) {
should.not.exist(err);
res.should.have.status(200);
should.exist(res.headers['set-cookie']);
done();
});
});
it('should regenerate with an empty session', function(done) {
agent1
.get('http://localhost:4000/dashboard')
.end(function(err, res) {
should.not.exist(err);
res.should.have.status(401);
should.not.exist(res.headers['set-cookie']);
done();
});
});
});
});