Skip to content

Commit

Permalink
Merge pull request #915 from nats-io/fix_ws_invalid_conn
Browse files Browse the repository at this point in the history
[FIXED] Websocket: discovered urls would not have "wss://" scheme
  • Loading branch information
kozlovic committed Mar 8, 2022
2 parents 9f0049d + 97d7532 commit 2f2f696
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion nats.go
Expand Up @@ -1435,7 +1435,7 @@ func (nc *Conn) setupServerPool() error {

// Check for Scheme hint to move to TLS mode.
for _, srv := range nc.srvPool {
if srv.url.Scheme == tlsScheme {
if srv.url.Scheme == tlsScheme || srv.url.Scheme == wsSchemeTLS {
// FIXME(dlc), this is for all in the pool, should be case by case.
nc.Opts.Secure = true
if nc.Opts.TLSConfig == nil {
Expand Down
30 changes: 30 additions & 0 deletions ws_test.go
Expand Up @@ -860,6 +860,36 @@ func TestWSWithTLS(t *testing.T) {
}
}

func TestWSTlsNoConfig(t *testing.T) {
opts := GetDefaultOptions()
opts.Servers = []string{"wss://localhost:443"}

nc := &Conn{Opts: opts}
if err := nc.setupServerPool(); err != nil {
t.Fatalf("Error setting up pool: %v", err)
}
// Verify that this has set Secure/TLSConfig
nc.mu.Lock()
ok := nc.Opts.Secure && nc.Opts.TLSConfig != nil
nc.mu.Unlock()
if !ok {
t.Fatal("Secure and TLSConfig were not set")
}
// Now try to add a bare host:ip to the pool and verify
// that the wss:// scheme is added.
if err := nc.addURLToPool("1.2.3.4:443", true, false); err != nil {
t.Fatalf("Error adding to pool: %v", err)
}
nc.mu.Lock()
for _, srv := range nc.srvPool {
if srv.url.Scheme != wsSchemeTLS {
nc.mu.Unlock()
t.Fatalf("Expected scheme to be %q, got url: %s", wsSchemeTLS, srv.url)
}
}
nc.mu.Unlock()
}

func TestWSGossipAndReconnect(t *testing.T) {
o1 := testWSGetDefaultOptions(t, false)
o1.ServerName = "A"
Expand Down

0 comments on commit 2f2f696

Please sign in to comment.