forked from egirna/icap-client
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser_test.go
More file actions
206 lines (188 loc) · 6.25 KB
/
parser_test.go
File metadata and controls
206 lines (188 loc) · 6.25 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
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
package icapclient
import (
"testing"
)
func TestParser(t *testing.T) {
t.Run("setEncapsulatedHeaderValue", func(t *testing.T) {
type testSample struct {
icapReqStr string
httpReqStr string
httpRespStr string
result string
}
sampleTable := []testSample{
{
icapReqStr: "REQMOD\r\nEncapsulated: %s\r\n\r\n",
httpReqStr: "GET / HTTP/1.1\r\n" +
"Host: www.origin-server.com\r\n" +
"Accept: text/html, text/plain\r\n" +
"Accept-Encoding: compress\r\n" +
"Cookie: ff39fk3jur@4ii0e02i\r\n" +
"If-None-Match: \"xyzzy\", \"r2d2xxxx\"\r\n\r\n",
httpRespStr: "",
result: "REQMOD\r\nEncapsulated: req-hdr=0, null-body=170\r\n\r\n",
},
{
icapReqStr: "REQMOD\r\nEncapsulated: %s\r\n\r\n",
httpReqStr: "POST /origin-resource/form.pl HTTP/1.1\r\n" +
"Host: www.origin-server.com\r\n" +
"Accept: text/html, text/plain\r\n" +
"Accept-Encoding: compress\r\n" +
"Pragma: no-cache\r\n\r\n" +
"1e\r\n" +
"I am posting this information.\r\n" +
"0\r\n\r\n",
result: "REQMOD\r\nEncapsulated: req-hdr=0, req-body=147\r\n\r\n",
},
{
icapReqStr: "RESPMOD\r\nEncapsulated: %s\r\n\r\n",
httpReqStr: "GET /origin-resource HTTP/1.1\r\n" +
"Host: www.origin-server.com\r\n" +
"Accept: text/html, text/plain, image/gif\r\n" +
"Accept-Encoding: gzip, compress\r\n\r\n",
httpRespStr: "HTTP/1.1 200 OK\r\n" +
"Date: Mon, 10 Jan 2000 09:52:22 GMT\r\n" +
"Server: Apache/1.3.6 (Unix)\r\n" +
"ETag: \"63840-1ab7-378d415b\"\r\n" +
"Content-Type: text/html\r\n" +
"Content-Length: 51\r\n\r\n" +
"33\r\n" +
"This is data that was returned by an origin server.\r\n" +
"0\r\n\r\n",
result: "RESPMOD\r\nEncapsulated: req-hdr=0, res-hdr=137, res-body=296\r\n\r\n",
},
{
icapReqStr: "RESPMOD\r\nEncapsulated: %s\r\n\r\n",
httpReqStr: "POST /origin-resource/form.pl HTTP/1.1\r\n" +
"Host: www.origin-server.com\r\n" +
"Accept: text/html, text/plain\r\n" +
"Accept-Encoding: compress\r\n" +
"Pragma: no-cache\r\n\r\n" +
"1e\r\n" +
"I am posting this information.\r\n" +
"0\r\n\r\n",
httpRespStr: "HTTP/1.1 200 OK\r\n" +
"Date: Mon, 10 Jan 2000 09:52:22 GMT\r\n" +
"Server: Apache/1.3.6 (Unix)\r\n" +
"ETag: \"63840-1ab7-378d415b\"\r\n" +
"Content-Type: text/html\r\n" +
"Content-Length: 51\r\n\r\n" +
"33\r\n" +
"This is data that was returned by an origin server.\r\n" +
"0\r\n\r\n",
result: "RESPMOD\r\nEncapsulated: req-hdr=0, req-body=147, res-hdr=188, res-body=347\r\n\r\n",
},
{
icapReqStr: "RESPMOD\r\nEncapsulated: %s\r\n\r\n",
httpReqStr: "POST /origin-resource/form.pl HTTP/1.1\r\n" +
"Host: www.origin-server.com\r\n" +
"Accept: text/html, text/plain\r\n" +
"Accept-Encoding: compress\r\n" +
"Pragma: no-cache\r\n\r\n" +
"1e\r\n" +
"I am posting this information.\r\n" +
"0\r\n\r\n",
httpRespStr: "HTTP/1.1 200 OK\r\n" +
"Date: Mon, 10 Jan 2000 09:52:22 GMT\r\n" +
"Server: Apache/1.3.6 (Unix)\r\n" +
"ETag: \"63840-1ab7-378d415b\"\r\n" +
"Content-Type: text/html\r\n" +
"Content-Length: 51\r\n\r\n",
result: "RESPMOD\r\nEncapsulated: req-hdr=0, req-body=147, res-hdr=188, null-body=347\r\n\r\n",
},
{
icapReqStr: "OPTIONS\r\nEncapsulated: %s\r\n\r\n",
httpReqStr: "",
httpRespStr: "",
result: "OPTIONS\r\nEncapsulated: null-body=0\r\n\r\n",
},
{
icapReqStr: "OPTIONS\r\nEncapsulated: %s\r\n\r\n",
httpReqStr: "GET /origin-resource HTTP/1.1\r\n" +
"Host: www.origin-server.com\r\n" +
"Accept: text/html, text/plain, image/gif\r\n" +
"Accept-Encoding: gzip, compress\r\n\r\n",
httpRespStr: "",
result: "OPTIONS\r\nEncapsulated: opt-body=0\r\n\r\n",
},
}
for _, sample := range sampleTable {
setEncapsulatedHeaderValue(&sample.icapReqStr, sample.httpReqStr, sample.httpRespStr)
if sample.icapReqStr != sample.result {
t.Logf("Wanted icap message after setting encapsulation: %s , got:%s", sample.result, sample.icapReqStr)
t.Fail()
}
}
})
t.Run("addHexaBodyByteNotations", func(t *testing.T) {
type testSample struct {
msg string
result string
}
sampleTable := []testSample{
{
msg: "Hello World!",
result: "c\r\nHello World!\r\n0\r\n",
},
{
msg: "This is another message. Alright bye!",
result: "25\r\nThis is another message. Alright bye!\r\n0\r\n",
},
}
for _, sample := range sampleTable {
addHexaBodyByteNotations(&sample.msg)
if sample.msg != sample.result {
t.Logf("Wanted message after adding hexa body notations: %s, got:%s", sample.result, sample.msg)
t.Fail()
}
}
})
t.Run("parsePreviewBodyBytes", func(t *testing.T) {
type testSample struct {
previewBytes int
httpMsg string
result string
}
sampleTable := []testSample{
{
previewBytes: 10,
httpMsg: "HTTP/1.1 200 OK\r\n" +
"Date: Mon, 10 Jan 2000 09:52:22 GMT\r\n" +
"Server: Apache/1.3.6 (Unix)\r\n" +
"ETag: \"63840-1ab7-378d415b\"\r\n" +
"Content-Type: text/html\r\n" +
"Content-Length: 51\r\n\r\n" +
"This is data that was returned by an origin server.\r\n\r\n",
result: "HTTP/1.1 200 OK\r\n" +
"Date: Mon, 10 Jan 2000 09:52:22 GMT\r\n" +
"Server: Apache/1.3.6 (Unix)\r\n" +
"ETag: \"63840-1ab7-378d415b\"\r\n" +
"Content-Type: text/html\r\n" +
"Content-Length: 51\r\n\r\n" +
"This is da",
},
{
previewBytes: 10,
httpMsg: "POST /origin-resource/form.pl HTTP/1.1\r\n" +
"Host: www.origin-server.com\r\n" +
"Accept: text/html, text/plain\r\n" +
"Accept-Encoding: compress\r\n" +
"Pragma: no-cache\r\n\r\n" +
"I am posting this information.\r\n",
result: "POST /origin-resource/form.pl HTTP/1.1\r\n" +
"Host: www.origin-server.com\r\n" +
"Accept: text/html, text/plain\r\n" +
"Accept-Encoding: compress\r\n" +
"Pragma: no-cache\r\n\r\n" +
"I am posti",
},
}
for _, sample := range sampleTable {
parsePreviewBodyBytes(&sample.httpMsg, sample.previewBytes)
if sample.httpMsg != sample.result {
t.Logf("Wanted http message after parsing to be: %s , got: %s", sample.result, sample.httpMsg)
t.Fail()
}
}
})
}