uri_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. package api
  17. import (
  18. "reflect"
  19. "testing"
  20. )
  21. func TestParseURI(t *testing.T) {
  22. type test struct {
  23. uri string
  24. expectURI *URI
  25. expectErr bool
  26. expectRaw bool
  27. expectImmutable bool
  28. }
  29. tests := []test{
  30. {
  31. uri: "",
  32. expectErr: true,
  33. },
  34. {
  35. uri: "foo",
  36. expectErr: true,
  37. },
  38. {
  39. uri: "bzz",
  40. expectErr: true,
  41. },
  42. {
  43. uri: "bzz:",
  44. expectURI: &URI{Scheme: "bzz"},
  45. },
  46. {
  47. uri: "bzzi:",
  48. expectURI: &URI{Scheme: "bzzi"},
  49. expectImmutable: true,
  50. },
  51. {
  52. uri: "bzzr:",
  53. expectURI: &URI{Scheme: "bzzr"},
  54. expectRaw: true,
  55. },
  56. {
  57. uri: "bzz:/",
  58. expectURI: &URI{Scheme: "bzz"},
  59. },
  60. {
  61. uri: "bzz:/abc123",
  62. expectURI: &URI{Scheme: "bzz", Addr: "abc123"},
  63. },
  64. {
  65. uri: "bzz:/abc123/path/to/entry",
  66. expectURI: &URI{Scheme: "bzz", Addr: "abc123", Path: "path/to/entry"},
  67. },
  68. {
  69. uri: "bzzr:/",
  70. expectURI: &URI{Scheme: "bzzr"},
  71. expectRaw: true,
  72. },
  73. {
  74. uri: "bzzr:/abc123",
  75. expectURI: &URI{Scheme: "bzzr", Addr: "abc123"},
  76. expectRaw: true,
  77. },
  78. {
  79. uri: "bzzr:/abc123/path/to/entry",
  80. expectURI: &URI{Scheme: "bzzr", Addr: "abc123", Path: "path/to/entry"},
  81. expectRaw: true,
  82. },
  83. {
  84. uri: "bzz://",
  85. expectURI: &URI{Scheme: "bzz"},
  86. },
  87. {
  88. uri: "bzz://abc123",
  89. expectURI: &URI{Scheme: "bzz", Addr: "abc123"},
  90. },
  91. {
  92. uri: "bzz://abc123/path/to/entry",
  93. expectURI: &URI{Scheme: "bzz", Addr: "abc123", Path: "path/to/entry"},
  94. },
  95. }
  96. for _, x := range tests {
  97. actual, err := Parse(x.uri)
  98. if x.expectErr {
  99. if err == nil {
  100. t.Fatalf("expected %s to error", x.uri)
  101. }
  102. continue
  103. }
  104. if err != nil {
  105. t.Fatalf("error parsing %s: %s", x.uri, err)
  106. }
  107. if !reflect.DeepEqual(actual, x.expectURI) {
  108. t.Fatalf("expected %s to return %#v, got %#v", x.uri, x.expectURI, actual)
  109. }
  110. if actual.Raw() != x.expectRaw {
  111. t.Fatalf("expected %s raw to be %t, got %t", x.uri, x.expectRaw, actual.Raw())
  112. }
  113. if actual.Immutable() != x.expectImmutable {
  114. t.Fatalf("expected %s immutable to be %t, got %t", x.uri, x.expectImmutable, actual.Immutable())
  115. }
  116. }
  117. }