I am using node js + express for my server. I am writing test with superagent + node unit, my routes needs session for accessing it, can I fake this session for testing my route/controller? (May be superagent don't have this functionality, so suggest please another tool)
Asked
Active
Viewed 669 times
0
-
Did you tried with superagent? ("May be it don t have" != "It don t have") – DrakaSAN Jan 10 '14 at 10:25
-
@DrakaSAN I don't find it in it's API – Slow Harry Jan 10 '14 at 10:59
-
Well I m not web develloper, but it seems the sessions is supposed to be created by node on the server, not on the client (nb: I don t know what is testing route) – DrakaSAN Jan 10 '14 at 11:04
-
@DrakaSAN route is module handling requests to my server. Superagent can send it to my server, a lot of developers use it for test purposes, to check how some url requested works under real requests. So you can fake real session for test user profile for example, I did it, but not in node js, and now I am asking about it – Slow Harry Jan 10 '14 at 12:52
-
Well I m afraid I can t help you more, I never done any http server with node.js, nor ever used superagent, hope someone will be able to answer you – DrakaSAN Jan 10 '14 at 13:07
-
possible duplicate of [How to create a mock http.ServerRequest and http.ServerResponse?](http://stackoverflow.com/questions/15022544/how-to-create-a-mock-http-serverrequest-and-http-serverresponse) – Paul Sweatte Oct 02 '14 at 21:13
1 Answers
0
Here is an minimal working example using express-session
:
app.js
:
const express = require("express");
const session = require("express-session");
const app = express();
app.use(
session({
secret: "keyboard cat",
resave: false,
saveUninitialized: true,
}),
);
app.post("/signin", (req, res) => {
req.session.auth = "123";
console.info("sign in success");
res.status(200).end();
});
app.get("/protected", (req, res) => {
console.log("req.session.auth: ", req.session.auth);
if (!req.session.auth) {
return res.sendStatus(401);
}
res.json({ data: "protected data" });
});
module.exports = app;
app.test.js
:
const app = require("./app");
const superagent = require("superagent");
const { expect } = require("chai");
describe("21040811", () => {
let server;
const port = 4001;
const agent = superagent.agent();
before((done) => {
server = app.listen(port, () => {
console.info(`HTTP server is listening on http://localhost:${port}`);
done();
});
});
after((done) => {
server.close(done);
});
it("should return 401 status code", () => {
return agent.get(`http://localhost:${port}/protected`).catch((err) => {
expect(err.response.status).to.be.equal(401);
});
});
it("should sign in success and access /protected API correctly", () => {
return agent
.post(`http://localhost:${port}/signin`)
.then((res) => {
expect(res.status).to.be.equal(200);
})
.then(() => agent.get(`http://localhost:${port}/protected`))
.then((res) => {
expect(res.status).to.be.equal(200);
expect(res.body).to.be.eql({ data: "protected data" });
});
});
});
Integration test result with 100% coverage:
21040811
HTTP server is listening on http://localhost:4001
req.session.auth: undefined
✓ should return 401 status code
sign in success
req.session.auth: 123
✓ should sign in success and access /protected API correctly
2 passing (61ms)
-------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
app.js | 100 | 100 | 100 | 100 | |
app.test.js | 100 | 100 | 100 | 100 | |
-------------|----------|----------|----------|----------|-------------------|
Source code: https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/21040811

Lin Du
- 88,126
- 95
- 281
- 483