github.com/bloxroute-labs/bor@v0.1.4/rpc/websocket_test.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package rpc
    18  
    19  import "testing"
    20  
    21  func TestWSGetConfigNoAuth(t *testing.T) {
    22  	config, err := wsGetConfig("ws://example.com:1234", "")
    23  	if err != nil {
    24  		t.Logf("wsGetConfig failed: %s", err)
    25  		t.Fail()
    26  		return
    27  	}
    28  	if config.Location.User != nil {
    29  		t.Log("User should have been stripped from the URL")
    30  		t.Fail()
    31  	}
    32  	if config.Location.Hostname() != "example.com" ||
    33  		config.Location.Port() != "1234" || config.Location.Scheme != "ws" {
    34  		t.Logf("Unexpected URL: %s", config.Location)
    35  		t.Fail()
    36  	}
    37  }
    38  
    39  func TestWSGetConfigWithBasicAuth(t *testing.T) {
    40  	config, err := wsGetConfig("wss://testuser:test-PASS_01@example.com:1234", "")
    41  	if err != nil {
    42  		t.Logf("wsGetConfig failed: %s", err)
    43  		t.Fail()
    44  		return
    45  	}
    46  	if config.Location.User != nil {
    47  		t.Log("User should have been stripped from the URL")
    48  		t.Fail()
    49  	}
    50  	if config.Header.Get("Authorization") != "Basic dGVzdHVzZXI6dGVzdC1QQVNTXzAx" {
    51  		t.Log("Basic auth header is incorrect")
    52  		t.Fail()
    53  	}
    54  }