Skip to content

Commit 153a074

Browse files
author
James Allen
committed
test: total coverage
1 parent 2074040 commit 153a074

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
11
import { ForwardedProtoMiddleware } from './forwarded-proto.middleware';
22

33
describe('ForwardedProtoMiddleware', () => {
4+
let forwardedProtoMiddleware: ForwardedProtoMiddleware;
5+
6+
beforeEach(() => {
7+
forwardedProtoMiddleware = new ForwardedProtoMiddleware();
8+
})
49
it('should be defined', () => {
5-
expect(new ForwardedProtoMiddleware()).toBeTruthy();
10+
expect(forwardedProtoMiddleware).toBeTruthy();
11+
});
12+
it('should do nothing without x-forwarded-proto provided', () => {
13+
const req = { headers: {} };
14+
const res = { redirect: jest.fn() };
15+
const next = jest.fn();
16+
forwardedProtoMiddleware.use(req, res, next);
17+
expect(res.redirect).not.toHaveBeenCalled();
18+
expect(next).toHaveBeenCalled();
19+
});
20+
it('should do nothing if x-forwarded-proto is not http', () => {
21+
const req = { headers: { 'x-forwarded-proto': 'ftp' } };
22+
const res = { redirect: jest.fn() };
23+
const next = jest.fn();
24+
forwardedProtoMiddleware.use(req, res, next);
25+
expect(res.redirect).not.toHaveBeenCalled();
26+
expect(next).toHaveBeenCalled();
27+
});
28+
it('should call the redirect method with given data', () => {
29+
const hostname = Date.now(); // dynamic value
30+
const url = Date.now() * 2; // dynamic value
31+
const req = { hostname, headers: { 'x-forwarded-proto': 'http' }, url };
32+
const res = { redirect: jest.fn() };
33+
const next = jest.fn();
34+
forwardedProtoMiddleware.use(req, res, next);
35+
expect(res.redirect).toHaveBeenCalledWith(`https://${hostname}${url}`);
36+
expect(next).not.toHaveBeenCalled();
637
});
738
});

0 commit comments

Comments
 (0)