Skip to content

Commit da3136b

Browse files
committed
test: add tests for basePath validation
1 parent fe67e85 commit da3136b

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

test/base-handler.spec.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as http from 'http';
22
import { createRequest, createResponse } from 'node-mocks-http';
3-
import { testStorage, TestUploader } from './shared';
3+
import { testStorage, TestStorage, TestUploader } from './shared';
44

55
describe('BaseHandler', () => {
66
let uploader: TestUploader;
@@ -92,6 +92,38 @@ describe('BaseHandler', () => {
9292
expect(uploader.getId({ url } as http.IncomingMessage)).toBe(id);
9393
});
9494

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+
95127
interface MockReq extends http.IncomingMessage {
96128
user?: { _id: string };
97129
}

0 commit comments

Comments
 (0)