github.com/klaytn/klaytn@v1.12.1/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://klaytn.com") 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 != "klaytn.com" { 32 t.Errorf("expected: %v, got: %v", "klaytn.com", url.Path) 33 } 34 35 _, err = parseURL("klaytn.com") 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: "klaytn.com"} 43 if url.String() != "https://klaytn.com" { 44 t.Errorf("expected: %v, got: %v", "https://klaytn.com", url.String()) 45 } 46 47 url = URL{Scheme: "", Path: "klaytn.com"} 48 if url.String() != "klaytn.com" { 49 t.Errorf("expected: %v, got: %v", "klaytn.com", url.String()) 50 } 51 } 52 53 func TestURLMarshalJSON(t *testing.T) { 54 url := URL{Scheme: "https", Path: "klaytn.com"} 55 json, err := url.MarshalJSON() 56 if err != nil { 57 t.Errorf("unexpcted error: %v", err) 58 } 59 if string(json) != "\"https://klaytn.com\"" { 60 t.Errorf("expected: %v, got: %v", "\"https://klaytn.com\"", string(json)) 61 } 62 } 63 64 func TestURLUnmarshalJSON(t *testing.T) { 65 url := &URL{} 66 err := url.UnmarshalJSON([]byte("\"https://klaytn.com\"")) 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 != "klaytn.com" { 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", "klaytn.com"}, URL{"https", "klaytn.com"}, 0}, 85 {URL{"http", "klaytn.com"}, URL{"https", "klaytn.com"}, -1}, 86 {URL{"https", "klaytn.com/a"}, URL{"https", "klaytn.com"}, 1}, 87 {URL{"https", "abc.org"}, URL{"https", "klaytn.com"}, -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 }