url_test.go 2.8 KB

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