github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/accounts/url_test.go (about)

     1  // Copyright 2018 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 accounts
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func TestURLParsing(t *testing.T) {
    24  	t.Parallel()
    25  	url, err := parseURL("https://ethereum.org")
    26  	if err != nil {
    27  		t.Errorf("unexpected error: %v", err)
    28  	}
    29  	if url.Scheme != "https" {
    30  		t.Errorf("expected: %v, got: %v", "https", url.Scheme)
    31  	}
    32  	if url.Path != "ethereum.org" {
    33  		t.Errorf("expected: %v, got: %v", "ethereum.org", url.Path)
    34  	}
    35  
    36  	for _, u := range []string{"ethereum.org", ""} {
    37  		if _, err = parseURL(u); err == nil {
    38  			t.Errorf("input %v, expected err, got: nil", u)
    39  		}
    40  	}
    41  }
    42  
    43  func TestURLString(t *testing.T) {
    44  	t.Parallel()
    45  	url := URL{Scheme: "https", Path: "ethereum.org"}
    46  	if url.String() != "https://ethereum.org" {
    47  		t.Errorf("expected: %v, got: %v", "https://ethereum.org", url.String())
    48  	}
    49  
    50  	url = URL{Scheme: "", Path: "ethereum.org"}
    51  	if url.String() != "ethereum.org" {
    52  		t.Errorf("expected: %v, got: %v", "ethereum.org", url.String())
    53  	}
    54  }
    55  
    56  func TestURLMarshalJSON(t *testing.T) {
    57  	t.Parallel()
    58  	url := URL{Scheme: "https", Path: "ethereum.org"}
    59  	json, err := url.MarshalJSON()
    60  	if err != nil {
    61  		t.Errorf("unexpected error: %v", err)
    62  	}
    63  	if string(json) != "\"https://ethereum.org\"" {
    64  		t.Errorf("expected: %v, got: %v", "\"https://ethereum.org\"", string(json))
    65  	}
    66  }
    67  
    68  func TestURLUnmarshalJSON(t *testing.T) {
    69  	t.Parallel()
    70  	url := &URL{}
    71  	err := url.UnmarshalJSON([]byte("\"https://ethereum.org\""))
    72  	if err != nil {
    73  		t.Errorf("unexpected error: %v", err)
    74  	}
    75  	if url.Scheme != "https" {
    76  		t.Errorf("expected: %v, got: %v", "https", url.Scheme)
    77  	}
    78  	if url.Path != "ethereum.org" {
    79  		t.Errorf("expected: %v, got: %v", "https", url.Path)
    80  	}
    81  }
    82  
    83  func TestURLComparison(t *testing.T) {
    84  	t.Parallel()
    85  	tests := []struct {
    86  		urlA   URL
    87  		urlB   URL
    88  		expect int
    89  	}{
    90  		{URL{"https", "ethereum.org"}, URL{"https", "ethereum.org"}, 0},
    91  		{URL{"http", "ethereum.org"}, URL{"https", "ethereum.org"}, -1},
    92  		{URL{"https", "ethereum.org/a"}, URL{"https", "ethereum.org"}, 1},
    93  		{URL{"https", "abc.org"}, URL{"https", "ethereum.org"}, -1},
    94  	}
    95  
    96  	for i, tt := range tests {
    97  		result := tt.urlA.Cmp(tt.urlB)
    98  		if result != tt.expect {
    99  			t.Errorf("test %d: cmp mismatch: expected: %d, got: %d", i, tt.expect, result)
   100  		}
   101  	}
   102  }