snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
This commit is contained in:
196
apps/meteor/server/api/lib/getUploadFormData.spec.ts
Normal file
196
apps/meteor/server/api/lib/getUploadFormData.spec.ts
Normal file
@@ -0,0 +1,196 @@
|
||||
import { ReadableStream } from 'stream/web';
|
||||
|
||||
import { expect } from 'chai';
|
||||
|
||||
import { getUploadFormData } from './getUploadFormData';
|
||||
|
||||
const createMockRequest = (
|
||||
fields: Record<string, string>,
|
||||
file?: {
|
||||
fieldname: string;
|
||||
filename: string;
|
||||
content: string | Buffer;
|
||||
mimetype?: string;
|
||||
},
|
||||
options: { simulateError?: boolean } = {},
|
||||
): Request => {
|
||||
const boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW';
|
||||
const parts: string[] = [];
|
||||
|
||||
if (file) {
|
||||
parts.push(
|
||||
`--${boundary}`,
|
||||
`Content-Disposition: form-data; name="${file.fieldname}"; filename="${file.filename}"`,
|
||||
`Content-Type: ${file.mimetype || 'application/octet-stream'}`,
|
||||
'',
|
||||
file.content.toString(),
|
||||
);
|
||||
}
|
||||
|
||||
for (const [name, value] of Object.entries(fields)) {
|
||||
parts.push(`--${boundary}`, `Content-Disposition: form-data; name="${name}"`, '', value);
|
||||
}
|
||||
|
||||
parts.push(`--${boundary}--`);
|
||||
|
||||
const buffer = Buffer.from(parts.join('\r\n'));
|
||||
|
||||
const mockRequest: any = {
|
||||
headers: {
|
||||
entries: () => [['content-type', `multipart/form-data; boundary=${boundary}`]],
|
||||
},
|
||||
body: new ReadableStream({
|
||||
async pull(controller) {
|
||||
if (options.simulateError) {
|
||||
controller.error(new Error('aborted'));
|
||||
return;
|
||||
}
|
||||
controller.enqueue(buffer);
|
||||
controller.close();
|
||||
},
|
||||
}),
|
||||
};
|
||||
|
||||
return mockRequest as Request & { headers: Record<string, string> };
|
||||
};
|
||||
|
||||
describe('getUploadFormData', () => {
|
||||
it('should successfully parse a single file upload and fields', async () => {
|
||||
const mockRequest = createMockRequest(
|
||||
{ fieldName: 'fieldValue' },
|
||||
{
|
||||
fieldname: 'fileField',
|
||||
filename: 'test.txt',
|
||||
content: 'Hello, this is a test file!',
|
||||
mimetype: 'text/plain',
|
||||
},
|
||||
);
|
||||
|
||||
const result = await getUploadFormData({ request: mockRequest }, { field: 'fileField' });
|
||||
|
||||
expect(result).to.deep.include({
|
||||
fieldname: 'fileField',
|
||||
filename: 'test.txt',
|
||||
mimetype: 'text/plain',
|
||||
fields: { fieldName: 'fieldValue' },
|
||||
});
|
||||
|
||||
expect(result.fileBuffer).to.not.be.undefined;
|
||||
expect(result.fileBuffer.toString()).to.equal('Hello, this is a test file!');
|
||||
});
|
||||
it('should parse a file upload with multiple additional fields', async () => {
|
||||
const mockRequest = createMockRequest(
|
||||
{
|
||||
fieldName: 'fieldValue',
|
||||
extraField1: 'extraValue1',
|
||||
extraField2: 'extraValue2',
|
||||
},
|
||||
{
|
||||
fieldname: 'fileField',
|
||||
filename: 'test_with_fields.txt',
|
||||
content: 'This file has additional fields!',
|
||||
mimetype: 'text/plain',
|
||||
},
|
||||
);
|
||||
|
||||
const result = await getUploadFormData({ request: mockRequest }, { field: 'fileField' });
|
||||
|
||||
expect(result).to.deep.include({
|
||||
fieldname: 'fileField',
|
||||
filename: 'test_with_fields.txt',
|
||||
mimetype: 'text/plain',
|
||||
fields: {
|
||||
fieldName: 'fieldValue',
|
||||
extraField1: 'extraValue1',
|
||||
extraField2: 'extraValue2',
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.fileBuffer).to.not.be.undefined;
|
||||
expect(result.fileBuffer.toString()).to.equal('This file has additional fields!');
|
||||
});
|
||||
|
||||
it('should handle a file upload when fileOptional is true', async () => {
|
||||
const mockRequest = createMockRequest(
|
||||
{ fieldName: 'fieldValue' },
|
||||
{
|
||||
fieldname: 'fileField',
|
||||
filename: 'optional.txt',
|
||||
content: 'This file is optional!',
|
||||
mimetype: 'text/plain',
|
||||
},
|
||||
);
|
||||
|
||||
const result = await getUploadFormData({ request: mockRequest }, { fileOptional: true });
|
||||
|
||||
expect(result).to.deep.include({
|
||||
fieldname: 'fileField',
|
||||
filename: 'optional.txt',
|
||||
mimetype: 'text/plain',
|
||||
fields: { fieldName: 'fieldValue' },
|
||||
});
|
||||
|
||||
expect(result.fileBuffer).to.not.be.undefined;
|
||||
expect(result.fileBuffer?.toString()).to.equal('This file is optional!');
|
||||
});
|
||||
|
||||
it('should throw an error when no file is uploaded and fileOptional is false', async () => {
|
||||
const mockRequest = createMockRequest({ fieldName: 'fieldValue' });
|
||||
|
||||
try {
|
||||
await getUploadFormData({ request: mockRequest }, { fileOptional: false });
|
||||
throw new Error('Expected function to throw');
|
||||
} catch (error) {
|
||||
expect((error as Error).message).to.equal('[No file uploaded]');
|
||||
}
|
||||
});
|
||||
|
||||
it('should return fields without errors when no file is uploaded but fileOptional is true', async () => {
|
||||
const mockRequest = createMockRequest({ fieldName: 'fieldValue' }); // No file
|
||||
|
||||
const result = await getUploadFormData({ request: mockRequest }, { fileOptional: true });
|
||||
|
||||
expect(result).to.deep.equal({
|
||||
fields: { fieldName: 'fieldValue' },
|
||||
file: undefined,
|
||||
fileBuffer: undefined,
|
||||
fieldname: undefined,
|
||||
filename: undefined,
|
||||
encoding: undefined,
|
||||
mimetype: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it('should reject an oversized file', async () => {
|
||||
const mockRequest = createMockRequest(
|
||||
{},
|
||||
{
|
||||
fieldname: 'fileField',
|
||||
filename: 'large.txt',
|
||||
content: 'x'.repeat(1024 * 1024 * 2), // 2 MB file
|
||||
mimetype: 'text/plain',
|
||||
},
|
||||
);
|
||||
|
||||
try {
|
||||
await getUploadFormData(
|
||||
{ request: mockRequest },
|
||||
{ sizeLimit: 1024 * 1024 }, // 1 MB limit
|
||||
);
|
||||
throw new Error('Expected function to throw');
|
||||
} catch (error) {
|
||||
expect((error as Error).message).to.equal('[error-file-too-large]');
|
||||
}
|
||||
});
|
||||
|
||||
it('should handle an aborted request stream', async () => {
|
||||
const mockRequest = createMockRequest({}, undefined, { simulateError: true });
|
||||
|
||||
try {
|
||||
await getUploadFormData({ request: mockRequest }, { fileOptional: true });
|
||||
throw new Error('Expected function to throw');
|
||||
} catch (error) {
|
||||
expect((error as Error).message).to.equal('aborted');
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user