-
-
Notifications
You must be signed in to change notification settings - Fork 683
/
Copy pathfixtures.test.js
175 lines (151 loc) · 5.62 KB
/
fixtures.test.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* eslint-disable global-require */
/* eslint-disable import/no-dynamic-require */
import { strictEqual, deepEqual } from 'node:assert';
import test from 'node:test';
import { createReadStream, constants, accessSync } from 'node:fs';
import { createConnection } from 'node:net';
import { join } from 'node:path';
import { createServer } from 'node:http';
import { pipeline } from 'node:stream';
import formidable from '../../src/index.js';
const PORT = 13534;
const CWDTest = join(process.cwd(), "test-node");
const FIXTURES_HTTP = join(CWDTest, 'fixture', 'http');
const UPLOAD_DIR = join(CWDTest, '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', (testContext, done) => {
const server = createServer();
server.listen(PORT, findFixtures);
function properExitTest(...x) {
server.close();
done(...x)
}
function strictEqualExit(...x) {
try {
strictEqual(...x)
} catch (assertionError) {
properExitTest(assertionError);
throw assertionError;
}
}
function findFixtures() {
const remainingFixtures = 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`,
http: join(FIXTURES_HTTP, fixtureGroup, `${details.fixture}.http`),
};
});
});
}).flat(Infinity);
testNext(remainingFixtures);
}
function testNext(remainingFixtures) {
const fixtureWithName = remainingFixtures.shift();
if (!fixtureWithName) {
properExitTest();
return;
}
const fixtureName = fixtureWithName.name;
const fixture = fixtureWithName.fixture;
uploadFixture(fixtureWithName, (err, parts) => {
if (err) {
err.fixtureName = fixtureName;
properExitTest(new Error(err));
return;
}
fixture.forEach((expectedPart, i) => {
const parsedPart = parts[i];
strictEqualExit(parsedPart.type, expectedPart.type);
strictEqualExit(parsedPart.name, expectedPart.name);
if (parsedPart.type === 'file') {
const file = parsedPart.value;
strictEqualExit(file.originalFilename, expectedPart.originalFilename,
`${JSON.stringify([expectedPart, file])}`);
if (expectedPart.sha1) {
strictEqualExit(
file.hash,
expectedPart.sha1,
`SHA1 error ${file.originalFilename} on ${file.filepath} ${JSON.stringify([expectedPart, file])}`,
);
}
}
});
testNext(remainingFixtures);
});
}
function uploadFixture(fixtureWithName, verifyFixture) {
const fixturePath = fixtureWithName.http;
let verifyFixtureOnce = verifyFixture;
try {
accessSync(fixturePath, constants.W_OK | constants.R_OK);
} catch (err) {
properExitTest(new Error(`can't open ${fixturePath}`));
}
const socket = createConnection(PORT);
const file = createReadStream(fixturePath);
// make sure verifyFixture is only called once
function callback(...args) {
const realCallback = verifyFixtureOnce;
verifyFixtureOnce = function callbackFn() { };
if (socket.writable) {
socket.destroy();
}
realCallback(...args);
}
server.once('request', (req, res) => {
const form = formidable({
uploadDir: UPLOAD_DIR,
hashAlgorithm: 'sha1',
keepExtensions: true,
});
const parts = [];
form
.once('error', (error) => {
const a = form;
const b = a;
const c = a._parser.explain();
console.log(c);
callback(error);
})
.on('fileBegin', (name, value) => {
parts.push({ type: 'file', name, value });
})
.on('field', (name, value) => {
parts.push({ type: 'field', name, value });
})
.once('end', () => {
res.end();
callback(null, parts);
});
form.parse(req);
});
// file.pipe(socket, {end: true});
pipeline(
file,
socket,
(err) => {
if (err) {
console.error('error');
} else {
console.log("success")
}
},
);
}
});