ethstats_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2021 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 ethstats
  17. import (
  18. "strconv"
  19. "testing"
  20. )
  21. func TestParseEthstatsURL(t *testing.T) {
  22. cases := []struct {
  23. url string
  24. node, pass, host string
  25. }{
  26. {
  27. url: `"debug meowsbits":mypass@ws://mordor.dash.fault.dev:3000`,
  28. node: "debug meowsbits", pass: "mypass", host: "ws://mordor.dash.fault.dev:3000",
  29. },
  30. {
  31. url: `"debug @meowsbits":mypass@ws://mordor.dash.fault.dev:3000`,
  32. node: "debug @meowsbits", pass: "mypass", host: "ws://mordor.dash.fault.dev:3000",
  33. },
  34. {
  35. url: `"debug: @meowsbits":mypass@ws://mordor.dash.fault.dev:3000`,
  36. node: "debug: @meowsbits", pass: "mypass", host: "ws://mordor.dash.fault.dev:3000",
  37. },
  38. {
  39. url: `name:@ws://mordor.dash.fault.dev:3000`,
  40. node: "name", pass: "", host: "ws://mordor.dash.fault.dev:3000",
  41. },
  42. {
  43. url: `name@ws://mordor.dash.fault.dev:3000`,
  44. node: "name", pass: "", host: "ws://mordor.dash.fault.dev:3000",
  45. },
  46. {
  47. url: `:mypass@ws://mordor.dash.fault.dev:3000`,
  48. node: "", pass: "mypass", host: "ws://mordor.dash.fault.dev:3000",
  49. },
  50. {
  51. url: `:@ws://mordor.dash.fault.dev:3000`,
  52. node: "", pass: "", host: "ws://mordor.dash.fault.dev:3000",
  53. },
  54. }
  55. for i, c := range cases {
  56. parts, err := parseEthstatsURL(c.url)
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. node, pass, host := parts[0], parts[1], parts[2]
  61. // unquote because the value provided will be used as a CLI flag value, so unescaped quotes will be removed
  62. nodeUnquote, err := strconv.Unquote(node)
  63. if err == nil {
  64. node = nodeUnquote
  65. }
  66. if node != c.node {
  67. t.Errorf("case=%d mismatch node value, got: %v ,want: %v", i, node, c.node)
  68. }
  69. if pass != c.pass {
  70. t.Errorf("case=%d mismatch pass value, got: %v ,want: %v", i, pass, c.pass)
  71. }
  72. if host != c.host {
  73. t.Errorf("case=%d mismatch host value, got: %v ,want: %v", i, host, c.host)
  74. }
  75. }
  76. }