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 195 196 197 198 199 200 201 202 203 204 205 206 207
|
'use strict';
const express = require('../support/express');
const app = express();
const request = require('../support/client');
const assert = require('assert');
const cookieParser = require('cookie-parser');
const cookiejar = require('cookiejar');
const session = require('express-session');
let http = require('http');
if (process.env.HTTP2_TEST) {
http = require('http2');
http.Http2ServerResponse.prototype._implicitHeader = function () {
this.writeHead(this.statusCode);
};
}
app.use(cookieParser());
app.use(
session({
secret: 'secret',
resave: true,
saveUninitialized: true
})
);
app.post('/signin', (request_, res) => {
request_.session.user = 'hunter@hunterloftis.com';
res.redirect('/dashboard');
});
app.post('/setcookie', (request_, res) => {
res.cookie('cookie', 'jar');
res.sendStatus(200);
});
app.get('/getcookie', (request_, res) => {
res.status(200).send(request_.cookies.cookie);
});
app.get('/cookieheader', (request_, res) => {
res.status(200).send(request_.headers.cookie);
});
app.get('/dashboard', (request_, res) => {
if (request_.session.user) return res.status(200).send('dashboard');
res.status(401).send('dashboard');
});
app.all('/signout', (request_, res) => {
request_.session.regenerate(() => {
res.status(200).send('signout');
});
});
app.get('/', (request_, res) => {
if (request_.session.user) return res.redirect('/dashboard');
res.status(200).send('home');
});
app.post('/redirect', (request_, res) => {
res.redirect('/simple');
});
app.get('/simple', (request_, res) => {
res.status(200).send('simple');
});
let base = 'http://localhost';
let server;
before(function listen(done) {
server = http.createServer(app);
server = server.listen(0, function listening() {
base += `:${server.address().port}`;
done();
});
});
describe('request', () => {
describe('persistent agent', () => {
const agent1 = request.agent();
const agent2 = request.agent();
const agent3 = request.agent();
const agent4 = request.agent();
it('should gain a session on POST', () =>
agent3.post(`${base}/signin`).then((res) => {
assert.equal(res.status, 200);
assert.ok('set-cookie' in res.headers === false);
assert.equal(res.text, 'dashboard');
}));
it('should start with empty session (set cookies)', (done) => {
agent1.get(`${base}/dashboard`).end((error, res) => {
assert.ok(error instanceof Error);
assert.equal(res.status, 401);
assert.ok('set-cookie' in res.headers);
done();
});
});
it('should gain a session (cookies already set)', () =>
agent1.post(`${base}/signin`).then((res) => {
assert.equal(res.status, 200);
assert.ok('set-cookie' in res.headers === false);
assert.equal('dashboard', res.text);
}));
it('should persist cookies across requests', () =>
agent1.get(`${base}/dashboard`).then((res) => {
assert.equal(res.status, 200);
}));
it('should have the cookie set in the end callback', () =>
agent4
.post(`${base}/setcookie`)
.then(() => agent4.get(`${base}/getcookie`))
.then((res) => {
assert.equal(res.status, 200);
assert.strictEqual(res.text, 'jar');
}));
it('should produce a valid cookie header', (done) => {
agent4
.set('Cookie', 'first_cookie=dummy; cookie=jam')
.get(`${base}/cookieheader`)
.then((res) => {
const cookiePairs = res.text.split('; '); // https://httpwg.org/specs/rfc6265.html#rfc.section.4.2.1
assert.deepStrictEqual(cookiePairs, [
'first_cookie=dummy',
'cookie=jar',
`connect.sid=${agent4.jar.getCookie('connect.sid', cookiejar.CookieAccessInfo.All).value}`,
]);
done();
});
});
it('should not share cookies between domains', () => {
assert.equal(agent4.get('https://google.com').cookies, "");
});
it('should send cookies to allowed domain with a different path', () => {
const postRequest = agent4.post(`${base}/x/y/z`)
const cookiesNames = postRequest.cookies.split(';').map(cookie => cookie.split('=')[0])
assert.deepStrictEqual(cookiesNames, ['cookie', ' connect.sid']);
});
it('should not share cookies', (done) => {
agent2.get(`${base}/dashboard`).end((error, res) => {
assert.ok(error instanceof Error);
assert.equal(res.status, 401);
done();
});
});
it('should not lose cookies between agents', () =>
agent1.get(`${base}/dashboard`).then((res) => {
assert.equal(res.status, 200);
}));
it('should be able to follow redirects', () =>
agent1.get(base).then((res) => {
assert.equal(res.status, 200);
assert.equal(res.text, 'dashboard');
}));
it('should be able to post redirects', () =>
agent1
.post(`${base}/redirect`)
.send({ foo: 'bar', baz: 'blaaah' })
.then((res) => {
assert.equal(res.status, 200);
assert.equal(res.text, 'simple');
assert.deepStrictEqual(res.redirects, [`${base}/simple`]);
}));
it('should be able to limit redirects', (done) => {
agent1
.get(base)
.redirects(0)
.end((error, res) => {
assert.ok(error instanceof Error);
assert.equal(res.status, 302);
assert.deepEqual(res.redirects, []);
assert.equal(res.header.location, '/dashboard');
done();
});
});
it('should be able to create a new session (clear cookie)', () =>
agent1.post(`${base}/signout`).then((res) => {
assert.equal(res.status, 200);
assert.ok('set-cookie' in res.headers);
}));
it('should regenerate with an empty session', (done) => {
agent1.get(`${base}/dashboard`).end((error, res) => {
assert.ok(error instanceof Error);
assert.equal(res.status, 401);
assert.ok('set-cookie' in res.headers === false);
done();
});
});
});
});
|