|
1 | 1 | import { ForwardedProtoMiddleware } from './forwarded-proto.middleware';
|
2 | 2 |
|
3 | 3 | describe('ForwardedProtoMiddleware', () => {
|
| 4 | + let forwardedProtoMiddleware: ForwardedProtoMiddleware; |
| 5 | + |
| 6 | + beforeEach(() => { |
| 7 | + forwardedProtoMiddleware = new ForwardedProtoMiddleware(); |
| 8 | + }) |
4 | 9 | 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(); |
6 | 37 | });
|
7 | 38 | });
|
0 commit comments