github.com/ebceco/ebc@v1.8.19-0.20190309150932-8cb0b9e06484/accounts/url_test.go (about)

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