-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathFilesRouter.spec.js
More file actions
94 lines (76 loc) · 2.64 KB
/
FilesRouter.spec.js
File metadata and controls
94 lines (76 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const fs = require('fs');
const path = require('path');
describe('FilesRouter', () => {
describe('File Uploads', () => {
const V8_STRING_LIMIT_BYTES = 536_870_912;
let server;
beforeAll(async () => {
server = await reconfigureServer({
maxUploadSize: '1GB',
port: 8384,
});
});
afterAll(async () => {
// clean up the server for resuse
if (server && server.close) {
await new Promise((resolve, reject) => {
server.close(err => {
if (err) return reject(err);
resolve();
});
});
}
});
/**
* Quick helper function to upload the file to the server via the REST API
* We do this because creating a Parse.File object with a file over 512MB
* will try to use the Web API FileReader API, which will fail the test
*
* @param {string} fileName the name of the file
* @param {string} filePath the path to the file locally
* @returns
*/
const postFile = async (fileName, filePath) => {
const url = `${Parse.serverURL}/files/${fileName}`;
const headers = {
'X-Parse-Application-Id': Parse.applicationId,
'X-Parse-Master-Key': Parse.masterKey,
'Content-Type': 'multipart/form-data',
};
// Create a FormData object to send the file
const formData = new FormData();
formData.append('file', fs.createReadStream(filePath));
// Send the request
const response = await fetch(url, {
method: 'POST',
headers,
body: formData,
});
return response;
};
it('should allow Parse.File uploads under 512MB', async done => {
const filePath = path.join(__dirname, 'file.txt');
fs.writeFileSync(filePath, Buffer.alloc(1024 * 1024));
const response = await postFile('file.txt', filePath);
expect(response.ok).toBe(true);
fs.unlinkSync(filePath);
done();
});
it('should allow Parse.File uploads exactly 512MB', async done => {
const filePath = path.join(__dirname, 'file.txt');
fs.writeFileSync(filePath, Buffer.alloc(V8_STRING_LIMIT_BYTES));
const response = await postFile('file.txt', filePath);
expect(response.ok).toBe(true);
fs.unlinkSync(filePath);
done();
});
it('should allow Parse.File uploads over 512MB', async done => {
const filePath = path.join(__dirname, 'file.txt');
fs.writeFileSync(filePath, Buffer.alloc(V8_STRING_LIMIT_BYTES + 50 * 1024 * 1024));
const response = await postFile('file.txt', filePath);
expect(response.ok).toBe(true);
fs.unlinkSync(filePath);
done();
});
});
});