2 // Copyright © 2011-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.
29 "github.com/gmallard/stompngo/senv"
33 Host and port for Dial.
35 func badVerHostAndPort() (string, string) {
36 h
:= os
.Getenv("STOMP_HOSTBV") // export only if you understand these tests
40 p
:= os
.Getenv("STOMP_PORTBV") // export only if you understand these tests
48 Check if 1.1+ style Headers are needed, and return appropriate Headers.
50 func check11(h Headers
) Headers
{
51 v
:= os
.Getenv("STOMP_TEST11p")
56 v
= SPL_11
// Just use 1.1
58 h
= h
.Add(HK_ACCEPT_VERSION
, v
)
59 s
:= "localhost" // STOMP 1.1 vhost (configure for Apollo)
60 if os
.Getenv("STOMP_RMQ") != "" { // Rabbitmq default vhost
68 Return headers appropriate for the protocol level.
70 func headersProtocol(h Headers
, protocol
string) Headers
{
71 if protocol
== SPL_10
{
74 h
= h
.Add(HK_ACCEPT_VERSION
, protocol
)
75 vh
:= "localhost" // STOMP 1.{1,2} vhost
76 if os
.Getenv("STOMP_RMQ") != "" { // Rabbitmq default vhost
79 h
= h
.Add(HK_HOST
, vh
).Add(HK_HEART_BEAT
, senv
.Heartbeats())
86 func checkReceived(t
*testing
.T
, conn
*Connection
, eofok
bool) {
89 case md
= <-conn
.MessageData
:
90 log
.Printf("md is [%q]\n", md
)
91 if eofok
&& md
.Error
== nil {
94 if eofok
&& md
.Error
.Error() == "EOF" {
98 t
.Fatalf("Unexpected frame received, got [%#v]\n", md
)
106 func checkReceivedMD(t
*testing
.T
, conn
*Connection
,
107 sc
<-chan MessageData
, id
string) {
110 case md
= <-conn
.MessageData
:
112 t
.Fatalf("id: read channel error: expected [nil], got: [%#v] [%#v]\n",
113 id
, md
.Message
.Command
)
117 t
.Fatalf("id: receive error: [%#v] [%#v]\n",
124 Close a network connection.
126 func closeConn(t
*testing
.T
, n net
.Conn
) error
{
130 t
.Fatalf("Unexpected n.Close() error: %#v\n", err
)
138 func getMessageData(sc
<-chan MessageData
, conn
*Connection
, t
*testing
.T
) (md MessageData
) {
139 // When this is called, there should not be any MessageData instance
140 // available on the connection level MessageData channel.
143 case md
= <-conn
.MessageData
:
145 t
.Fatalf("read channel error: expected [nil], got: [%#v]\n",
152 Open a network connection.
154 func openConn(t
*testing
.T
) (net
.Conn
, error
) {
155 h
, p
:= senv
.HostAndPort()
156 hap
:= net
.JoinHostPort(h
, p
)
157 n
, err
:= net
.Dial(NetProtoTCP
, hap
)
160 t
.Fatalf("Unexpected net.Dial error: %#v\n", err
)
166 Test helper. Send multiple messages.
168 func sendMultiple(md multi_send_data
) error
{
169 h
:= Headers
{HK_DESTINATION
, md
.dest
}
170 for i
:= 0; i
< md
.count
; i
++ {
171 cstr
:= fmt
.Sprintf("%d", i
)
172 mts
:= md
.mpref
+ cstr
173 e
= md
.conn
.Send(h
, mts
)
182 Test helper. Send multiple []byte messages.
184 func sendMultipleBytes(md multi_send_data
) error
{
185 h
:= Headers
{HK_DESTINATION
, md
.dest
}
186 for i
:= 0; i
< md
.count
; i
++ {
187 cstr
:= fmt
.Sprintf("%d", i
)
188 mts
:= md
.mpref
+ cstr
189 e
= md
.conn
.SendBytes(h
, []byte(mts
))
198 Test helper. Get properly formatted destination.
200 func tdest(d
string) string {
201 if brokerid
!= TEST_ARTEMIS
{
205 if strings
.Index(d
, "topic") >= 0 {
208 return pref
+ strings
.Replace(d
, "/", ".", -1)
212 Test debug helper. Get properly formatted destination.
214 func tdumpmd(md MessageData
) {
215 fmt
.Printf("Command: %s\n", md
.Message
.Command
)
216 fmt
.Println("Headers:")
217 for i
:= 0; i
< len(md
.Message
.Headers
); i
+= 2 {
218 fmt
.Printf("key:%s\t\tvalue:%s\n",
219 md
.Message
.Headers
[i
], md
.Message
.Headers
[i
+1])
221 hdb
:= hex
.Dump(md
.Message
.Body
)
222 fmt
.Printf("Body: %s\n", hdb
)
224 fmt
.Printf("Error: %s\n", md
.Error
.Error())
226 fmt
.Println("Error: nil")
231 Test helper. Check disconnect error.
233 func checkDisconnectError(t
*testing
.T
, e error
) {
238 t
.Fatalf("DISCONNECT Error: expected nil, got:<%#v>\n", e
)
242 Test helper. Fix up destination
244 func fixHeaderDest(h Headers
) Headers
{
246 for i
:= 0; i
< len(h
); i
+= 2 {
247 if r
[i
] == HK_DESTINATION
{
248 r
[i
+1] = tdest(r
[i
+1])
255 Test helper. Set which broker is being tested.
257 func setTestBroker() int {
258 brokerid
= TEST_ANYBROKER
259 if os
.Getenv("STOMP_AMQ") != "" {
261 } else if os
.Getenv("STOMP_RMQ") != "" {
263 } else if os
.Getenv("STOMP_ARTEMIS") != "" {
264 brokerid
= TEST_ARTEMIS
265 } else if os
.Getenv("STOMP_APOLLO") != "" {
266 brokerid
= TEST_APOLLO
272 Test helper. Set long heartbeat test flag.
274 func setHeartBeatFlags() {
275 if os
.Getenv("STOMP_HBLONG") == "Y" { // Note: a single value to run long hb tests
276 testhbrd
.testhbl
= true
278 if os
.Getenv("STOMP_HBVERBOSE") != "" { // Any value will do
279 testhbrd
.testhbvb
= true
285 Test helper. Check for missing headers
287 func checkDupeHeaders(ms
, wh Headers
) error
{
288 for i
:= 0; i
< len(wh
); i
+= 2 {
289 if !ms
.ContainsKV(wh
[i
], wh
[i
+1]) {
290 return Error("missing header values")