url_test.go 2.8 KB

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