Skip to content

Commit aefd453

Browse files
committed
add jwt test
1 parent 18323b3 commit aefd453

9 files changed

+31
-7
lines changed

.nyc_output/68e007168176710ef7cc517bf3d4ae5c.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

.nyc_output/72bb8ed154fc2fa259a20c3da47df154.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

.nyc_output/9a33e79cf1404ae89f61667d607a6899.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

.nyc_output/c609c525a991d848bf836dfdb993b8fb.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,10 +673,12 @@ export const routeInfo: Array<IRouteInfo> = new JsonRoute(app, {
673673
}).start();
674674
675675
676-
app.use((err: any, req: express.Request, res: express.Response) => {
676+
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
677677
if (err.name === 'UnauthorizedError') {
678678
res.status(401).json({"message": "invalid token..."});
679679
}
680+
681+
next();
680682
});
681683
682684
```

demo/api/controllers/ProtectedController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ exports.index = (req, res) => {
33
};
44

55
exports.indexnot = (req, res) => {
6-
res.json({message: 'NOT protected bny jwt'});
6+
res.json({message: 'NOT protected by jwt'});
77
};

demo/server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ export const routeInfo: Array<IRouteInfo> = new JsonRoute(app, {
1616

1717
console.log("Total routes:", routeInfo.length);
1818

19-
app.use((err: any, req: express.Request, res: express.Response) => {
19+
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
2020
if (err.name === 'UnauthorizedError') {
2121
res.status(401).json({"message": "invalid token..."});
2222
}
23+
next();
2324
});
2425

2526
/**

test.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ chai.use(chaiHttp);
1111

1212
describe('Server is Up: ', () => {
1313
it('Has 13 routes', () => {
14-
routeInfo.length.should.be.eql(13);
14+
routeInfo.length.should.be.eql(15);
1515
});
1616

1717
it('/GET return 200', (done) => {
@@ -201,4 +201,27 @@ describe('Regex route: ', () => {
201201
done();
202202
});
203203
});
204+
});
205+
206+
207+
describe('JWT route: ', () => {
208+
it('/protected GET - must return 401', (done) => {
209+
chai.request(URL)
210+
.get("/protected")
211+
.end((err, res) => {
212+
expect(res).to.have.status(401);
213+
done();
214+
});
215+
});
216+
it('/notprotected GET - must return 200', (done) => {
217+
chai.request(URL)
218+
.get("/notprotected")
219+
.end((err, res) => {
220+
expect(err).to.be.null;
221+
expect(res).to.have.status(200);
222+
res.body.should.be.a("object");
223+
res.body.should.have.property("message").eql("NOT protected by jwt");
224+
done();
225+
});
226+
});
204227
});

0 commit comments

Comments
 (0)