Version bump.
[stompngo.git] / codec_test.go
blobf06d07bde9cea56cb5380f264322d5138ecb38ac
1 //
2 // Copyright © 2011-2016 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 "os"
21 "testing"
22 "time"
25 type testdata struct {
26 encoded string
27 decoded string
30 var tdList = []testdata{
31 {"stringa", "stringa"},
32 {"stringb", "stringb"},
33 {"stringc", "stringc"},
34 {"stringd", "stringd"},
35 {"stringe", "stringe"},
36 {"stringf", "stringf"},
37 {"stringg", "stringg"},
38 {"stringh", "stringh"},
39 {"\\\\", "\\"},
40 {"\\n", "\n"},
41 {"\\c", ":"},
42 {"\\\\\\n\\c", "\\\n:"},
43 {"\\c\\n\\\\", ":\n\\"},
44 {"\\\\\\c", "\\:"},
45 {"c\\cc", "c:c"},
46 {"n\\nn", "n\nn"},
49 // Test STOMP 1.1 Header Codec - Basic Encode.
50 func TestCodecEncodeBasic(t *testing.T) {
51 for _, ede := range tdList {
52 ev := encode(ede.decoded)
53 if ede.encoded != ev {
54 t.Fatalf("ENCODE ERROR: expected: [%v] got: [%v]", ede.encoded, ev)
60 Test STOMP 1.1 Header Codec - Basic Decode.
62 func TestCodecDecodeBasic(t *testing.T) {
63 for _, ede := range tdList {
64 dv := decode(ede.encoded)
65 if ede.decoded != dv {
66 t.Fatalf("DECODE ERROR: expected: [%v] got: [%v]", ede.decoded, dv)
71 func BenchmarkCodecEncode(b *testing.B) {
72 for i := 0; i < len(tdList); i++ {
73 for n := 0; n < b.N; n++ {
74 _ = encode(tdList[i].decoded)
79 func BenchmarkCodecDecode(b *testing.B) {
80 for i := 0; i < len(tdList); i++ {
81 for n := 0; n < b.N; n++ {
82 _ = decode(tdList[i].encoded)
88 Test STOMP 1.1 Send / Receive - no codec error.
90 func TestCodec11SendRecvCodec(t *testing.T) {
91 if os.Getenv("STOMP_TEST11p") == "" {
92 t.Skip("Test11SendRecvCodec norun")
95 n, _ := openConn(t)
96 ch := check11(TEST_HEADERS)
97 conn, _ := Connect(n, ch)
99 d := tdest("/queue/gostomp.11sendrecv.2")
100 ms := "11sendrecv.2 - message 1"
101 wh := Headers{HK_DESTINATION, d}
103 sh := wh.Clone()
104 // Excercise the 1.1 Header Codec
105 k1 := "key:one"
106 v1 := "value\\one"
107 sh = sh.Add(k1, v1)
108 k2 := "key/ntwo"
109 v2 := "value:two\\back:slash"
110 sh = sh.Add(k2, v2)
111 k3 := "key:three/naaa\\bbb"
112 v3 := "value\\three:aaa/nbbb"
113 sh = sh.Add(k3, v3)
115 // Send
116 e := conn.Send(sh, ms)
117 if e != nil {
118 t.Fatalf("11Send failed: %v", e)
121 // Wait for server to deliver ERROR
122 time.Sleep(1e9) // Wait one
123 // Poll for adhoc ERROR from server
124 select {
125 case v := <-conn.MessageData:
126 t.Fatalf("11Adhoc Error: [%v]", v)
127 default:
130 // Subscribe
131 sbh := wh.Add(HK_ID, d)
132 sc, e := conn.Subscribe(sbh)
133 if e != nil {
134 t.Fatalf("11Subscribe failed: %v", e)
136 if sc == nil {
137 t.Fatalf("11Subscribe sub chan is nil")
140 // Read MessageData
141 var md MessageData
142 select {
143 case md = <-sc:
144 case md = <-conn.MessageData:
145 t.Fatalf("read channel error: expected [nil], got: [%v]\n",
146 md.Message.Command)
149 if md.Error != nil {
150 t.Fatalf("11Receive error: [%v]\n", md.Error)
152 // Check data and header values
153 b := md.Message.BodyString()
154 if b != ms {
155 t.Fatalf("11Receive expected: [%v] got: [%v]\n", ms, b)
157 if md.Message.Headers.Value(k1) != v1 {
158 t.Fatalf("11Receive header expected: [%v] got: [%v]\n", v1, md.Message.Headers.Value(k1))
160 if md.Message.Headers.Value(k2) != v2 {
161 t.Fatalf("11Receive header expected: [%v] got: [%v]\n", v2, md.Message.Headers.Value(k2))
163 if md.Message.Headers.Value(k3) != v3 {
164 t.Fatalf("11Receive header expected: [%v] got: [%v]\n", v3, md.Message.Headers.Value(k3))
166 // Unsubscribe
167 e = conn.Unsubscribe(sbh)
168 if e != nil {
169 t.Fatalf("11Unsubscribe failed: %v", e)
172 _ = conn.Disconnect(empty_headers)
173 _ = closeConn(t, n)