|
| 1 | +import handleRangeHeaders from '../../src/utils/handleRangeHeaders'; |
| 2 | + |
| 3 | +describe('handleRangeHeaders', () => { |
| 4 | + let context; |
| 5 | + |
| 6 | + beforeEach(() => { |
| 7 | + context = { |
| 8 | + logger: { |
| 9 | + error: jest.fn(), |
| 10 | + }, |
| 11 | + }; |
| 12 | + }); |
| 13 | + |
| 14 | + it('should return content in range with valid range header', () => { |
| 15 | + const content = 'abcdef'; |
| 16 | + const req = { |
| 17 | + headers: { |
| 18 | + range: 'bytes=1-4', |
| 19 | + }, |
| 20 | + }; |
| 21 | + |
| 22 | + const res = { |
| 23 | + setHeader: jest.fn(), |
| 24 | + }; |
| 25 | + |
| 26 | + const contentRes = handleRangeHeaders(context, content, req, res); |
| 27 | + expect(contentRes).toEqual('bcde'); |
| 28 | + expect(res.statusCode).toEqual(206); |
| 29 | + expect(res.setHeader.mock.calls).toMatchSnapshot(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should handle malformed range header', () => { |
| 33 | + const content = 'abcdef'; |
| 34 | + const req = { |
| 35 | + headers: { |
| 36 | + range: 'abc', |
| 37 | + }, |
| 38 | + }; |
| 39 | + |
| 40 | + const res = { |
| 41 | + setHeader: jest.fn(), |
| 42 | + }; |
| 43 | + |
| 44 | + const contentRes = handleRangeHeaders(context, content, req, res); |
| 45 | + expect(contentRes).toEqual('abcdef'); |
| 46 | + expect(context.logger.error.mock.calls).toMatchSnapshot(); |
| 47 | + expect(res.statusCode).toBeUndefined(); |
| 48 | + expect(res.setHeader.mock.calls).toMatchSnapshot(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should handle unsatisfiable range', () => { |
| 52 | + const content = 'abcdef'; |
| 53 | + const req = { |
| 54 | + headers: { |
| 55 | + range: 'bytes=10-20', |
| 56 | + }, |
| 57 | + }; |
| 58 | + |
| 59 | + const res = { |
| 60 | + setHeader: jest.fn(), |
| 61 | + }; |
| 62 | + |
| 63 | + const contentRes = handleRangeHeaders(context, content, req, res); |
| 64 | + expect(contentRes).toEqual('abcdef'); |
| 65 | + expect(res.statusCode).toEqual(416); |
| 66 | + expect(res.setHeader.mock.calls).toMatchSnapshot(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should handle multiple ranges', () => { |
| 70 | + const content = 'abcdef'; |
| 71 | + const req = { |
| 72 | + headers: { |
| 73 | + range: 'bytes=1-2,4-5', |
| 74 | + }, |
| 75 | + }; |
| 76 | + |
| 77 | + const res = { |
| 78 | + setHeader: jest.fn(), |
| 79 | + }; |
| 80 | + |
| 81 | + const contentRes = handleRangeHeaders(context, content, req, res); |
| 82 | + expect(contentRes).toEqual('abcdef'); |
| 83 | + expect(context.logger.error.mock.calls).toMatchSnapshot(); |
| 84 | + expect(res.statusCode).toBeUndefined(); |
| 85 | + expect(res.setHeader.mock.calls).toMatchSnapshot(); |
| 86 | + }); |
| 87 | +}); |
0 commit comments