-
-
Notifications
You must be signed in to change notification settings - Fork 688
Expand file tree
/
Copy pathfixtures.test.js
More file actions
138 lines (118 loc) · 3.78 KB
/
fixtures.test.js
File metadata and controls
138 lines (118 loc) · 3.78 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* eslint-disable global-require */
/* eslint-disable import/no-dynamic-require */
import { createReadStream } from 'node:fs';
import { createConnection } from 'node:net';
import { join } from 'node:path';
import { createServer } from 'node:http';
import { strictEqual } from 'node:assert';
import formidable from '../../src/index.js';
const PORT = 13534;
const CWD = process.cwd();
const FIXTURES_HTTP = join(CWD, 'test', 'fixture', 'http');
const UPLOAD_DIR = join(CWD, 'test', 'tmp');
import * as encoding from "../fixture/js/encoding.js";
import * as misc from "../fixture/js/misc.js";
import * as noFilename from "../fixture/js/no-filename.js";
import * as preamble from "../fixture/js/preamble.js";
import * as workarounds from "../fixture/js/workarounds.js";
import * as specialCharsInFilename from "../fixture/js/special-chars-in-filename.js";
const fixtures= {
encoding,
misc,
[`no-filename`]: noFilename,
preamble,
[`special-chars-in-filename`]: specialCharsInFilename,
// workarounds, // todo uncomment this and make it work
};
test('fixtures', (done) => {
const server = createServer();
server.listen(PORT, findFixtures);
function findFixtures() {
const results = Object.entries(fixtures).map(([fixtureGroup, fixture]) => {
return Object.entries(fixture).map(([k, v]) => {
return v.map(details => {
return {
fixture: v,
name: `${fixtureGroup}/${details.fixture}.http`
};
});
});
}).flat(Infinity);
testNext(results);
}
function testNext(results) {
const fixtureWithName = results.shift();
if (!fixtureWithName) {
server.close();
done();
return;
}
const fixtureName = fixtureWithName.name;
const fixture = fixtureWithName.fixture;
uploadFixture(fixtureName, (err, parts) => {
try {
if (err) {
err.fixtureName = fixtureName;
throw err;
}
fixture.forEach((expectedPart, i) => {
const parsedPart = parts[i];
strictEqual(parsedPart.type, expectedPart.type);
strictEqual(parsedPart.name, expectedPart.name);
if (parsedPart.type === 'file') {
const file = parsedPart.value;
strictEqual(file.originalFilename, expectedPart.originalFilename,
`${JSON.stringify([expectedPart, file])}`);
if (expectedPart.sha1) {
strictEqual(
file.hash,
expectedPart.sha1,
`SHA1 error ${file.originalFilename} on ${file.filepath} ${JSON.stringify([expectedPart, file])}`,
);
}
}
});
} catch (e) {
server.close();
done(e);
throw e;
}
testNext(results);
});
}
function uploadFixture(fixtureName, cb) {
server.once('request', (req, res) => {
const form = formidable({
uploadDir: UPLOAD_DIR,
hashAlgorithm: 'sha1',
});
function callback(...args) {
const realCallback = cb;
// eslint-disable-next-line no-param-reassign
cb = function callbackFn() {};
realCallback(...args);
}
const parts = [];
form
.on('error', callback)
.on('fileBegin', (name, value) => {
parts.push({ type: 'file', name, value });
})
.on('field', (name, value) => {
parts.push({ type: 'field', name, value });
})
.on('end', () => {
res.end();
callback(null, parts);
});
form.parse(req);
});
const socket = createConnection(PORT);
const fixturePath = join(FIXTURES_HTTP, fixtureName);
const file = createReadStream(fixturePath);
file.pipe(socket, { end: false });
socket.on('data', () => {
socket.end();
});
}
});