Address issue #50.
[stompngo.git] / headers_test.go
blob07f1b4ef54fabf2174defd517b1b5aa18a414ae1
1 //
2 // Copyright © 2012-2019 Guy M. Allard
3 //
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
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
17 package stompngo
19 import (
20 "testing"
24 Data Test: Headers Basic.
26 func TestHeadersBasic(t *testing.T) {
27 for _, _ = range Protocols() {
28 k := "keya"
29 v := "valuea"
30 h := Headers{k, v}
31 if nil != h.Validate() {
32 t.Fatalf("TestHeadersBasic Header validate error: [%v]\n", h.Validate())
34 if len(h) != 2 {
35 t.Fatalf("TestHeadersBasic Header Unexpected length error 1, length: [%v]\n",
36 len(h))
38 h = h.Add("keyb", "valueb").Add("keya", "valuea2")
39 if len(h) != 6 {
40 t.Fatalf("TestHeadersBasic Header Unexpected length error 2, length after add: [%v]\n",
41 len(h))
43 if _, ok := h.Contains(k); !ok {
44 t.Fatalf("TestHeadersBasic Header Unexpected false for key: [%v]\n", k)
46 k = "xyz"
47 if _, ok := h.Contains(k); ok {
48 t.Fatalf("TestHeadersBasic Header Unexpected true for key: [%v]\n", k)
51 h = Headers{k}
52 if e = h.Validate(); e != EHDRLEN {
53 t.Fatalf("TestHeadersBasic Header Validate, got [%v], expected [%v]\n",
54 e, EHDRLEN)
60 Data Test: Headers UTF8.
62 func TestHeadersUTF8(t *testing.T) {
63 for _, _ = range Protocols() {
64 k := "keya"
65 v := "valuea"
66 wh := Headers{k, v}
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)
72 if rs != "" {
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")
85 if e != EHDRUTF8 {
86 t.Fatalf("TestHeadersUTF8 Unexpected UTF8 error 4, got [%v], expected [%v]\n",
87 e, EHDRUTF8)
89 if rs != "\x80" {
90 t.Fatalf("TestHeadersUTF8 Unexpected UTF8 error 5, got [%v], expected [%v]\n",
91 rs, "\x80")
96 /*.
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")
102 hc := wh.Clone()
103 if !wh.Compare(hc) {
104 t.Fatalf("TestHeadersClone Unexpected false for clone: [%v], [%v]\n",
105 wh, hc)
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))
122 ol := len(hn)
123 hn = hn.Delete("ka")
124 if len(hn) != ol-2 {
125 t.Fatalf("TestHeadersAddDelete Unexpected length Delete 1, got: [%v], expected: [%v]\n",
126 len(hn), ol-2)
128 hn = hn.Delete("kcc")
129 if len(hn) != ol-4 {
130 t.Fatalf("TestHeadersAddDelete Unexpected length Delete 2, got: [%v], expected: [%v]\n",
131 len(hn), ol-4)
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")
143 if !b {
144 t.Fatalf("TestHeadersContainsKV KV01 got false, expected true")
146 b = ha.ContainsKV("kb", "zz")
147 if b {
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"}
162 b := ha.Compare(hb)
163 if !b {
164 t.Fatalf("TestHeadersCompare CMP01 Expected true, got false")
166 b = ha.Compare(hc)
167 if b {
168 t.Fatalf("TestHeadersCompare CMP02 Expected false, got true")
170 b = ha.Compare(hd)
171 if b {
172 t.Fatalf("TestHeadersCompare CMP03 Expected false, got true")
174 b = hd.Compare(ha)
175 if b {
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"}
187 s := ha.Size(false)
188 var w int64 = 4
189 if s != w {
190 t.Fatalf("TestHeadersSize SIZ01 size, got [%d], expected [%v]\n",
191 s, w)
194 ha = Headers{"kaa", "vaa2", "kba", "vba2", "kca", "vca2"}
195 s = ha.Size(true)
196 w = 3 + 1 + 4 + 1 + 3 + 1 + 4 + 1 + 3 + 1 + 4 + 1
197 if s != w {
198 t.Fatalf("TestHeadersSize SIZ02 size, got [%d] expected [%v]\n",
199 s, w)
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)
214 if e != nil {
215 t.Fatalf("TestHeadersEmtKV CHD01 Expected [nil], got [%v]\n", e)
217 e = checkHeaders(wh, SPL_11)
218 if e != nil {
219 t.Fatalf("TestHeadersEmtKV CHD02 Expected [nil], got [%v]\n", e)
222 e = checkHeaders(ek, SPL_10)
223 if e != EHDRMTK {
224 t.Fatalf("TestHeadersEmtKV CHD03 Expected [%v], got [%v]\n", EHDRMTK,
227 e = checkHeaders(ek, SPL_11)
228 if e != EHDRMTK {
229 t.Fatalf("TestHeadersEmtKV CHD04 Expected [%v], got [%v]\n", EHDRMTK,
233 e = checkHeaders(ev, SPL_10)
234 if e != EHDRMTV {
235 t.Fatalf("TestHeadersEmtKV CHD05 Expected [%v], got [%v]\n", EHDRMTV,
238 e = checkHeaders(ev, SPL_11)
239 if e != nil {
240 t.Fatalf("TestHeadersEmtKV CHD06 Expected [nil], got [%v]\n", e)