2 // Copyright © 2011-2016 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.
25 type testdata
struct {
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"},
42 {"\\\\\\n\\c", "\\\n:"},
43 {"\\c\\n\\\\", ":\n\\"},
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")
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
}
104 // Excercise the 1.1 Header Codec
109 v2
:= "value:two\\back:slash"
111 k3
:= "key:three/naaa\\bbb"
112 v3
:= "value\\three:aaa/nbbb"
116 e
:= conn
.Send(sh
, ms
)
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
125 case v
:= <-conn
.MessageData
:
126 t
.Fatalf("11Adhoc Error: [%v]", v
)
131 sbh
:= wh
.Add(HK_ID
, d
)
132 sc
, e
:= conn
.Subscribe(sbh
)
134 t
.Fatalf("11Subscribe failed: %v", e
)
137 t
.Fatalf("11Subscribe sub chan is nil")
144 case md
= <-conn
.MessageData
:
145 t
.Fatalf("read channel error: expected [nil], got: [%v]\n",
150 t
.Fatalf("11Receive error: [%v]\n", md
.Error
)
152 // Check data and header values
153 b
:= md
.Message
.BodyString()
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
))
167 e
= conn
.Unsubscribe(sbh
)
169 t
.Fatalf("11Unsubscribe failed: %v", e
)
172 _
= conn
.Disconnect(empty_headers
)