2 // Copyright © 2012-2019 Guy M. Allard
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
24 Data Test: Headers Basic.
26 func TestHeadersBasic(t
*testing
.T
) {
27 for _
, _
= range Protocols() {
31 if nil != h
.Validate() {
32 t
.Fatalf("TestHeadersBasic Header validate error: [%v]\n", h
.Validate())
35 t
.Fatalf("TestHeadersBasic Header Unexpected length error 1, length: [%v]\n",
38 h
= h
.Add("keyb", "valueb").Add("keya", "valuea2")
40 t
.Fatalf("TestHeadersBasic Header Unexpected length error 2, length after add: [%v]\n",
43 if _
, ok
:= h
.Contains(k
); !ok
{
44 t
.Fatalf("TestHeadersBasic Header Unexpected false for key: [%v]\n", k
)
47 if _
, ok
:= h
.Contains(k
); ok
{
48 t
.Fatalf("TestHeadersBasic Header Unexpected true for key: [%v]\n", k
)
52 if e
= h
.Validate(); e
!= EHDRLEN
{
53 t
.Fatalf("TestHeadersBasic Header Validate, got [%v], expected [%v]\n",
60 Data Test: Headers UTF8.
62 func TestHeadersUTF8(t
*testing
.T
) {
63 for _
, _
= range Protocols() {
67 var e error
// An error
68 var rs
string // Result string
69 if rs
, e
= wh
.ValidateUTF8(); e
!= nil {
70 t
.Fatalf("TestHeadersUTF8 Unexpected UTF8 error 1: [%v]\n", e
)
73 t
.Fatalf("TestHeadersUTF8 Unexpected UTF8 error 1B, got [%v], expected [%v]\n", rs
, "")
76 wh
= Headers
{k
, v
, `“Iñtërnâtiônàlizætiøn”`, "valueb", "keyc", `“Iñtërnâtiônàlizætiøn”`}
77 if _
, e
= wh
.ValidateUTF8(); e
!= nil {
78 t
.Fatalf("TestHeadersUTF8 Unexpected UTF8 error 2: [%v]\n", e
)
81 wh
= Headers
{k
, v
, `“Iñtërnâtiônàlizætiøn”`, "\x80", "keyc", `“Iñtërnâtiônàlizætiøn”`}
82 if rs
, e
= wh
.ValidateUTF8(); e
== nil {
83 t
.Fatalf("TestHeadersUTF8 Unexpected UTF8 error 3, got nil, expected an error")
86 t
.Fatalf("TestHeadersUTF8 Unexpected UTF8 error 4, got [%v], expected [%v]\n",
90 t
.Fatalf("TestHeadersUTF8 Unexpected UTF8 error 5, got [%v], expected [%v]\n",
97 Data Test: Headers Clone
99 func TestHeadersClone(t
*testing
.T
) {
100 for _
, _
= range Protocols() {
101 wh
:= Headers
{"ka", "va"}.Add("kb", "vb").Add("kc", "vc")
104 t
.Fatalf("TestHeadersClone Unexpected false for clone: [%v], [%v]\n",
111 Data Test: Headers Add / Delete.
113 func TestHeadersAddDelete(t
*testing
.T
) {
114 for _
, _
= range Protocols() {
115 ha
:= Headers
{"ka", "va", "kb", "vb", "kc", "vc"}
116 hb
:= Headers
{"kaa", "va", "kbb", "vb", "kcc", "vc"}
117 hn
:= ha
.AddHeaders(hb
)
118 if len(ha
)+len(hb
) != len(hn
) {
119 t
.Fatalf("TestHeadersAddDelete Unexpected length AddHeaders, got: [%v], expected: [%v]\n",
120 len(hn
), len(ha
)+len(hb
))
125 t
.Fatalf("TestHeadersAddDelete Unexpected length Delete 1, got: [%v], expected: [%v]\n",
128 hn
= hn
.Delete("kcc")
130 t
.Fatalf("TestHeadersAddDelete Unexpected length Delete 2, got: [%v], expected: [%v]\n",
137 Data Test: Headers ContainsKV
139 func TestHeadersContainsKV(t
*testing
.T
) {
140 for _
, _
= range Protocols() {
141 ha
:= Headers
{"ka", "va", "kb", "vb", "kc", "vc"}
142 b
:= ha
.ContainsKV("kb", "vb")
144 t
.Fatalf("TestHeadersContainsKV KV01 got false, expected true")
146 b
= ha
.ContainsKV("kb", "zz")
148 t
.Fatalf("TestHeadersContainsKV KV02 got true, expected false")
154 Data Test: Headers Compare
156 func TestHeadersCompare(t
*testing
.T
) {
157 for _
, _
= range Protocols() {
158 ha
:= Headers
{"ka", "va", "kb", "vb", "kc", "vc"}
159 hb
:= Headers
{"ka", "va", "kb", "vb", "kc", "vc"}
160 hc
:= Headers
{"ka", "va"}
161 hd
:= Headers
{"k1", "v1", "k2", "v2", "k3", "v3"}
164 t
.Fatalf("TestHeadersCompare CMP01 Expected true, got false")
168 t
.Fatalf("TestHeadersCompare CMP02 Expected false, got true")
172 t
.Fatalf("TestHeadersCompare CMP03 Expected false, got true")
176 t
.Fatalf("TestHeadersCompare CMP04 Expected false, got true")
182 Data Test: Headers Size
184 func TestHeadersSize(t
*testing
.T
) {
185 for _
, _
= range Protocols() {
186 ha
:= Headers
{"k", "v"}
190 t
.Fatalf("TestHeadersSize SIZ01 size, got [%d], expected [%v]\n",
194 ha
= Headers
{"kaa", "vaa2", "kba", "vba2", "kca", "vca2"}
196 w
= 3 + 1 + 4 + 1 + 3 + 1 + 4 + 1 + 3 + 1 + 4 + 1
198 t
.Fatalf("TestHeadersSize SIZ02 size, got [%d] expected [%v]\n",
205 Data Test: Empty Header Key / Value
207 func TestHeadersEmtKV(t
*testing
.T
) {
208 for _
, _
= range Protocols() {
209 wh
:= Headers
{"a", "b", "c", "d"} // work headers
210 ek
:= Headers
{"a", "b", "", "d"} // empty key
211 ev
:= Headers
{"a", "", "c", "d"} // empty value
213 e
= checkHeaders(wh
, SPL_10
)
215 t
.Fatalf("TestHeadersEmtKV CHD01 Expected [nil], got [%v]\n", e
)
217 e
= checkHeaders(wh
, SPL_11
)
219 t
.Fatalf("TestHeadersEmtKV CHD02 Expected [nil], got [%v]\n", e
)
222 e
= checkHeaders(ek
, SPL_10
)
224 t
.Fatalf("TestHeadersEmtKV CHD03 Expected [%v], got [%v]\n", EHDRMTK
,
227 e
= checkHeaders(ek
, SPL_11
)
229 t
.Fatalf("TestHeadersEmtKV CHD04 Expected [%v], got [%v]\n", EHDRMTK
,
233 e
= checkHeaders(ev
, SPL_10
)
235 t
.Fatalf("TestHeadersEmtKV CHD05 Expected [%v], got [%v]\n", EHDRMTV
,
238 e
= checkHeaders(ev
, SPL_11
)
240 t
.Fatalf("TestHeadersEmtKV CHD06 Expected [nil], got [%v]\n", e
)