|
1 |
| -describe('@dotcom-reliability-kit/middleware-allow-request-methods', () => { |
2 |
| - it('has some tests', () => { |
3 |
| - throw new Error('Please write some tests'); |
| 1 | +const { allowRequestMethods } = require('../../../lib/index'); |
| 2 | +const { UserInputError } = require('@dotcom-reliability-kit/errors'); |
| 3 | + |
| 4 | +// Mock Express request and response objects |
| 5 | +let mockRequest; |
| 6 | +let mockResponse; |
| 7 | +let mockNext; |
| 8 | + |
| 9 | +describe('allowRequestMethods', () => { |
| 10 | + beforeEach(() => { |
| 11 | + // Reset all mocks before each test |
| 12 | + mockRequest = { |
| 13 | + method: 'GET' |
| 14 | + }; |
| 15 | + mockResponse = { |
| 16 | + headersSent: false, |
| 17 | + header: jest.fn() |
| 18 | + }; |
| 19 | + mockNext = jest.fn(); |
| 20 | + }); |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + // Clear all mocks after each test |
| 24 | + jest.clearAllMocks(); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('initialisation and validation', () => { |
| 28 | + it('throws TypeError when no allowedMethods are provided', () => { |
| 29 | + expect(() => { |
| 30 | + allowRequestMethods(); |
| 31 | + }).toThrow(TypeError); |
| 32 | + |
| 33 | + expect(() => { |
| 34 | + allowRequestMethods({}); |
| 35 | + }).toThrow('The `allowedMethods` option must be an array of strings'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('throws TypeError when allowedMethods is an empty array', () => { |
| 39 | + expect(() => { |
| 40 | + allowRequestMethods({ allowedMethods: [] }); |
| 41 | + }).toThrow(TypeError); |
| 42 | + }); |
| 43 | + |
| 44 | + it('throws TypeError when allowedMethods contains non-string values', () => { |
| 45 | + expect(() => { |
| 46 | + allowRequestMethods({ allowedMethods: [123, true] }); |
| 47 | + }).toThrow(TypeError); |
| 48 | + }); |
| 49 | + |
| 50 | + it('creates middleware function when valid allowedMethods are provided', () => { |
| 51 | + const middleware = allowRequestMethods({ |
| 52 | + allowedMethods: ['GET', 'POST'] |
| 53 | + }); |
| 54 | + expect(typeof middleware).toBe('function'); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('middleware behavior', () => { |
| 59 | + it('sets Allow header with normalised methods', () => { |
| 60 | + const middleware = allowRequestMethods({ |
| 61 | + allowedMethods: ['get', 'post'] |
| 62 | + }); |
| 63 | + |
| 64 | + middleware(mockRequest, mockResponse, mockNext); |
| 65 | + |
| 66 | + expect(mockResponse.header).toHaveBeenCalledWith('Allow', 'GET, POST'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('skips setting header if headers are already sent', () => { |
| 70 | + mockResponse.headersSent = true; |
| 71 | + const middleware = allowRequestMethods({ |
| 72 | + allowedMethods: ['GET', 'POST'] |
| 73 | + }); |
| 74 | + |
| 75 | + middleware(mockRequest, mockResponse, mockNext); |
| 76 | + |
| 77 | + expect(mockResponse.header).not.toHaveBeenCalled(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('calls next() with 405 error for disallowed method', () => { |
| 81 | + mockRequest.method = 'DELETE'; |
| 82 | + const middleware = allowRequestMethods({ |
| 83 | + allowedMethods: ['GET', 'POST'] |
| 84 | + }); |
| 85 | + |
| 86 | + middleware(mockRequest, mockResponse, mockNext); |
| 87 | + |
| 88 | + expect(mockNext).toHaveBeenCalledWith(expect.any(UserInputError)); |
| 89 | + const error = mockNext.mock.calls[0][0]; |
| 90 | + expect(error.statusCode).toBe(405); |
| 91 | + }); |
| 92 | + |
| 93 | + it('calls next() without error for allowed method', () => { |
| 94 | + mockRequest.method = 'GET'; |
| 95 | + const middleware = allowRequestMethods({ |
| 96 | + allowedMethods: ['GET', 'POST'] |
| 97 | + }); |
| 98 | + |
| 99 | + middleware(mockRequest, mockResponse, mockNext); |
| 100 | + |
| 101 | + expect(mockNext).toHaveBeenCalledWith(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('handles case-insensitive method matching', () => { |
| 105 | + mockRequest.method = 'get'; |
| 106 | + const middleware = allowRequestMethods({ |
| 107 | + allowedMethods: ['GET', 'POST'] |
| 108 | + }); |
| 109 | + |
| 110 | + middleware(mockRequest, mockResponse, mockNext); |
| 111 | + |
| 112 | + expect(mockNext).toHaveBeenCalledWith(); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe('normaliseAllowedRequestMethods', () => { |
| 117 | + it('normalises methods to uppercase', () => { |
| 118 | + const middleware = allowRequestMethods({ |
| 119 | + allowedMethods: ['get', 'Post', 'DELETE'] |
| 120 | + }); |
| 121 | + |
| 122 | + middleware(mockRequest, mockResponse, mockNext); |
| 123 | + |
| 124 | + expect(mockResponse.header).toHaveBeenCalledWith( |
| 125 | + 'Allow', |
| 126 | + 'GET, POST, DELETE' |
| 127 | + ); |
| 128 | + }); |
4 | 129 | });
|
5 | 130 | });
|
0 commit comments