MCPcopy
hub / github.com/nats-io/nats.go / TestServerSecureConnections

Function TestServerSecureConnections

test/conn_test.go:136–237  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

134}
135
136func TestServerSecureConnections(t *testing.T) {
137 s, opts := RunServerWithConfig("./configs/tls.conf")
138 defer s.Shutdown()
139
140 endpoint := fmt.Sprintf("%s:%d", opts.Host, opts.Port)
141 secureURL := fmt.Sprintf("nats://%s:%s@%s/", opts.Username, opts.Password, endpoint)
142
143 // Make sure this succeeds
144 nc, err := nats.Connect(secureURL, nats.Secure(), nats.RootCAs("./configs/certs/ca.pem"))
145 if err != nil {
146 t.Fatalf("Failed to create secure (TLS) connection: %v", err)
147 }
148 defer nc.Close()
149
150 omsg := []byte("Hello World")
151 checkRecv := make(chan bool)
152
153 received := 0
154 nc.Subscribe("foo", func(m *nats.Msg) {
155 received++
156 if !bytes.Equal(m.Data, omsg) {
157 t.Fatal("Message received does not match")
158 }
159 checkRecv <- true
160 })
161 err = nc.Publish("foo", omsg)
162 if err != nil {
163 t.Fatalf("Failed to publish on secure (TLS) connection: %v", err)
164 }
165 nc.Flush()
166
167 state, err := nc.TLSConnectionState()
168 if err != nil {
169 t.Fatalf("Expected connection state: %v", err)
170 }
171 if !state.HandshakeComplete {
172 t.Fatalf("Expected valid connection state")
173 }
174
175 if err := Wait(checkRecv); err != nil {
176 t.Fatal("Failed receiving message")
177 }
178
179 nc.Close()
180
181 // Server required, but not specified in Connect(), should switch automatically
182 nc, err = nats.Connect(secureURL, nats.RootCAs("./configs/certs/ca.pem"))
183 if err != nil {
184 t.Fatalf("Failed to create secure (TLS) connection: %v", err)
185 }
186 nc.Close()
187
188 // Test flag mismatch
189 // Wanted but not available..
190 ds := RunDefaultServer()
191 defer ds.Shutdown()
192
193 nc, err = nats.Connect(nats.DefaultURL, nats.Secure(), nats.RootCAs("./configs/certs/ca.pem"))

Callers

nothing calls this directly

Calls 11

ConnectMethod · 0.80
FatalfMethod · 0.80
EqualMethod · 0.80
TLSConnectionStateMethod · 0.80
RunServerWithConfigFunction · 0.70
WaitFunction · 0.70
RunDefaultServerFunction · 0.70
SubscribeMethod · 0.65
PublishMethod · 0.65
CloseMethod · 0.45
FlushMethod · 0.45

Tested by

no test coverage detected