github.com/core-coin/go-core/v2@v2.1.9/accounts/url_test.go (about)

     1  // Copyright 2018 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core 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://coreblockchain.cc")
    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 != "coreblockchain.cc" {
    32  		t.Errorf("expected: %v, got: %v", "coreblockchain.cc", url.Path)
    33  	}
    34  
    35  	_, err = parseURL("coreblockchain.cc")
    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: "coreblockchain.cc"}
    43  	if url.String() != "https://coreblockchain.cc" {
    44  		t.Errorf("expected: %v, got: %v", "https://coreblockchain.cc", url.String())
    45  	}
    46  
    47  	url = URL{Scheme: "", Path: "coreblockchain.cc"}
    48  	if url.String() != "coreblockchain.cc" {
    49  		t.Errorf("expected: %v, got: %v", "coreblockchain.cc", url.String())
    50  	}
    51  }
    52  
    53  func TestURLMarshalJSON(t *testing.T) {
    54  	url := URL{Scheme: "https", Path: "coreblockchain.cc"}
    55  	json, err := url.MarshalJSON()
    56  	if err != nil {
    57  		t.Errorf("unexpcted error: %v", err)
    58  	}
    59  	if string(json) != "\"https://coreblockchain.cc\"" {
    60  		t.Errorf("expected: %v, got: %v", "\"https://coreblockchain.cc\"", string(json))
    61  	}
    62  }
    63  
    64  func TestURLUnmarshalJSON(t *testing.T) {
    65  	url := &URL{}
    66  	err := url.UnmarshalJSON([]byte("\"https://coreblockchain.cc\""))
    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 != "coreblockchain.cc" {
    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", "coreblockchain.cc"}, URL{"https", "coreblockchain.cc"}, 0},
    85  		{URL{"http", "coreblockchain.cc"}, URL{"https", "coreblockchain.cc"}, -1},
    86  		{URL{"https", "coreblockchain.cc/a"}, URL{"https", "coreblockchain.cc"}, 1},
    87  		{URL{"https", "abc.org"}, URL{"https", "coreblockchain.cc"}, -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  }