2 // Copyright © 2011-2017 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.
28 "github.com/gmallard/stompngo/senv"
32 Host and port for Dial.
34 func badVerHostAndPort() (string, string) {
35 h
:= os
.Getenv("STOMP_HOSTBV") // export only if you understand these tests
39 p
:= os
.Getenv("STOMP_PORTBV") // export only if you understand these tests
47 Check if 1.1+ style Headers are needed, and return appropriate Headers.
49 func check11(h Headers
) Headers
{
50 v
:= os
.Getenv("STOMP_TEST11p")
55 v
= SPL_11
// Just use 1.1
57 h
= h
.Add(HK_ACCEPT_VERSION
, v
)
58 s
:= "localhost" // STOMP 1.1 vhost (configure for Apollo)
59 if os
.Getenv("STOMP_RMQ") != "" { // Rabbitmq default vhost
67 Return headers appropriate for the protocol level.
69 func headersProtocol(h Headers
, protocol
string) Headers
{
70 if protocol
== SPL_10
{
73 h
= h
.Add(HK_ACCEPT_VERSION
, protocol
)
74 vh
:= "localhost" // STOMP 1.{1,2} vhost
75 if os
.Getenv("STOMP_RMQ") != "" { // Rabbitmq default vhost
78 h
= h
.Add(HK_HOST
, vh
).Add(HK_HEART_BEAT
, senv
.Heartbeats())
85 func checkReceived(t
*testing
.T
, conn
*Connection
) {
88 case md
= <-conn
.MessageData
:
90 t
.Fatalf("Unexpected frame received, got [%v]\n", md
)
98 func checkReceivedMD(t
*testing
.T
, conn
*Connection
,
99 sc
<-chan MessageData
, id
string) {
102 case md
= <-conn
.MessageData
:
104 t
.Fatalf("id: read channel error: expected [nil], got: [%v]\n",
105 id
, md
.Message
.Command
)
109 t
.Fatalf("id: receive error: [%v]\n",
116 Close a network connection.
118 func closeConn(t
*testing
.T
, n net
.Conn
) error
{
122 t
.Fatalf("Unexpected n.Close() error: %v\n", err
)
130 func getMessageData(sc
<-chan MessageData
, conn
*Connection
, t
*testing
.T
) (md MessageData
) {
131 // When this is called, there should not be any MessageData instance
132 // available on the connection level MessageData channel.
135 case md
= <-conn
.MessageData
:
137 t
.Fatalf("read channel error: expected [nil], got: [%v]\n",
144 Open a network connection.
146 func openConn(t
*testing
.T
) (net
.Conn
, error
) {
147 h
, p
:= senv
.HostAndPort()
148 hap
:= net
.JoinHostPort(h
, p
)
149 n
, err
:= net
.Dial(NetProtoTCP
, hap
)
152 t
.Fatalf("Unexpected net.Dial error: %v\n", err
)
158 Test helper. Send multiple messages.
160 func sendMultiple(md multi_send_data
) error
{
161 h
:= Headers
{HK_DESTINATION
, md
.dest
}
162 for i
:= 0; i
< md
.count
; i
++ {
163 cstr
:= fmt
.Sprintf("%d", i
)
164 mts
:= md
.mpref
+ cstr
165 e
= md
.conn
.Send(h
, mts
)
174 Test helper. Send multiple []byte messages.
176 func sendMultipleBytes(md multi_send_data
) error
{
177 h
:= Headers
{HK_DESTINATION
, md
.dest
}
178 for i
:= 0; i
< md
.count
; i
++ {
179 cstr
:= fmt
.Sprintf("%d", i
)
180 mts
:= md
.mpref
+ cstr
181 e
= md
.conn
.SendBytes(h
, []byte(mts
))
190 Test helper. Get properly formatted destination.
192 func tdest(d
string) string {
193 if brokerid
!= TEST_ARTEMIS
{
197 if strings
.Index(d
, "topic") >= 0 {
200 return pref
+ strings
.Replace(d
, "/", ".", -1)
204 Test debug helper. Get properly formatted destination.
206 func tdumpmd(md MessageData
) {
207 fmt
.Printf("Command: %s\n", md
.Message
.Command
)
208 fmt
.Println("Headers:")
209 for i
:= 0; i
< len(md
.Message
.Headers
); i
+= 2 {
210 fmt
.Printf("key:%s\t\tvalue:%s\n",
211 md
.Message
.Headers
[i
], md
.Message
.Headers
[i
+1])
213 hdb
:= hex
.Dump(md
.Message
.Body
)
214 fmt
.Printf("Body: %s\n", hdb
)
216 fmt
.Printf("Error: %s\n", md
.Error
.Error())
218 fmt
.Println("Error: nil")
223 Test helper. Check disconnect error.
225 func checkDisconnectError(t
*testing
.T
, e error
) {
230 t
.Fatalf("DISCONNECT Error: expected nil, got:<%v>\n", e
)
234 Test helper. Fix up destination
236 func fixHeaderDest(h Headers
) Headers
{
238 for i
:= 0; i
< len(h
); i
+= 2 {
239 if r
[i
] == HK_DESTINATION
{
240 r
[i
+1] = tdest(r
[i
+1])
247 Test helper. Set which broker is being tested.
249 func setTestBroker() int {
250 brokerid
= TEST_ANYBROKER
251 if os
.Getenv("STOMP_AMQ") != "" {
253 } else if os
.Getenv("STOMP_RMQ") != "" {
255 } else if os
.Getenv("STOMP_ARTEMIS") != "" {
256 brokerid
= TEST_ARTEMIS
257 } else if os
.Getenv("STOMP_APOLLO") != "" {
258 brokerid
= TEST_APOLLO
264 Test helper. Set long heartbeat test flag.
266 func setHeartBeatFlags() {
267 if os
.Getenv("STOMP_HBLONG") == "Y" { // Note: a single value to run long hb tests
268 testhbrd
.testhbl
= true
270 if os
.Getenv("STOMP_HBVERBOSE") != "" { // Any value will do
271 testhbrd
.testhbvb
= true
277 Test helper. Check for missing headers
279 func checkDupeHeaders(ms
, wh Headers
) error
{
280 for i
:= 0; i
< len(wh
); i
+= 2 {
281 if !ms
.ContainsKV(wh
[i
], wh
[i
+1]) {
282 return Error("missing header values")