url_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 TestIsRelativePath(t *testing.T) {
  6. scenarios := map[string]bool{
  7. // Valid relative paths
  8. "path/to/file.ext": true,
  9. "./path/to/file.ext": true,
  10. "../path/to/file.ext": true,
  11. "file.ext": true,
  12. "./file.ext": true,
  13. "../file.ext": true,
  14. "/absolute/path": true,
  15. "path?query=value": true,
  16. "path#fragment": true,
  17. "path?query#fragment": true,
  18. // Not relative paths
  19. "https://example.org/file.ext": false,
  20. "http://example.org/file.ext": false,
  21. "//example.org/file.ext": false,
  22. "//example.org": false,
  23. "ftp://example.org/file.ext": false,
  24. "mailto:user@example.org": false,
  25. "magnet:?xt=urn:btih:example": false,
  26. "": false,
  27. "magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C": false,
  28. }
  29. for input, expected := range scenarios {
  30. actual := IsRelativePath(input)
  31. if actual != expected {
  32. t.Errorf(`Unexpected result for IsRelativePath, got %v instead of %v for %q`, actual, expected, input)
  33. }
  34. }
  35. }
  36. func TestIsAbsoluteURL(t *testing.T) {
  37. scenarios := map[string]bool{
  38. "https://example.org/file.pdf": true,
  39. "magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7": true,
  40. "invalid url": false,
  41. }
  42. for input, expected := range scenarios {
  43. actual := IsAbsoluteURL(input)
  44. if actual != expected {
  45. t.Errorf(`Unexpected result, got %v instead of %v for %q`, actual, expected, input)
  46. }
  47. }
  48. }
  49. func TestAbsoluteURL(t *testing.T) {
  50. scenarios := [][]string{
  51. {"https://example.org/path/file.ext", "https://example.org/folder/", "/path/file.ext"},
  52. {"https://example.org/folder/path/file.ext", "https://example.org/folder/", "path/file.ext"},
  53. {"https://example.org/", "https://example.org/path", "./"},
  54. {"https://example.org/folder/", "https://example.org/folder/", "./"},
  55. {"https://example.org/path/file.ext", "https://example.org/folder", "path/file.ext"},
  56. {"https://example.org/path/file.ext", "https://example.org/folder/", "https://example.org/path/file.ext"},
  57. {"https://static.example.org/path/file.ext", "https://www.example.org/", "//static.example.org/path/file.ext"},
  58. {"magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a", "https://www.example.org/", "magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a"},
  59. {"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"},
  60. }
  61. for _, scenario := range scenarios {
  62. actual, err := AbsoluteURL(scenario[1], scenario[2])
  63. if err != nil {
  64. t.Errorf(`Got error for (%q, %q): %v`, scenario[1], scenario[2], err)
  65. }
  66. if actual != scenario[0] {
  67. t.Errorf(`Unexpected result, got %q instead of %q for (%q, %q)`, actual, scenario[0], scenario[1], scenario[2])
  68. }
  69. }
  70. }
  71. func TestRootURL(t *testing.T) {
  72. scenarios := map[string]string{
  73. "https://example.org/path/file.ext": "https://example.org/",
  74. "//static.example.org/path/file.ext": "https://static.example.org/",
  75. "https://example|org/path/file.ext": "https://example|org/path/file.ext",
  76. }
  77. for input, expected := range scenarios {
  78. actual := RootURL(input)
  79. if actual != expected {
  80. t.Errorf(`Unexpected result, got %q instead of %q`, actual, expected)
  81. }
  82. }
  83. }
  84. func TestIsHTTPS(t *testing.T) {
  85. scenarios := map[string]bool{
  86. "https://example.org/": true,
  87. "http://example.org/": false,
  88. "https://example|org/": false,
  89. }
  90. for input, expected := range scenarios {
  91. actual := IsHTTPS(input)
  92. if actual != expected {
  93. t.Errorf(`Unexpected result, got %v instead of %v`, actual, expected)
  94. }
  95. }
  96. }
  97. func TestDomain(t *testing.T) {
  98. scenarios := map[string]string{
  99. "https://static.example.org/": "static.example.org",
  100. "https://example|org/": "https://example|org/",
  101. }
  102. for input, expected := range scenarios {
  103. actual := Domain(input)
  104. if actual != expected {
  105. t.Errorf(`Unexpected result, got %q instead of %q`, actual, expected)
  106. }
  107. }
  108. }
  109. func TestJoinBaseURLAndPath(t *testing.T) {
  110. type args struct {
  111. baseURL string
  112. path string
  113. }
  114. tests := []struct {
  115. name string
  116. args args
  117. want string
  118. wantErr bool
  119. }{
  120. {"empty base url", args{"", "/api/bookmarks/"}, "", true},
  121. {"empty path", args{"https://example.com", ""}, "", true},
  122. {"invalid base url", args{"incorrect url", ""}, "", true},
  123. {"valid", args{"https://example.com", "/api/bookmarks/"}, "https://example.com/api/bookmarks/", false},
  124. {"valid", args{"https://example.com/subfolder", "/api/bookmarks/"}, "https://example.com/subfolder/api/bookmarks/", false},
  125. }
  126. for _, tt := range tests {
  127. t.Run(tt.name, func(t *testing.T) {
  128. got, err := JoinBaseURLAndPath(tt.args.baseURL, tt.args.path)
  129. if (err != nil) != tt.wantErr {
  130. t.Errorf("JoinBaseURLAndPath error = %v, wantErr %v", err, tt.wantErr)
  131. return
  132. }
  133. if got != tt.want {
  134. t.Errorf("JoinBaseURLAndPath = %v, want %v", got, tt.want)
  135. }
  136. })
  137. }
  138. }