-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathrequest-utils.spec.ts
More file actions
123 lines (104 loc) · 4.64 KB
/
Copy pathrequest-utils.spec.ts
File metadata and controls
123 lines (104 loc) · 4.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
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
import { Buffer } from 'buffer';
import * as zlib from 'zlib';
import * as stream from 'stream';
import { expect, nodeOnly } from './test-utils';
import { buildBodyReader, preprocessRequest } from '../src/util/request-utils';
import { LastHopEncrypted } from '../src/util/socket-extensions';
nodeOnly(() => {
describe("buildBodyReader", () => {
describe(".text", () => {
it('returns the raw text for unspecified requests', async () => {
const body = buildBodyReader(Buffer.from('hello world'), {});
expect(await body.getText()).to.equal('hello world');
});
it('returns the raw text for identity requests', async () => {
const body = buildBodyReader(Buffer.from('hello world'), {
'content-encoding': 'identity'
});
expect(await body.getText()).to.equal('hello world');
});
it('is undefined for unknown encodings', async () => {
const body = buildBodyReader(Buffer.from('hello world'), {
'content-encoding': 'randomized'
});
expect(await body.getText()).to.equal(undefined);
});
it('can decode gzip bodies', async () => {
const content = zlib.gzipSync('Gzip response');
const body = buildBodyReader(content, {
'content-encoding': 'gzip'
});
expect(await body.getText()).to.equal('Gzip response');
});
it('can decode zlib deflate bodies', async () => {
const content = zlib.deflateSync('Deflate response');
const body = buildBodyReader(content, {
'content-encoding': 'deflate'
});
expect(await body.getText()).to.equal('Deflate response');
});
it('can decode raw deflate bodies', async () => {
const content = zlib.deflateRawSync('Raw deflate response');
const body = buildBodyReader(content, {
'content-encoding': 'deflate'
});
expect(await body.getText()).to.equal('Raw deflate response');
});
it('can decode brotli bodies', async function () {
if (!zlib.brotliCompressSync) this.skip();
const content = zlib.brotliCompressSync('Brotli brotli brotli brotli brotli');
const body = buildBodyReader(content, {
'content-encoding': 'br'
});
expect(await body.getText()).to.equal('Brotli brotli brotli brotli brotli');
});
it('can decode zstandard bodies', async function () {
if (!zlib.zstdCompressSync) this.skip();
const content = zlib.zstdCompressSync('hello zstd zstd zstd world');
const body = buildBodyReader(content, {
'content-encoding': 'zstd'
});
expect(await body.getText()).to.equal('hello zstd zstd zstd world');
});
it('can decode bodies with multiple encodings', async function () {
if (!zlib.brotliCompressSync) this.skip();
const content = zlib.gzipSync(
zlib.brotliCompressSync(
'First brotli, then gzip, now this'
)
);
const body = buildBodyReader(content, {
'content-encoding': 'br, identity, gzip, identity'
});
expect(await body.getText()).to.equal('First brotli, then gzip, now this');
});
});
});
describe("preprocessRequest", () => {
it('reconstructs valid absolute URLs from bracketed IPv6 host headers', () => {
const req = Object.assign(new stream.PassThrough(), {
method: 'GET',
url: '/api',
headers: {
host: '[::1]:8000'
},
rawHeaders: ['Host', '[::1]:8000'],
httpVersion: '1.1',
socket: {
[LastHopEncrypted]: false
}
}) as any;
const result = preprocessRequest(req, {
type: 'request',
serverPort: 45454,
maxBodySize: 1024
});
expect(result).to.not.equal(null);
expect(req.url).to.equal('http://[::1]:8000/api');
expect(req.destination).to.deep.equal({
hostname: '::1',
port: 8000
});
});
});
});