Version bump.
[stompngo.git] / data_test.go
blob7d87ca72021241c787eda4931d75886c1f64b480
1 //
2 // Copyright © 2011-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: Frame Basic.
26 func TestDataFrameBasic(t *testing.T) {
27 cm := CONNECT
28 wh := Headers{"keya", "valuea"}
29 ms := "The Message Body"
30 f := &Frame{Command: cm, Headers: wh, Body: []byte(ms)}
32 if cm != f.Command {
33 t.Fatalf("TestDataFrameBasic Command, expected: [%v], got [%v]\n",
34 cm, f.Command)
36 if !wh.Compare(f.Headers) {
37 t.Fatalf("TestDataFrameBasic Headers, expected: [true], got [false], for [%v] [%v]\n",
38 wh, f.Headers)
40 if ms != string(f.Body) {
41 t.Fatalf("TestDataFrameBasic Body string, expected: [%v], got [%v]\n",
42 ms, string(f.Body))
47 Data Test: Message Basic.
49 func TestDataMessageBasic(t *testing.T) {
50 fc := CONNECT
51 wh := Headers{"keya", "valuea"}
52 ms := "The Message Body"
53 m := &Message{Command: fc, Headers: wh, Body: []byte(ms)}
55 if fc != m.Command {
56 t.Fatalf("TestDataMessageBasic Command, expected: [%v], got [%v]\n",
57 fc, m.Command)
59 if !wh.Compare(m.Headers) {
60 t.Fatalf("TestDataMessageBasic Headers, expected: [true], got [false], for [%v] [%v]\n",
61 wh, m.Headers)
63 if ms != m.BodyString() {
64 t.Fatalf("TestDataMessageBasic Body string, expected: [%v], got [%v]\n",
65 ms, m.BodyString())
70 Data Test: protocols.
72 func TestDataprotocols(t *testing.T) {
73 if !Supported(SPL_10) {
74 t.Fatalf("TestDataprotocolsExpected: [true], got: [false] for protocol level %v\n",
75 SPL_10)
77 if !Supported(SPL_11) {
78 t.Fatalf("TestDataprotocols Expected: [true], got: [false] for protocol level %v\n",
79 SPL_11)
81 if !Supported(SPL_12) {
82 t.Fatalf("TestDataprotocols Expected: [true], got: [false] for protocol level %v\n",
83 SPL_12)
85 if Supported("9.9") {
86 t.Fatalf("TestDataprotocols Expected: [false], got: [true] for protocol level %v\n",
87 "9.9")
90 for _, v := range suptests {
91 b := Supported(v.v)
92 if b != v.s {
93 t.Fatalf("TestDataprotocols Expected: [%v] for protocol level [%v]\n",
94 v.s, v.v)
100 Data test: Protocols.
102 func TestDataProtocols(t *testing.T) {
103 for i, p := range Protocols() {
104 if supported[i] != p {
105 t.Fatalf("TestDataProtocols Expected [%v], got [%v]\n",
106 supported[i], p)
112 Data test: Error.
114 func TestDataError(t *testing.T) {
115 es := "An error string"
116 e = Error(es)
117 if es != e.Error() {
118 t.Fatalf("TestDataError Expected [%v], got [%v]\n", es, e.Error())
123 Data Test: Message Size.
125 func TestDataMessageSize(t *testing.T) {
126 f := CONNECT
127 wh := Headers{"keya", "valuea"}
128 ms := "The Message Body"
129 m := &Message{Command: f, Headers: wh, Body: []byte(ms)}
130 b := false
132 var w int64 = int64(len(CONNECT)) + 1 + wh.Size(b) + 1 + int64(len(ms)) + 1
133 r := m.Size(b)
134 if w != r {
135 t.Fatalf("TestDataMessageSize Message size, expected: [%d], got [%d]\n",
136 w, r)
141 Data Test: Broker Command Validity.
143 func TestDataBrokerCmdVal(t *testing.T) {
144 var tData = map[string]bool{MESSAGE: true, ERROR: true, RECEIPT: true,
145 CONNECT: false, DISCONNECT: false, SUBSCRIBE: false, BEGIN: false,
146 STOMP: false, COMMIT: false, ABORT: false, UNSUBSCRIBE: false,
147 SEND: false, ACK: false, NACK: false, CONNECTED: false,
148 "JUNK": false}
149 for k, v := range tData {
150 if v != validCmds[k] {
151 t.Fatalf("TestDataBrokerCmdVal Command Validity, expected: [%t], got [%t] for key [%s]\n",
153 validCmds[k], k)
158 func BenchmarkHeaderAdd(b *testing.B) {
159 h := Headers{"k1", "v1"}
160 for n := 0; n < b.N; n++ {
161 _ = h.Add("akey", "avalue")
165 func BenchmarkHeaderAppend(b *testing.B) {
166 h := []string{"k1", "v1"}
167 for n := 0; n < b.N; n++ {
168 _ = append(h, "akey", "avalue")