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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
import * as assert from 'assert';
import * as fs from 'fs';
import { LLParse } from 'llparse';
import { Group, MDGator, Metadata, Test } from 'mdgator';
import * as path from 'path';
import * as vm from 'vm';
import * as llhttp from '../src/llhttp';
import {IHTTPResult} from '../src/llhttp/http';
import {IURLResult} from '../src/llhttp/url';
import { build, FixtureResult, TestType } from './fixtures';
//
// Cache nodes/llparse instances ahead of time
// (different types of tests will re-use them)
//
interface INodeCacheEntry {
llparse: LLParse;
entry: IHTTPResult['entry'];
}
interface IUrlCacheEntry {
llparse: LLParse;
entry: IURLResult['entry']['normal'];
}
const nodeCache = new Map<llhttp.HTTPMode, INodeCacheEntry>();
const urlCache = new Map<llhttp.HTTPMode, IUrlCacheEntry>();
const modeCache = new Map<string, FixtureResult>();
function buildNode(mode: llhttp.HTTPMode) {
let entry = nodeCache.get(mode);
if (entry) {
return entry;
}
const p = new LLParse();
const instance = new llhttp.HTTP(p, mode);
entry = { llparse: p, entry: instance.build().entry };
nodeCache.set(mode, entry);
return entry;
}
function buildURL(mode: llhttp.HTTPMode) {
let entry = urlCache.get(mode);
if (entry) {
return entry;
}
const p = new LLParse();
const instance = new llhttp.URL(p, mode, true);
const node = instance.build();
// Loop
node.exit.toHTTP.otherwise(node.entry.normal);
node.exit.toHTTP09.otherwise(node.entry.normal);
entry = { llparse: p, entry: node.entry.normal };
urlCache.set(mode, entry);
return entry;
}
//
// Build binaries using cached nodes/llparse
//
async function buildMode(mode: llhttp.HTTPMode, ty: TestType, meta: any)
: Promise<FixtureResult> {
const cacheKey = `${mode}:${ty}:${JSON.stringify(meta || {})}`;
let entry = modeCache.get(cacheKey);
if (entry) {
return entry;
}
let node;
let prefix: string;
let extra: string[];
if (ty === 'url') {
node = buildURL(mode);
prefix = 'url';
extra = [];
} else {
node = buildNode(mode);
prefix = 'http';
extra = [
'-DLLHTTP__TEST_HTTP',
path.join(__dirname, '..', 'src', 'native', 'http.c'),
];
}
if (meta.pause) {
extra.push(`-DLLHTTP__TEST_PAUSE_${meta.pause.toUpperCase()}=1`);
}
if (meta.skipBody) {
extra.push('-DLLHTTP__TEST_SKIP_BODY=1');
}
entry = await build(node.llparse, node.entry, `${prefix}-${mode}-${ty}`, {
extra,
}, ty);
modeCache.set(cacheKey, entry);
return entry;
}
interface IFixtureMap {
[key: string]: { [key: string]: Promise<FixtureResult> };
}
//
// Run test suite
//
function run(name: string): void {
const md = new MDGator();
const raw = fs.readFileSync(path.join(__dirname, name + '.md')).toString();
const groups = md.parse(raw);
function runSingleTest(mode: llhttp.HTTPMode, ty: TestType, meta: any,
input: string,
expected: ReadonlyArray<string | RegExp>): void {
it(`should pass in mode="${mode}" and for type="${ty}"`, async () => {
const binary = await buildMode(mode, ty, meta);
await binary.check(input, expected, {
noScan: meta.noScan === true,
});
});
}
function runTest(test: Test) {
describe(test.name + ` at ${name}.md:${test.line + 1}`, () => {
let modes: llhttp.HTTPMode[] = [ 'strict', 'loose' ];
let types: TestType[] = [ 'none' ];
const isURL = test.values.has('url');
const inputKey = isURL ? 'url' : 'http';
assert(test.values.has(inputKey),
`Missing "${inputKey}" code in md file`);
assert.strictEqual(test.values.get(inputKey)!.length, 1,
`Expected just one "${inputKey}" input`);
let meta: Metadata;
if (test.meta.has(inputKey)) {
meta = test.meta.get(inputKey)![0]!;
} else {
assert(isURL, 'Missing required http metadata');
meta = {};
}
if (isURL) {
types = [ 'url' ];
} else {
assert(meta.hasOwnProperty('type'), 'Missing required `type` metadata');
if (meta.type === 'request') {
types.push('request');
} else if (meta.type === 'response') {
types.push('response');
} else if (meta.type === 'request-only') {
types = [ 'request' ];
} else if (meta.type === 'request-lenient-headers') {
types = [ 'request-lenient-headers' ];
} else if (meta.type === 'request-lenient-chunked-length') {
types = [ 'request-lenient-chunked-length' ];
} else if (meta.type === 'request-lenient-keep-alive') {
types = [ 'request-lenient-keep-alive' ];
} else if (meta.type === 'request-lenient-transfer-encoding') {
types = [ 'request-lenient-transfer-encoding' ];
} else if (meta.type === 'request-lenient-version') {
types = [ 'request-lenient-version' ];
} else if (meta.type === 'response-lenient-keep-alive') {
types = [ 'response-lenient-keep-alive' ];
} else if (meta.type === 'response-lenient-headers') {
types = [ 'response-lenient-headers' ];
} else if (meta.type === 'response-lenient-version') {
types = [ 'response-lenient-version' ];
} else if (meta.type === 'response-only') {
types = [ 'response' ];
} else if (meta.type === 'request-finish') {
types = [ 'request-finish' ];
} else if (meta.type === 'response-finish') {
types = [ 'response-finish' ];
} else {
throw new Error(`Invalid value of \`type\` metadata: "${meta.type}"`);
}
}
assert(test.values.has('log'), 'Missing `log` code in md file');
assert.strictEqual(test.values.get('log')!.length, 1,
'Expected just one output');
if (meta.mode === 'strict') {
modes = [ 'strict' ];
} else if (meta.mode === 'loose') {
modes = [ 'loose' ];
} else {
assert(!meta.hasOwnProperty('mode'),
`Invalid value of \`mode\` metadata: "${meta.mode}"`);
}
let input: string = test.values.get(inputKey)![0];
let expected: string = test.values.get('log')![0];
// Remove trailing newline
input = input.replace(/\n$/, '');
// Remove escaped newlines
input = input.replace(/\\(\r\n|\r|\n)/g, '');
// Normalize all newlines
input = input.replace(/\r\n|\r|\n/g, '\r\n');
// Replace escaped CRLF, tabs, form-feed
input = input.replace(/\\r/g, '\r');
input = input.replace(/\\n/g, '\n');
input = input.replace(/\\t/g, '\t');
input = input.replace(/\\f/g, '\f');
input = input.replace(/\\x([0-9a-fA-F]+)/g, (all, hex) => {
return String.fromCharCode(parseInt(hex, 16));
});
// Useful in token tests
input = input.replace(/\\([0-7]{1,3})/g, (_, digits) => {
return String.fromCharCode(parseInt(digits, 8));
});
// Evaluate inline JavaScript
input = input.replace(/\$\{(.+?)\}/g, (_, code) => {
return vm.runInNewContext(code) + '';
});
// Escape first symbol `\r` or `\n`, `|`, `&` for Windows
if (process.platform === 'win32') {
const firstByte = Buffer.from(input)[0];
if (firstByte === 0x0a || firstByte === 0x0d) {
input = '\\' + input;
}
input = input.replace(/\|/g, '^|');
input = input.replace(/&/g, '^&');
}
// Replace escaped tabs/form-feed in expected too
expected = expected.replace(/\\t/g, '\t');
expected = expected.replace(/\\f/g, '\f');
// Split
const expectedLines = expected.split(/\n/g).slice(0, -1);
const fullExpected = expectedLines.map((line) => {
if (line.startsWith('/')) {
return new RegExp(line.trim().slice(1, -1));
} else {
return line;
}
});
for (const mode of modes) {
for (const ty of types) {
if (meta.skip === true || (process.env.ONLY === 'true' && !meta.only)) {
continue;
}
runSingleTest(mode, ty, meta, input, fullExpected);
}
}
});
}
function runGroup(group: Group) {
describe(group.name + ` at ${name}.md:${group.line + 1}`, function() {
this.timeout(60000);
for (const child of group.children) {
runGroup(child);
}
for (const test of group.tests) {
runTest(test);
}
});
}
for (const group of groups) {
runGroup(group);
}
}
run('request/sample');
run('request/lenient-headers');
run('request/lenient-version');
run('request/method');
run('request/uri');
run('request/connection');
run('request/content-length');
run('request/transfer-encoding');
run('request/invalid');
run('request/finish');
run('request/pausing');
run('request/pipelining');
run('response/sample');
run('response/connection');
run('response/content-length');
run('response/transfer-encoding');
run('response/invalid');
run('response/finish');
run('response/lenient-version');
run('response/pausing');
run('response/pipelining');
run('url');
|