-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
agency.js
194 lines (170 loc) · 4.99 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
require('should-http');
var express = require('express')
, app = express()
, request = require('../../')
, assert = require('assert')
, should = require('should')
, cookieParser = require('cookie-parser')
, session = require('express-session');
app.use(cookieParser());
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true
}));
app.post('/signin', function(req, res) {
req.session.user = '[email protected]';
res.redirect('/dashboard');
});
app.post('/setcookie', function(req, res) {
res.cookie('cookie', 'jar');
res.sendStatus(200);
});
app.get('/getcookie', function(req, res) {
res.status(200).send(req.cookies.cookie);
});
app.get('/dashboard', function(req, res) {
if (req.session.user) return res.status(200).send('dashboard');
res.status(401).send('dashboard');
});
app.all('/signout', function(req, res) {
req.session.regenerate(function() {
res.status(200).send('signout');
});
});
app.get('/', function(req, res) {
if (req.session.user) return res.redirect('/dashboard');
res.status(200).send('home');
});
app.post('/redirect', function(req, res) {
res.redirect('/simple');
});
app.get('/simple', function(req, res) {
res.status(200).send('simple');
});
var base = 'http://localhost'
var server;
before(function listen(done) {
server = app.listen(0, function listening() {
base += ':' + server.address().port;
done();
});
});
describe('request', function() {
describe('persistent agent', function() {
var agent1 = request.agent();
var agent2 = request.agent();
var agent3 = request.agent();
var agent4 = request.agent();
it('should gain a session on POST', function() {
return agent3
.post(base + '/signin')
.then(function(res) {
res.should.have.status(200);
should.not.exist(res.headers['set-cookie']);
res.text.should.containEql('dashboard');
});
});
it('should start with empty session (set cookies)', function(done) {
agent1
.get(base + '/dashboard')
.end(function(err, res) {
should.exist(err);
res.should.have.status(401);
should.exist(res.headers['set-cookie']);
done();
});
});
it('should gain a session (cookies already set)', function() {
return agent1
.post(base + '/signin')
.then(function(res) {
res.should.have.status(200);
should.not.exist(res.headers['set-cookie']);
res.text.should.containEql('dashboard');
});
});
it('should persist cookies across requests', function() {
return agent1
.get(base + '/dashboard')
.then(function(res) {
res.should.have.status(200);
});
});
it('should have the cookie set in the end callback', function() {
return agent4
.post(base + '/setcookie')
.then(function() {
return agent4.get(base + '/getcookie')
})
.then(function(res) {
res.should.have.status(200);
assert.strictEqual(res.text, 'jar');
});
});
it('should not share cookies', function(done) {
agent2
.get(base + '/dashboard')
.end(function(err, res) {
should.exist(err);
res.should.have.status(401);
done();
});
});
it('should not lose cookies between agents', function() {
return agent1
.get(base + '/dashboard')
.then(function(res) {
res.should.have.status(200);
});
});
it('should be able to follow redirects', function() {
return agent1
.get(base)
.then(function(res) {
res.should.have.status(200);
res.text.should.containEql('dashboard');
});
});
it('should be able to post redirects', function() {
return agent1
.post(base + '/redirect')
.send({ foo: 'bar', baz: 'blaaah' })
.then(function(res) {
res.should.have.status(200);
res.text.should.containEql('simple');
res.redirects.should.eql([base + '/simple']);
});
});
it('should be able to limit redirects', function(done) {
agent1
.get(base)
.redirects(0)
.end(function(err, res) {
should.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() {
return agent1
.post(base + '/signout')
.then(function(res) {
res.should.have.status(200);
should.exist(res.headers['set-cookie']);
});
});
it('should regenerate with an empty session', function(done) {
agent1
.get(base + '/dashboard')
.end(function(err, res) {
should.exist(err);
res.should.have.status(401);
should.not.exist(res.headers['set-cookie']);
done();
});
});
});
});