Version bump.
[stompngo.git] / conndisc_test.go
blob1779cdfd0606bf12670f10e7a79f69263d9517a3
1 //
2 // Copyright © 2011-2018 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 "testing"
24 ConnDisc Test: net.Conn.
26 func TestConnCDDiscNetconn(t *testing.T) {
27 n, _ = openConn(t)
28 _ = closeConn(t, n)
32 ConnDisc Test: stompngo.Connect.
34 func TestConnCDDisc(t *testing.T) {
35 for _, sp := range Protocols() {
36 n, _ = openConn(t)
37 ch := login_headers
38 ch = headersProtocol(ch, sp)
39 conn, e = Connect(n, ch)
40 if e != nil {
41 t.Fatalf("TestConnCDDisc Expected no connect error, got [%v]\n", e)
43 if conn == nil {
44 t.Fatalf("TestConnCDDisc Expected a connection, got [nil]\n")
46 if conn.ConnectResponse.Command != CONNECTED {
47 t.Fatalf("TestConnCDDisc Expected command [%v], got [%v]\n", CONNECTED,
48 conn.ConnectResponse.Command)
50 if !conn.connected {
51 t.Fatalf("TestConnCDDisc Expected connected [true], got [false]\n")
53 if !conn.Connected() {
54 t.Fatalf("TestConnCDDisc Expected connected [true], got [false]\n")
57 if conn.Session() == "" {
58 t.Fatalf("TestConnCDDisc Expected connected session, got [default value]\n")
61 if conn.SendTickerInterval() != 0 {
62 t.Fatalf("TestConnCDDisc Expected zero SendTickerInterval, got [%v]\n",
63 conn.SendTickerInterval())
65 if conn.ReceiveTickerInterval() != 0 {
66 t.Fatalf("TestConnCDDisc Expected zero ReceiveTickerInterval, got [%v]\n",
67 conn.SendTickerInterval())
69 if conn.SendTickerCount() != 0 {
70 t.Fatalf("TestConnCDDisc Expected zero SendTickerCount, got [%v]\n",
71 conn.SendTickerCount())
73 if conn.ReceiveTickerCount() != 0 {
74 t.Fatalf("TestConnCDDisc Expected zero ReceiveTickerCount, got [%v]\n",
75 conn.SendTickerCount())
78 if conn.FramesRead() != 1 {
79 t.Fatalf("TestConnCDDisc Expected 1 frame read, got [%d]\n", conn.FramesRead())
81 if conn.BytesRead() <= 0 {
82 t.Fatalf("TestConnCDDisc Expected non-zero bytes read, got [%d]\n", conn.BytesRead())
84 if conn.FramesWritten() != 1 {
85 t.Fatalf("TestConnCDDisc Expected 1 frame written, got [%d]\n", conn.FramesWritten())
87 if conn.BytesWritten() <= 0 {
88 t.Fatalf("ETestConnCDDisc xpected non-zero bytes written, got [%d]\n",
89 conn.BytesWritten())
91 if conn.Running().Nanoseconds() == 0 {
92 t.Fatalf("TestConnCDDisc Expected non-zero runtime, got [0]\n")
95 checkReceived(t, conn, false)
96 e = conn.Disconnect(empty_headers)
97 checkDisconnectError(t, e)
98 _ = closeConn(t, n)
103 ConnDisc Test: stompngo.Disconnect with client bypassing a receipt.
105 func TestConnCDDiscNoDiscReceipt(t *testing.T) {
106 for _, sp := range Protocols() {
107 n, _ = openConn(t)
108 ch := login_headers
109 ch = headersProtocol(ch, sp)
110 conn, e = Connect(n, ch)
111 if e != nil {
112 t.Fatalf("TestConnCDDiscNoDiscReceipt Expected no connect error, got [%v]\n", e)
114 // DISCONNECT Here
115 checkReceived(t, conn, false)
116 e = conn.Disconnect(NoDiscReceipt)
117 checkDisconnectError(t, e)
118 if e != nil {
119 t.Fatalf("TestConnCDDiscNoDiscReceipt Expected no disconnect error, got [%v]\n", e)
121 if conn.DisconnectReceipt.Message.Command != "" {
122 t.Fatalf("TestConnCDDiscNoDiscReceipt Expected no disconnect receipt command, got [%v]\n",
123 conn.DisconnectReceipt.Message.Command)
125 // NO DISCONNECT checks here
126 _ = closeConn(t, n)
131 ConnDisc Test: stompngo.Disconnect with receipt requested.
133 func TestConnCDDiscStompDiscReceipt(t *testing.T) {
134 for _, sp := range Protocols() {
135 n, _ = openConn(t)
136 ch := login_headers
137 ch = headersProtocol(ch, sp)
138 conn, e = Connect(n, ch)
139 if e != nil {
140 t.Fatalf("TestConnCDDiscStompDiscReceipt Expected no connect error, got [%v]\n",
143 // DISCONNECT Here
144 checkReceived(t, conn, false)
145 e = conn.Disconnect(Headers{HK_RECEIPT, rid})
146 if e != nil {
148 t.Fatalf("TestConnCDDiscStompDiscReceipt Expected no disconnect error, got [%v]\n",
151 if conn.DisconnectReceipt.Error != nil {
152 t.Fatalf("TestConnCDDiscStompDiscReceipt Expected no receipt error, got [%v]\n",
153 conn.DisconnectReceipt.Error)
155 md := conn.DisconnectReceipt.Message
156 irid, ok := md.Headers.Contains(HK_RECEIPT_ID)
157 if !ok {
158 t.Fatalf("TestConnCDDiscStompDiscReceipt Expected receipt-id, not received\n")
160 if rid != irid {
161 t.Fatalf("TestConnCDDiscStompDiscReceipt Expected receipt-id [%q], got [%q]\n",
162 rid, irid)
164 // NO DISCONNECT checks here
165 _ = closeConn(t, n)
170 ConnDisc Test: Body Length of CONNECTED response.
172 func TestConnCDBodyLen(t *testing.T) {
173 for _, sp := range Protocols() {
174 n, _ = openConn(t)
175 ch := login_headers
176 ch = headersProtocol(ch, sp)
177 conn, e = Connect(n, ch)
178 if e != nil {
179 t.Fatalf("TestConnCDBodyLen Expected no connect error, got [%v]\n", e)
181 if len(conn.ConnectResponse.Body) != 0 {
182 t.Fatalf("TestConnCDBodyLen Expected body length 0, got [%v]\n",
183 len(conn.ConnectResponse.Body))
185 e = conn.Disconnect(empty_headers)
186 if e != nil {
187 t.Fatalf("TestConnCDBodyLen Expected no disconnect error, got [%v]\n", e)
189 _ = closeConn(t, n)
194 Conn11 Test: Test Protocol level
196 func TestConnCDProto(t *testing.T) {
197 for _, sp := range Protocols() {
198 n, _ = openConn(t)
199 ch := login_headers
200 ch = headersProtocol(ch, sp)
201 conn, e = Connect(n, ch)
202 if e != nil {
203 t.Fatalf("TestConnCDProto Expected no connect error, got [%v]\n", e)
205 if conn.Protocol() != sp {
206 t.Fatalf("TestConnCDProto Expected protocol %v, got [%v]\n", sp, conn.Protocol())
208 checkReceived(t, conn, false)
209 e = conn.Disconnect(empty_headers)
210 if e != nil {
211 t.Fatalf("TestConnCDProto Expected no disconnect error, got [%v]\n", e)
213 _ = closeConn(t, n)
218 ConnReceipt Test: Test receipt not allowed on connect.
220 func TestConnCDReceipt(t *testing.T) {
221 for _, sp := range Protocols() {
222 n, _ = openConn(t)
223 ch := login_headers
224 ch = headersProtocol(ch, sp)
225 ch = ch.Add(HK_RECEIPT, "abcd1234")
226 _, e = Connect(n, ch)
227 if e == nil {
228 t.Fatalf("TestConnCDReceipt Expected connect error, got nil\n")
230 if e != ENORECPT {
231 t.Fatalf("TestConnCDReceipt Expected [%v], got [%v]\n", ENORECPT, e)
233 // No DISCONNECT checks for this test.
234 _ = closeConn(t, n)
239 ConnDisc Test: ECONBAD
241 func TestConnCDEconBad(t *testing.T) {
242 for _, sp := range Protocols() {
243 n, _ = openConn(t)
244 ch := login_headers
245 ch = headersProtocol(ch, sp)
246 conn, e = Connect(n, ch)
247 if e != nil {
248 t.Fatalf("TestConnCDEconBad Expected no connect error, got [%v]\n", e)
250 checkReceived(t, conn, false)
251 e = conn.Disconnect(empty_headers)
252 if e != nil {
253 t.Fatalf("TestConnCDEconBad Expected no disconnect error, got [%v]\n", e)
255 _ = closeConn(t, n)
257 e = conn.Abort(empty_headers)
258 if e != ECONBAD {
259 t.Fatalf("TestConnCDEconBad Abort expected [%v] got [%v]\n", ECONBAD, e)
261 e = conn.Ack(empty_headers)
262 if e != ECONBAD {
263 t.Fatalf("TestConnCDEconBad Ack expected [%v] got [%v]\n", ECONBAD, e)
265 e = conn.Begin(empty_headers)
266 if e != ECONBAD {
267 t.Fatalf("TestConnCDEconBad Begin expected [%v] got [%v]\n", ECONBAD, e)
269 e = conn.Commit(empty_headers)
270 if e != ECONBAD {
271 t.Fatalf("TestConnCDEconBad Commit expected [%v] got [%v]\n", ECONBAD, e)
273 e = conn.Nack(empty_headers)
274 if e != ECONBAD {
275 t.Fatalf("TestConnCDEconBad Nack expected [%v] got [%v]\n", ECONBAD, e)
277 e = conn.Send(empty_headers, "")
278 if e != ECONBAD {
279 t.Fatalf("TestConnCDEconBad Send expected [%v] got [%v]\n", ECONBAD, e)
281 _, e = conn.Subscribe(empty_headers)
282 if e != ECONBAD {
283 t.Fatalf("TestConnCDEconBad Subscribe expected [%v] got [%v]\n", ECONBAD, e)
285 e = conn.Unsubscribe(empty_headers)
286 if e != ECONBAD {
287 t.Fatalf("TestConnCDEconBad Unsubscribe expected [%v] got [%v]\n", ECONBAD, e)
293 ConnDisc Test: ECONBAD
295 func TestConnCDEconDiscDone(t *testing.T) {
296 for _, sp := range Protocols() {
297 n, _ = openConn(t)
298 ch := login_headers
299 ch = headersProtocol(ch, sp)
300 conn, e = Connect(n, ch)
301 if e != nil {
302 t.Fatalf("TestConnCDEconDiscDone Expected no connect error, got [%v]\n", e)
304 e = conn.Disconnect(empty_headers)
305 if e != nil {
306 t.Fatalf("TestConnCDEconDiscDone Expected no disconnect error, got [%v]\n", e)
308 _ = closeConn(t, n)
310 e = conn.Disconnect(empty_headers)
311 if e != ECONBAD {
312 t.Fatalf("TestConnCDEconDiscDone Previous disconnect expected [%v] got [%v]\n", ECONBAD, e)
318 ConnDisc Test: setProtocolLevel
320 func TestConnCDCDSetProtocolLevel(t *testing.T) {
321 for _, sp := range Protocols() {
322 n, _ = openConn(t)
323 ch := login_headers
324 ch = headersProtocol(ch, sp)
325 conn, e = Connect(n, ch)
326 if e != nil {
327 t.Fatalf("TestConnCDCDSetProtocolLevel Expected no connect error, got [%v]\n", e)
328 } //
329 for i, v := range verChecks {
330 conn.protocol = SPL_10 // reset
331 e = conn.setProtocolLevel(v.ch, v.sh)
332 if e != v.e {
333 t.Fatalf("TestConnCDCDSetProtocolLevel Verdata Item [%d], expected [%v], got [%v]\n", i, v.e, e)
337 checkReceived(t, conn, false)
338 e = conn.Disconnect(empty_headers)
339 checkDisconnectError(t, e)
340 _ = closeConn(t, n)
345 ConnDisc Test: connRespData
347 func TestConnCDRespData(t *testing.T) {
348 for _, sp := range Protocols() {
349 n, _ = openConn(t)
350 ch := login_headers
351 ch = headersProtocol(ch, sp)
352 conn, e = Connect(n, ch)
353 if e != nil {
354 t.Fatalf("TestConnCDRespData Expected no connect error, got [%v]\n", e)
356 for i, f := range frames {
357 _, e = connectResponse(f.data)
358 if e != f.resp {
359 t.Fatalf("TestConnCDRespData Index [%v], expected [%v], got [%v]\n", i, f.resp, e)
362 checkReceived(t, conn, false)
363 e = conn.Disconnect(empty_headers)
364 checkDisconnectError(t, e)
365 _ = closeConn(t, n)