github.com/theQRL/go-zond@v0.1.1/zondstats/zondstats_test.go (about)

     1  // Copyright 2021 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 zondstats
    18  
    19  import (
    20  	"strconv"
    21  	"testing"
    22  )
    23  
    24  func TestParseEthstatsURL(t *testing.T) {
    25  	cases := []struct {
    26  		url              string
    27  		node, pass, host string
    28  	}{
    29  		{
    30  			url:  `"debug meowsbits":mypass@ws://mordor.dash.fault.dev:3000`,
    31  			node: "debug meowsbits", pass: "mypass", host: "ws://mordor.dash.fault.dev:3000",
    32  		},
    33  		{
    34  			url:  `"debug @meowsbits":mypass@ws://mordor.dash.fault.dev:3000`,
    35  			node: "debug @meowsbits", pass: "mypass", host: "ws://mordor.dash.fault.dev:3000",
    36  		},
    37  		{
    38  			url:  `"debug: @meowsbits":mypass@ws://mordor.dash.fault.dev:3000`,
    39  			node: "debug: @meowsbits", pass: "mypass", host: "ws://mordor.dash.fault.dev:3000",
    40  		},
    41  		{
    42  			url:  `name:@ws://mordor.dash.fault.dev:3000`,
    43  			node: "name", pass: "", host: "ws://mordor.dash.fault.dev:3000",
    44  		},
    45  		{
    46  			url:  `name@ws://mordor.dash.fault.dev:3000`,
    47  			node: "name", pass: "", host: "ws://mordor.dash.fault.dev:3000",
    48  		},
    49  		{
    50  			url:  `:mypass@ws://mordor.dash.fault.dev:3000`,
    51  			node: "", pass: "mypass", host: "ws://mordor.dash.fault.dev:3000",
    52  		},
    53  		{
    54  			url:  `:@ws://mordor.dash.fault.dev:3000`,
    55  			node: "", pass: "", host: "ws://mordor.dash.fault.dev:3000",
    56  		},
    57  	}
    58  
    59  	for i, c := range cases {
    60  		parts, err := parseEthstatsURL(c.url)
    61  		if err != nil {
    62  			t.Fatal(err)
    63  		}
    64  		node, pass, host := parts[0], parts[1], parts[2]
    65  
    66  		// unquote because the value provided will be used as a CLI flag value, so unescaped quotes will be removed
    67  		nodeUnquote, err := strconv.Unquote(node)
    68  		if err == nil {
    69  			node = nodeUnquote
    70  		}
    71  
    72  		if node != c.node {
    73  			t.Errorf("case=%d mismatch node value, got: %v ,want: %v", i, node, c.node)
    74  		}
    75  		if pass != c.pass {
    76  			t.Errorf("case=%d mismatch pass value, got: %v ,want: %v", i, pass, c.pass)
    77  		}
    78  		if host != c.host {
    79  			t.Errorf("case=%d mismatch host value, got: %v ,want: %v", i, host, c.host)
    80  		}
    81  	}
    82  }