4
0

url_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package urllib // import "miniflux.app/v2/internal/urllib"
  4. import "testing"
  5. func TestIsAbsoluteURL(t *testing.T) {
  6. scenarios := map[string]bool{
  7. "https://example.org/file.pdf": true,
  8. "magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7": true,
  9. "invalid url": false,
  10. }
  11. for input, expected := range scenarios {
  12. actual := IsAbsoluteURL(input)
  13. if actual != expected {
  14. t.Errorf(`Unexpected result, got %v instead of %v for %q`, actual, expected, input)
  15. }
  16. }
  17. }
  18. func TestAbsoluteURL(t *testing.T) {
  19. scenarios := [][]string{
  20. {"https://example.org/path/file.ext", "https://example.org/folder/", "/path/file.ext"},
  21. {"https://example.org/folder/path/file.ext", "https://example.org/folder/", "path/file.ext"},
  22. {"https://example.org/", "https://example.org/path", "./"},
  23. {"https://example.org/folder/", "https://example.org/folder/", "./"},
  24. {"https://example.org/path/file.ext", "https://example.org/folder", "path/file.ext"},
  25. {"https://example.org/path/file.ext", "https://example.org/folder/", "https://example.org/path/file.ext"},
  26. {"https://static.example.org/path/file.ext", "https://www.example.org/", "//static.example.org/path/file.ext"},
  27. {"magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a", "https://www.example.org/", "magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a"},
  28. {"magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7", "https://www.example.org/", "magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7"},
  29. }
  30. for _, scenario := range scenarios {
  31. actual, err := AbsoluteURL(scenario[1], scenario[2])
  32. if err != nil {
  33. t.Errorf(`Got error for (%q, %q): %v`, scenario[1], scenario[2], err)
  34. }
  35. if actual != scenario[0] {
  36. t.Errorf(`Unexpected result, got %q instead of %q for (%q, %q)`, actual, scenario[0], scenario[1], scenario[2])
  37. }
  38. }
  39. }
  40. func TestRootURL(t *testing.T) {
  41. scenarios := map[string]string{
  42. "https://example.org/path/file.ext": "https://example.org/",
  43. "//static.example.org/path/file.ext": "https://static.example.org/",
  44. "https://example|org/path/file.ext": "https://example|org/path/file.ext",
  45. }
  46. for input, expected := range scenarios {
  47. actual := RootURL(input)
  48. if actual != expected {
  49. t.Errorf(`Unexpected result, got %q instead of %q`, actual, expected)
  50. }
  51. }
  52. }
  53. func TestIsHTTPS(t *testing.T) {
  54. scenarios := map[string]bool{
  55. "https://example.org/": true,
  56. "http://example.org/": false,
  57. "https://example|org/": false,
  58. }
  59. for input, expected := range scenarios {
  60. actual := IsHTTPS(input)
  61. if actual != expected {
  62. t.Errorf(`Unexpected result, got %v instead of %v`, actual, expected)
  63. }
  64. }
  65. }
  66. func TestDomain(t *testing.T) {
  67. scenarios := map[string]string{
  68. "https://static.example.org/": "static.example.org",
  69. "https://example|org/": "https://example|org/",
  70. }
  71. for input, expected := range scenarios {
  72. actual := Domain(input)
  73. if actual != expected {
  74. t.Errorf(`Unexpected result, got %q instead of %q`, actual, expected)
  75. }
  76. }
  77. }
  78. func TestJoinBaseURLAndPath(t *testing.T) {
  79. type args struct {
  80. baseURL string
  81. path string
  82. }
  83. tests := []struct {
  84. name string
  85. args args
  86. want string
  87. wantErr bool
  88. }{
  89. {"empty base url", args{"", "/api/bookmarks/"}, "", true},
  90. {"empty path", args{"https://example.com", ""}, "", true},
  91. {"invalid base url", args{"incorrect url", ""}, "", true},
  92. {"valid", args{"https://example.com", "/api/bookmarks/"}, "https://example.com/api/bookmarks/", false},
  93. {"valid", args{"https://example.com/subfolder", "/api/bookmarks/"}, "https://example.com/subfolder/api/bookmarks/", false},
  94. }
  95. for _, tt := range tests {
  96. t.Run(tt.name, func(t *testing.T) {
  97. got, err := JoinBaseURLAndPath(tt.args.baseURL, tt.args.path)
  98. if (err != nil) != tt.wantErr {
  99. t.Errorf("JoinBaseURLAndPath error = %v, wantErr %v", err, tt.wantErr)
  100. return
  101. }
  102. if got != tt.want {
  103. t.Errorf("JoinBaseURLAndPath = %v, want %v", got, tt.want)
  104. }
  105. })
  106. }
  107. }