-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathV1BinaryProxyProtocol.test.ts
149 lines (126 loc) · 5.89 KB
/
V1BinaryProxyProtocol.test.ts
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
import { INETProtocol } from './enum/INETProtocol';
import { Peer } from './Peer';
import { V1BinaryProxyProtocol, V1BinaryProxyProtocolParseError } from './V1BinaryProxyProtocol';
test('shuold parse V1 PROXY protocol with data as byte array successfully', () => {
const protocolText = 'PROXY TCP4 127.0.0.1 192.0.2.1 12345 54321\r\nHTTP/1.0 200 OK\r\n\r\nhello, world';
const protocolBuf = Buffer.from(protocolText, 'utf8');
const expected = new V1BinaryProxyProtocol(
INETProtocol.TCP4,
new Peer('127.0.0.1', 12345),
new Peer('192.0.2.1', 54321),
Buffer.from('HTTP/1.0 200 OK\r\n\r\nhello, world', 'utf8'),
);
expect(V1BinaryProxyProtocol.parse(protocolBuf)).toEqual(expected);
expect(expected.build()).toEqual(protocolBuf);
});
test('should parse TCP4 V1 PROXY protocol without data as byte array successfully', () => {
const protocolText = 'PROXY TCP4 127.0.0.1 192.0.2.1 12345 54321\r\n';
const protocolBuf = Buffer.from(protocolText, 'utf8');
const expected = new V1BinaryProxyProtocol(
INETProtocol.TCP4,
new Peer('127.0.0.1', 12345),
new Peer('192.0.2.1', 54321),
new Uint8Array(),
);
expect(V1BinaryProxyProtocol.parse(protocolBuf)).toEqual(expected);
expect(expected.build()).toEqual(protocolBuf);
});
test('should build V1 PROXY protocol without data as byte array successfully', () => {
const proto = new V1BinaryProxyProtocol(
INETProtocol.TCP4,
new Peer('127.0.0.1', 12345),
new Peer('192.0.2.1', 54321),
);
const expectedProtocolText = 'PROXY TCP4 127.0.0.1 192.0.2.1 12345 54321\r\n';
const expectedProtocolBuf = Buffer.from(expectedProtocolText, 'utf8');
expect(proto.build()).toEqual(expectedProtocolBuf);
});
test('should parse TCP6 V1 PROXY protocol without data as byte array successfully', () => {
const protocolText = 'PROXY TCP6 127.0.0.1 192.0.2.1 12345 54321\r\n';
const protocolBuf = Buffer.from(protocolText, 'utf8');
const expected = new V1BinaryProxyProtocol(
INETProtocol.TCP6,
new Peer('127.0.0.1', 12345),
new Peer('192.0.2.1', 54321),
new Uint8Array(),
);
expect(V1BinaryProxyProtocol.parse(protocolBuf)).toEqual(expected);
expect(expected.build()).toEqual(protocolBuf);
});
test('should parse UNKNOWN V1 PROXY protocol without data as byte array successfully', () => {
const protocolText = 'PROXY UNKNOWN 127.0.0.1 192.0.2.1 12345 54321\r\n';
const protocolBuf = Buffer.from(protocolText, 'utf8');
const expected = new V1BinaryProxyProtocol(
INETProtocol.UNKNOWN,
new Peer('127.0.0.1', 12345),
new Peer('192.0.2.1', 54321),
new Uint8Array(),
);
expect(V1BinaryProxyProtocol.parse(protocolBuf)).toEqual(expected);
expect(expected.build()).toEqual(protocolBuf);
});
test('should raise error on parsing V1 PROXY protocol without data as byte array with invalid inet protocol', () => {
const protocolText = 'PROXY INVALID 127.0.0.1 192.0.2.1 12345 54321\r\n';
const protocolBuf = Buffer.from(protocolText, 'utf8');
expect(() => {
V1BinaryProxyProtocol.parse(protocolBuf);
}).toThrowError(new V1BinaryProxyProtocolParseError('unexpected INET protocol has come'));
});
test('should raise error on parsing V1 PROXY protocol as byte array when protocol signature is invalid', () => {
const protocolText = 'INVALID TCP4 127.0.0.1 192.0.2.1 12345 54321\r\n';
const protocolBuf = Buffer.from(protocolText, 'utf8');
expect(() => {
V1BinaryProxyProtocol.parse(protocolBuf);
}).toThrowError(new V1BinaryProxyProtocolParseError("doesn't match with protocol signature"));
});
test('should raise error on parsing V1 PROXY protocol as byte array when newline character is missing', () => {
{
const protocolText = 'PROXY TCP4 127.0.0.1 192.0.2.1 ';
const protocolBuf = Buffer.from(protocolText, 'utf8');
expect(() => {
V1BinaryProxyProtocol.parse(protocolBuf);
}).toThrowError(new V1BinaryProxyProtocolParseError('port information is missing'));
}
{
const protocolText = 'PROXY TCP4 127.0.0.1 192.0.2.1 12345 5432X';
const protocolBuf = Buffer.from(protocolText, 'utf8');
expect(() => {
V1BinaryProxyProtocol.parse(protocolBuf);
}).toThrowError(new V1BinaryProxyProtocolParseError('invalid port information has come'));
}
{
const protocolText = 'PROXY TCP4 127.0.0.1 192.0.2.1 12345 54321\rX';
const protocolBuf = Buffer.from(protocolText, 'utf8');
expect(() => {
V1BinaryProxyProtocol.parse(protocolBuf);
}).toThrowError(new V1BinaryProxyProtocolParseError('invalid port information has come'));
}
});
test('should raise error on parsing V1 PROXY protocol as byte array when ip address is missing', () => {
const protocolText = 'PROXY TCP4 192.0.2.1 12345 54321\r\n';
const protocolBuf = Buffer.from(protocolText, 'utf8');
expect(() => {
V1BinaryProxyProtocol.parse(protocolBuf);
}).toThrowError(new V1BinaryProxyProtocolParseError('address information is missing'));
});
test('should raise error on parsing V1 PROXY protocol as byte array when port is missing', () => {
const protocolText = 'PROXY TCP4 127.0.0.1 192.0.2.1 54321\r\n';
const protocolBuf = Buffer.from(protocolText, 'utf8');
expect(() => {
V1BinaryProxyProtocol.parse(protocolBuf);
}).toThrowError(new V1BinaryProxyProtocolParseError('port information is missing'));
});
test('should raise error on parsing V1 PROXY protocol as byte array when port is invalid', () => {
const protocolText = 'PROXY TCP4 127.0.0.1 192.0.2.1 ABCDE 54321\r\n';
const protocolBuf = Buffer.from(protocolText, 'utf8');
expect(() => {
V1BinaryProxyProtocol.parse(protocolBuf);
}).toThrowError(new V1BinaryProxyProtocolParseError('invalid port information has come'));
});
test('unexpected termination', () => {
const protocolText = 'PROXY';
const protocolBuf = Buffer.from(protocolText, 'utf8');
expect(() => {
V1BinaryProxyProtocol.parse(protocolBuf);
}).toThrowError(new V1BinaryProxyProtocolParseError('protocol payload has been terminated unexpectedly'));
});