|
1 | 1 | import * as http from 'http'; |
2 | 2 | import { createRequest, createResponse } from 'node-mocks-http'; |
3 | | -import { testStorage, TestUploader } from './shared'; |
| 3 | +import { testStorage, TestStorage, TestUploader } from './shared'; |
4 | 4 |
|
5 | 5 | describe('BaseHandler', () => { |
6 | 6 | let uploader: TestUploader; |
@@ -92,6 +92,38 @@ describe('BaseHandler', () => { |
92 | 92 | expect(uploader.getId({ url } as http.IncomingMessage)).toBe(id); |
93 | 93 | }); |
94 | 94 |
|
| 95 | + it('should reject path outside basePath (raw Node.js)', () => { |
| 96 | + const res = createResponse(); |
| 97 | + const req = createRequest({ method: 'GET', url: '/wrong' }); |
| 98 | + delete (req as any).originalUrl; |
| 99 | + uploader.handle(req, res); |
| 100 | + expect(res.statusCode).toBe(404); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should accept path matching basePath (raw Node.js)', () => { |
| 104 | + const res = createResponse(); |
| 105 | + const req = createRequest({ method: 'PATCH', url: '/files' }); |
| 106 | + delete (req as any).originalUrl; |
| 107 | + uploader.handle(req, res); |
| 108 | + expect(res.statusCode).toBe(405); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should accept all paths when basePath is "/"', () => { |
| 112 | + const uploader2 = new TestUploader({ storage: new TestStorage({ basePath: '/' }) }); |
| 113 | + const res = createResponse(); |
| 114 | + const req = createRequest({ method: 'PATCH', url: '/anything' }); |
| 115 | + delete (req as any).originalUrl; |
| 116 | + uploader2.handle(req, res); |
| 117 | + expect(res.statusCode).toBe(405); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should skip basePath check when originalUrl is set', () => { |
| 121 | + const res = createResponse(); |
| 122 | + const req = createRequest({ method: 'PATCH', url: '/api/other' }); |
| 123 | + uploader.handle(req, res); |
| 124 | + expect(res.statusCode).toBe(405); |
| 125 | + }); |
| 126 | + |
95 | 127 | interface MockReq extends http.IncomingMessage { |
96 | 128 | user?: { _id: string }; |
97 | 129 | } |
|
0 commit comments