forked from gmallard/stompngo
/
utils_test.go
181 lines (165 loc) · 3.93 KB
/
utils_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//
// Copyright © 2011-2014 Guy M. Allard
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package stompngo
import (
"fmt"
"net"
"os"
"testing"
//
"github.com/gmallard/stompngo/senv"
)
var TEST_HEADERS = Headers{"login", "guest", "passcode", "guest"}
var TEST_TDESTPREF = "/queue/test.pref."
var TEST_TRANID = "TransactionA"
var empty_headers = Headers{}
type multi_send_data struct {
conn *Connection // this connection
dest string // queue/topic name
mpref string // message prefix
count int // number of messages
}
type frameData struct {
data string
resp error
}
var frames = []frameData{ // Many are possible but very unlikely
{"EBADFRM", EBADFRM},
{"EUNKFRM\n\n\x00", EUNKFRM},
{"ERROR\n\n\x00", nil},
{"ERROR\n\x00", EBADFRM},
{"ERROR\n\n", EBADFRM},
{"ERROR\nbadconhdr\n\n\x00", EUNKHDR},
{"ERROR\nbadcon:badmsg\n\n\x00", nil},
{"ERROR\nbadcon:badmsg\n\nbad message\x00", nil},
{"CONNECTED\n\n\x00", nil},
{"CONNECTED\n\nconnbody\x00", EBDYDATA},
{"CONNECTED\n\nconnbadbody", EBDYDATA},
{"CONNECTED\nk1:v1\nk2:v2\n\nconnbody\x00", EBDYDATA},
{"CONNECTED\nk1:v1\nk2:v2\n\nconnbody", EBDYDATA},
{"CONNECTED\nk1:v1\nk2:v2\n\n\x00", nil},
}
/*
Open a network connection.
*/
func openConn(t *testing.T) (net.Conn, error) {
h, p := hostAndPort()
n, err := net.Dial("tcp", net.JoinHostPort(h, p))
if err != nil {
t.Errorf("Unexpected net.Dial error: %v\n", err)
}
return n, err
}
/*
Close a network connection.
*/
func closeConn(t *testing.T, n net.Conn) error {
err := n.Close()
if err != nil {
t.Errorf("Unexpected n.Close() error: %v\n", err)
}
return err
}
/*
Host and port for Dial
*/
func hostAndPort() (string, string) {
return senv.HostAndPort()
}
/*
Check if 1.1+ style Headers are needed, and return appropriate Headers.
*/
func check11(h Headers) Headers {
v := os.Getenv("STOMP_TEST11p")
if v == "" {
return h
}
if !Supported(v) {
v = SPL_11 // Just use 1.1
}
h = h.Add("accept-version", v)
s := "localhost" // STOMP 1.1 vhost (configure for Apollo)
if os.Getenv("STOMP_RMQ") != "" { // Rabbitmq default vhost
s = "/"
}
h = h.Add("host", s)
return h
}
/*
Test helper. Send multiple messages.
*/
func sendMultiple(md multi_send_data) error {
h := Headers{"destination", md.dest}
for i := 0; i < md.count; i++ {
cstr := fmt.Sprintf("%d", i)
mts := md.mpref + cstr
e := md.conn.Send(h, mts)
if e != nil {
return e // now
}
}
return nil
}
/*
Test helper. Send multiple []byte messages.
*/
func sendMultipleBytes(md multi_send_data) error {
h := Headers{"destination", md.dest}
for i := 0; i < md.count; i++ {
cstr := fmt.Sprintf("%d", i)
mts := md.mpref + cstr
e := md.conn.SendBytes(h, []byte(mts))
if e != nil {
return e // now
}
}
return nil
}
/*
Test helper.
*/
func getMessageData(c *Connection, s <-chan MessageData) (r MessageData) {
if os.Getenv("STOMP_TEST11p") == "" {
r = <-c.MessageData
} else {
r = <-s
}
return r
}
/*
Test helper.
*/
func checkReceived(t *testing.T, c *Connection, id string) {
select {
case v := <-c.MessageData:
t.Errorf("Unexpected frame received, id [%s], got [%v]\n", id, v)
default:
}
}
/*
Host and port for Dial.
*/
func badVerHostAndPort() (string, string) {
h := os.Getenv("STOMP_HOSTBV") // export only if you understand these tests
if h == "" {
h = "localhost"
}
p := os.Getenv("STOMP_PORTBV") // export only if you understand these tests
if p == "" {
p = "61613"
}
return h, p
}