Address issue #50.
[stompngo.git] / sub_test.go
blobad894353917397f5ba416487285c204afbf9e907
1 //
2 // Copyright © 2011-2019 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 "fmt"
21 "log"
22 //"os"
23 "testing"
24 //"time"
27 func TestSubNoHeader(t *testing.T) {
28 n, _ = openConn(t)
29 ch := login_headers
30 ch = headersProtocol(ch, SPL_10) // Start with 1.0
31 conn, e = Connect(n, ch)
32 if e != nil {
33 t.Fatalf("TestSubNoHeader CONNECT Failed: e:<%q> connresponse:<%q>\n", e,
34 conn.ConnectResponse)
37 for ti, tv := range subNoHeaderDataList {
38 conn.protocol = tv.proto // Cheat, fake all protocols
39 _, e = conn.Subscribe(empty_headers)
40 if e == nil {
41 t.Fatalf("TestSubNoHeader[%d] proto:%s expected:%v got:nil\n",
42 ti, tv.proto, tv.exe)
44 if e != tv.exe {
45 t.Fatalf("TestSubNoHeader[%d] proto:%s expected:%v got:%v\n",
46 ti, tv.proto, tv.exe, e)
50 e = conn.Disconnect(empty_headers)
51 checkDisconnectError(t, e)
52 _ = closeConn(t, n)
53 log.Printf("TestSubNoHeader %d tests complete.\n", len(subNoHeaderDataList))
56 func TestSubNoID(t *testing.T) {
57 n, _ = openConn(t)
58 ch := login_headers
59 ch = headersProtocol(ch, SPL_10) // Start with 1.0
60 conn, e = Connect(n, ch)
61 if e != nil {
62 t.Fatalf("TestSubNoID CONNECT Failed: e:<%q> connresponse:<%q>\n", e,
63 conn.ConnectResponse)
66 for ti, tv := range subNoIDDataList {
67 conn.protocol = tv.proto // Cheat, fake all protocols
68 ud := tdest(tv.subh.Value(HK_DESTINATION))
69 _, e = conn.Subscribe(Headers{HK_DESTINATION, ud})
70 if e != tv.exe {
71 t.Fatalf("TestSubNoID[%d] proto:%s expected:%v got:%v\n",
72 ti, tv.proto, tv.exe, e)
76 e = conn.Disconnect(empty_headers)
77 checkDisconnectError(t, e)
78 _ = closeConn(t, n)
79 log.Printf("TestSubNoID %d tests complete.\n", len(subNoIDDataList))
82 func TestSubPlain(t *testing.T) {
83 for ti, tv := range subPlainDataList {
84 n, _ = openConn(t)
85 ch := login_headers
86 ch = headersProtocol(ch, tv.proto)
87 conn, e = Connect(n, ch)
88 if e != nil {
89 t.Fatalf("TestSubPlain CONNECT Failed: e:<%q> connresponse:<%q>\n", e,
90 conn.ConnectResponse)
93 // SUBSCRIBE Phase
94 sh := fixHeaderDest(tv.subh) // destination fixed if needed
95 sc, e = conn.Subscribe(sh)
96 if sc == nil {
97 t.Fatalf("TestSubPlain[%d] SUBSCRIBE, proto:[%s], channel is nil\n",
98 ti, tv.proto)
100 if e != tv.exe1 {
101 t.Fatalf("TestSubPlain[%d] SUBSCRIBE, proto:%s expected:%v got:%v\n",
102 ti, tv.proto, tv.exe1, e)
105 // UNSUBSCRIBE Phase
106 sh = fixHeaderDest(tv.unsubh) // destination fixed if needed
107 e = conn.Unsubscribe(sh)
108 if e != tv.exe2 {
109 t.Fatalf("TestSubPlain[%d] UNSUBSCRIBE, proto:%s expected:%v got:%v\n",
110 ti, tv.proto, tv.exe2, e)
113 e = conn.Disconnect(empty_headers)
114 checkDisconnectError(t, e)
115 _ = closeConn(t, n)
117 log.Printf("TestSubPlain %d tests complete.\n", len(subPlainDataList))
120 func TestSubNoTwice(t *testing.T) {
121 for ti, tv := range subTwiceDataList {
122 n, _ = openConn(t)
123 ch := login_headers
124 ch = headersProtocol(ch, tv.proto)
125 conn, e = Connect(n, ch)
126 if e != nil {
127 t.Fatalf("TestSubNoTwice CONNECT Failed: e:<%q> connresponse:<%q>\n",
129 conn.ConnectResponse)
132 // SUBSCRIBE Phase 1
133 sh := fixHeaderDest(tv.subh) // destination fixed if needed
134 sc, e = conn.Subscribe(sh)
135 if sc == nil {
136 t.Fatalf("TestSubNoTwice[%d] SUBSCRIBE1, proto:[%s], channel is nil\n",
137 ti, tv.proto)
139 if e != tv.exe1 {
140 t.Fatalf("TestSubNoTwice[%d] SUBSCRIBE1, proto:%s expected:%v got:%v\n",
141 ti, tv.proto, tv.exe1, e)
144 // SUBSCRIBE Phase 2
145 sc, e = conn.Subscribe(sh)
146 if e != tv.exe2 {
147 t.Fatalf("TestSubNoTwice[%d] SUBSCRIBE2, proto:%s expected:%v got:%v\n",
148 ti, tv.proto, tv.exe2, e)
151 e = conn.Disconnect(empty_headers)
152 checkDisconnectError(t, e)
153 _ = closeConn(t, n)
155 log.Printf("TestSubNoTwice %d tests complete.\n", len(subTwiceDataList))
158 func TestSubRoundTrip(t *testing.T) {
159 for ti, tv := range subPlainDataList { // *NOTE* Use the PlainData table
160 n, _ = openConn(t)
161 ch := login_headers
162 ch = headersProtocol(ch, tv.proto)
163 conn, e = Connect(n, ch)
164 if e != nil {
165 t.Fatalf("TestSubRoundTrip CONNECT Failed: e:<%q> connresponse:<%q>\n",
167 conn.ConnectResponse)
169 sh := fixHeaderDest(tv.subh) // destination fixed if needed
171 // SEND Phase
172 msg := "SUBROUNDTRIP: " + tv.proto
173 nh := Headers{HK_DESTINATION, sh.Value(HK_DESTINATION)}
174 e = conn.Send(nh, msg)
175 if e != nil {
176 t.Fatalf("TestSubRoundTrip[%d] SEND, proto:%s expected:%v got:%v\n",
177 ti, tv.proto, nil, e)
180 // SUBSCRIBE Phase
181 sc, e = conn.Subscribe(sh)
182 if sc == nil {
183 t.Fatalf("TestSubRoundTrip[%d] SUBSCRIBE, proto:[%s], channel is nil\n",
184 ti, tv.proto)
186 if e != tv.exe1 {
187 t.Fatalf("TestSubRoundTrip[%d] SUBSCRIBE, proto:%s expected:%v got:%v\n",
188 ti, tv.proto, tv.exe1, e)
191 // RECEIVE Phase
192 id := fmt.Sprintf("TestSubRoundTrip[%d] RECEIVE, proto:%s", ti, tv.proto)
193 checkReceivedMD(t, conn, sc, id)
194 if msg != md.Message.BodyString() {
195 t.Fatalf("TestSubRoundTrip[%d] RECEIVE, proto:%s expected:%v got:%v\n",
196 ti, tv.proto, msg, md.Message.BodyString())
199 // UNSUBSCRIBE Phase
200 e = conn.Unsubscribe(sh)
201 if e != tv.exe2 {
202 t.Fatalf("TestSubRoundTrip[%d] UNSUBSCRIBE, proto:%s expected:%v got:%v\n",
203 ti, tv.proto, tv.exe2, e)
206 e = conn.Disconnect(empty_headers)
207 checkDisconnectError(t, e)
208 _ = closeConn(t, n)
210 log.Printf("TestSubRoundTrip %d tests complete.\n", len(subPlainDataList))
213 func TestSubAckModes(t *testing.T) {
214 for ti, tv := range subAckDataList {
215 n, _ = openConn(t)
216 ch := login_headers
217 ch = headersProtocol(ch, tv.proto)
218 conn, e = Connect(n, ch)
219 if e != nil {
220 t.Fatalf("TestSubAckModes CONNECT Failed: e:<%q> connresponse:<%q>\n",
222 conn.ConnectResponse)
225 // SUBSCRIBE Phase 1
226 sh := fixHeaderDest(tv.subh) // destination fixed if needed
227 sc, e = conn.Subscribe(sh)
228 if e == nil {
229 if sc == nil {
230 t.Fatalf("TestSubAckModes[%d] SUBSCRIBE, proto:[%s], channel is nil\n",
231 ti, tv.proto)
234 if e != tv.exe {
235 t.Fatalf("TestSubAckModes[%d] SUBSCRIBE, proto:%s expected:%v got:%v\n",
236 ti, tv.proto, tv.exe, e)
239 e = conn.Disconnect(empty_headers)
240 checkDisconnectError(t, e)
241 _ = closeConn(t, n)
243 log.Printf("TestSubAckModes %d tests complete.\n", len(subAckDataList))