Skip to content

Commit

Permalink
Added express-session and login endpoint
Browse files Browse the repository at this point in the history
- the main change here is to add express-session and a login and authenticated endpoint so I can start figuring out how to get the agent to maintain cookies
- however I found a really weird issue where as soon as you add express-session then express starts acting differently & breaks reqresnext
- at this point both res.write and res.end get called, where res.end only gets the last char
- because reqresnext's end method doesn't concatenate with write, you only get the last char returned
- therefore I've had to add a workaround in lieu of antongolub/reqresnext#26 being looked at
  • Loading branch information
ErisDS committed Jan 25, 2022
1 parent eb73f62 commit e420f56
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 4 deletions.
10 changes: 8 additions & 2 deletions packages/express-test/lib/express-test-reqres.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
const {Request, Response} = require('reqresnext');

const {isJSON} = require('./utils');

module.exports.doRequest = function doRequest(reqOptions = {}, resOptions = {}) {
const req = new Request(Object.assign({}, reqOptions, {app: this.app}));
const res = new Response(Object.assign({}, resOptions, {app: this.app, req: req}));

res._headers = res.header;
// reqresnext has a bug where end overwrites the data
res.end = (chunk, encoding) => {
if (chunk) {
res.write(chunk, encoding);
}
res.emit('finish');
return this;
};

return new Promise((resolve) => {
res.on('finish', () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/express-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"devDependencies": {
"c8": "7.11.0",
"express": "4.17.2",
"express-session": "1.17.2",
"lodash": "4.17.21",
"mocha": "9.1.4",
"reqresnext": "1.6.4",
"should": "13.2.3",
Expand Down
25 changes: 24 additions & 1 deletion packages/express-test/test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require('./utils');
const app = require('./fixtures/app');

const agents = {
MockAgent: agentProvider.getAgent(app, 'mock'),
// MockAgent: agentProvider.getAgent(app, 'mock')
// WrappedAgent: agentProvider.getAgent(app, 'wrapper'),
ReqResAgent: agentProvider.getAgent(app, 'reqres')
};
Expand Down Expand Up @@ -54,6 +54,29 @@ Object.keys(agents).forEach((agentName) => {
body.should.eql({posts: [{id: 42, title: 'So long and thanks for all the fish.'}]});
text.should.eql('{"posts":[{"id":42,"title":"So long and thanks for all the fish."}]}');
});

it('login', async function () {
const {statusCode, headers, body, text} = await agent.post('/login/', {
body: {
username: 'hello',
password: 'world'
}
});

statusCode.should.eql(200);
headers.should.be.an.Object().with.properties('x-powered-by', 'content-type', 'content-length', 'etag', 'set-cookie');
body.should.eql({});
text.should.eql('OK');
});

it.skip('authenticated request', async function () {
const {statusCode, headers, body, text} = await agent.get('/api/posts/42/');

statusCode.should.eql(200);
headers.should.be.an.Object().with.properties('x-powered-by', 'content-type', 'content-length', 'etag');
body.should.eql({posts: [{id: 42, title: 'Hello World!'}]});
text.should.eql('{"posts":[{"id":42,"title":"Hello World!"}]}');
});
});
});

Expand Down
39 changes: 39 additions & 0 deletions packages/express-test/test/fixtures/app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
const express = require('express');
const session = require('express-session');

const app = express();

const fs = require('fs').promises;
const path = require('path');

const isLoggedIn = function (req, res, next) {
if (req.session.isLoggedIn) {
return next();
}

res.sendStatus(403);
};

app.use(express.json());

app.use(session({
secret: 'verysecretstring',
name: 'testauth'
}));

app.get('/', (req, res) => {
return res.send('Hello World!');
});
Expand All @@ -31,4 +46,28 @@ app.put('/posts/:id', async (req, res) => {
return res.json(json);
});

app.post('/login/', async (req, res) => {
if (req.body.username && req.body.password && req.body.username === 'hello' && req.body.password === 'world') {
req.session.loggedIn = true;
req.session.username = req.body.username;
return res.sendStatus(200);
}

return res.sendStatus(401);
});

app.get('/logout', (req, res) => {
req.session.destroy();
res.sendStatus(200);
});
app.get('/api/posts/:id', isLoggedIn, async (req, res) => {
const json = getPost(req.params.id);

if (!json) {
return res.sendStatus(404);
}

return res.json(json);
});

module.exports = app;
38 changes: 37 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,11 @@ depd@^1.1.0, depd@~1.1.2:
resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9"
integrity sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=

depd@~2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/depd/-/depd-2.0.0.tgz#b696163cc757560d09cf22cc8fad1571b79e76df"
integrity sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==

destroy@~1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.0.4.tgz#978857442c44749e4206613e37946205826abd80"
Expand Down Expand Up @@ -2014,6 +2019,20 @@ etag@~1.8.1:
resolved "https://registry.yarnpkg.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887"
integrity sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=

express-session@1.17.2:
version "1.17.2"
resolved "https://registry.yarnpkg.com/express-session/-/express-session-1.17.2.tgz#397020374f9bf7997f891b85ea338767b30d0efd"
integrity sha512-mPcYcLA0lvh7D4Oqr5aNJFMtBMKPLl++OKKxkHzZ0U0oDq1rpKBnkR5f5vCHR26VeArlTOEF9td4x5IjICksRQ==
dependencies:
cookie "0.4.1"
cookie-signature "1.0.6"
debug "2.6.9"
depd "~2.0.0"
on-headers "~1.0.2"
parseurl "~1.3.3"
safe-buffer "5.2.1"
uid-safe "~2.1.5"

express@4.17.2, express@^4.17.1:
version "4.17.2"
resolved "https://registry.yarnpkg.com/express/-/express-4.17.2.tgz#c18369f265297319beed4e5558753cc8c1364cb3"
Expand Down Expand Up @@ -3029,7 +3048,7 @@ lodash.upperfirst@4.3.1:
resolved "https://registry.yarnpkg.com/lodash.upperfirst/-/lodash.upperfirst-4.3.1.tgz#1365edf431480481ef0d1c68957a5ed99d49f7ce"
integrity sha1-E2Xt9DFIBIHvDRxolXpe2Z1J984=

lodash@^4.17.11, lodash@^4.17.14, lodash@^4.17.19, lodash@^4.17.21:
lodash@4.17.21, lodash@^4.17.11, lodash@^4.17.14, lodash@^4.17.19, lodash@^4.17.21:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
Expand Down Expand Up @@ -3418,6 +3437,11 @@ on-finished@~2.3.0:
dependencies:
ee-first "1.1.1"

on-headers@~1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/on-headers/-/on-headers-1.0.2.tgz#772b0ae6aaa525c399e489adfad90c403eb3c28f"
integrity sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==

once@1.4.0, once@^1.3.0, once@^1.3.1, once@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
Expand Down Expand Up @@ -3676,6 +3700,11 @@ ramda@^0.27.0:
resolved "https://registry.yarnpkg.com/ramda/-/ramda-0.27.2.tgz#84463226f7f36dc33592f6f4ed6374c48306c3f1"
integrity sha512-SbiLPU40JuJniHexQSAgad32hfwd+DRUdwF2PlVuI5RZD0/vahUco7R8vD86J/tcEKKF9vZrUVwgtmGCqlCKyA==

random-bytes@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/random-bytes/-/random-bytes-1.0.0.tgz#4f68a1dc0ae58bd3fb95848c30324db75d64360b"
integrity sha1-T2ih3Arli9P7lYSMMDJNt11kNgs=

randombytes@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/randombytes/-/randombytes-2.1.0.tgz#df6f84372f0270dc65cdf6291349ab7a473d4f2a"
Expand Down Expand Up @@ -4430,6 +4459,13 @@ type-is@^1.6.18, type-is@~1.6.18:
media-typer "0.3.0"
mime-types "~2.1.24"

uid-safe@~2.1.5:
version "2.1.5"
resolved "https://registry.yarnpkg.com/uid-safe/-/uid-safe-2.1.5.tgz#2b3d5c7240e8fc2e58f8aa269e5ee49c0857bd3a"
integrity sha512-KPHm4VL5dDXKz01UuEd88Df+KzynaohSL9fBh096KWAxSKZQDI2uBrVqtvRM4rwrIrRRKsdLNML/lnaaVSRioA==
dependencies:
random-bytes "~1.0.0"

unbox-primitive@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/unbox-primitive/-/unbox-primitive-1.0.1.tgz#085e215625ec3162574dc8859abee78a59b14471"
Expand Down

0 comments on commit e420f56

Please sign in to comment.