url_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 (
  5. "net"
  6. "net/url"
  7. "testing"
  8. )
  9. func TestIsRelativePath(t *testing.T) {
  10. scenarios := map[string]bool{
  11. // Valid relative paths
  12. "path/to/file.ext": true,
  13. "./path/to/file.ext": true,
  14. "../path/to/file.ext": true,
  15. "file.ext": true,
  16. "./file.ext": true,
  17. "../file.ext": true,
  18. "/absolute/path": true,
  19. "path?query=value": true,
  20. "path#fragment": true,
  21. "path?query#fragment": true,
  22. // Not relative paths
  23. "https://example.org/file.ext": false,
  24. "http://example.org/file.ext": false,
  25. "//example.org/file.ext": false,
  26. "//example.org": false,
  27. `/\example.org`: false,
  28. `\example.org`: false,
  29. `path\to\file.ext`: false,
  30. "ftp://example.org/file.ext": false,
  31. "mailto:user@example.org": false,
  32. "magnet:?xt=urn:btih:example": false,
  33. "": false,
  34. "magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C": false,
  35. }
  36. for input, expected := range scenarios {
  37. actual := IsRelativePath(input)
  38. if actual != expected {
  39. t.Errorf(`Unexpected result for IsRelativePath, got %v instead of %v for %q`, actual, expected, input)
  40. }
  41. }
  42. }
  43. func TestIsAbsoluteURL(t *testing.T) {
  44. scenarios := map[string]bool{
  45. "https://example.org/file.pdf": true,
  46. "https://example.org/file.pdf?download=1#page=2": true,
  47. "mailto:user@example.org": false,
  48. "data:text/plain,hello": false,
  49. "magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7": false,
  50. "invalid url": false,
  51. "/relative/path": false,
  52. "//example.org/path": false,
  53. " https://example.org/path": false,
  54. "\thttps://example.org/path": false,
  55. }
  56. for input, expected := range scenarios {
  57. actual := IsAbsoluteURL(input)
  58. if actual != expected {
  59. t.Errorf(`Unexpected result, got %v instead of %v for %q`, actual, expected, input)
  60. }
  61. }
  62. }
  63. func TestAbsoluteURL(t *testing.T) {
  64. type absoluteScenario struct {
  65. name string
  66. base string
  67. relative string
  68. expected string
  69. wantErr bool
  70. runWithParsed bool
  71. useNilParsed bool
  72. }
  73. scenarios := []absoluteScenario{
  74. {"absolute path", "https://example.org/folder/", "/path/file.ext", "https://example.org/path/file.ext", false, true, false},
  75. {"relative path", "https://example.org/folder/", "path/file.ext", "https://example.org/folder/path/file.ext", false, true, false},
  76. {"dot path root", "https://example.org/path", "./", "https://example.org/", false, true, false},
  77. {"dot path folder", "https://example.org/folder/", "./", "https://example.org/folder/", false, true, false},
  78. {"missing slash in base", "https://example.org/folder", "path/file.ext", "https://example.org/path/file.ext", false, true, false},
  79. {"already absolute", "https://example.org/folder/", "https://example.org/path/file.ext", "https://example.org/path/file.ext", false, true, false},
  80. {"protocol relative", "https://www.example.org/", "//static.example.org/path/file.ext", "https://static.example.org/path/file.ext", false, true, false},
  81. {"magnet keeps scheme", "https://www.example.org/", "magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a", "magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a", false, true, false},
  82. {"magnet with query", "https://www.example.org/", "magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7", "magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7", false, true, false},
  83. {"empty relative returns base", "https://example.org/folder/", "", "https://example.org/folder/", false, true, false},
  84. {"invalid base errors", "://bad", "path/file.ext", "", true, false, false},
  85. {"absolute ignores invalid base", "://bad", "https://example.org/path/file.ext", "https://example.org/path/file.ext", false, true, true},
  86. }
  87. for _, scenario := range scenarios {
  88. t.Run(scenario.name, func(t *testing.T) {
  89. actual, err := ResolveToAbsoluteURL(scenario.base, scenario.relative)
  90. if scenario.wantErr {
  91. if err == nil {
  92. t.Fatalf("expected error for base %q relative %q", scenario.base, scenario.relative)
  93. }
  94. return
  95. }
  96. if err != nil {
  97. t.Fatalf("unexpected error for base %q relative %q: %v", scenario.base, scenario.relative, err)
  98. }
  99. if actual != scenario.expected {
  100. t.Fatalf("unexpected result, got %q instead of %q for (%q, %q)", actual, scenario.expected, scenario.base, scenario.relative)
  101. }
  102. if scenario.runWithParsed {
  103. var parsedBase *url.URL
  104. if !scenario.useNilParsed && scenario.base != "" {
  105. var parseErr error
  106. parsedBase, parseErr = url.Parse(scenario.base)
  107. if parseErr != nil {
  108. t.Fatalf("unable to parse base %q: %v", scenario.base, parseErr)
  109. }
  110. }
  111. actualParsed, errParsed := ResolveToAbsoluteURLWithParsedBaseURL(parsedBase, scenario.relative)
  112. if errParsed != nil {
  113. t.Fatalf("unexpected error with parsed base for (%q, %q): %v", scenario.base, scenario.relative, errParsed)
  114. }
  115. if actualParsed != scenario.expected {
  116. t.Fatalf("unexpected parsed-base result, got %q instead of %q for (%q, %q)", actualParsed, scenario.expected, scenario.base, scenario.relative)
  117. }
  118. }
  119. })
  120. }
  121. }
  122. func TestRootURL(t *testing.T) {
  123. scenarios := map[string]string{
  124. "": "",
  125. "https://example.org/path/file.ext": "https://example.org/",
  126. "https://example.org/path/file.ext?test=abc": "https://example.org/",
  127. "//static.example.org/path/file.ext": "https://static.example.org/",
  128. "https://example|org/path/file.ext": "https://example|org/path/file.ext",
  129. "/relative/path": "/relative/path",
  130. "http://example.org:8080/path": "http://example.org:8080/",
  131. }
  132. for input, expected := range scenarios {
  133. actual := RootURL(input)
  134. if actual != expected {
  135. t.Errorf(`Unexpected result, got %q instead of %q`, actual, expected)
  136. }
  137. }
  138. }
  139. func TestIsHTTPS(t *testing.T) {
  140. scenarios := map[string]bool{
  141. "https://example.org/": true,
  142. "http://example.org/": false,
  143. "https://example|org/": false,
  144. }
  145. for input, expected := range scenarios {
  146. actual := IsHTTPS(input)
  147. if actual != expected {
  148. t.Errorf(`Unexpected result, got %v instead of %v`, actual, expected)
  149. }
  150. }
  151. }
  152. func TestDomain(t *testing.T) {
  153. scenarios := map[string]string{
  154. "https://static.example.org/": "static.example.org",
  155. "https://example|org/": "https://example|org/",
  156. }
  157. for input, expected := range scenarios {
  158. actual := Domain(input)
  159. if actual != expected {
  160. t.Errorf(`Unexpected result, got %q instead of %q`, actual, expected)
  161. }
  162. }
  163. }
  164. func TestDomainWithoutWWW(t *testing.T) {
  165. scenarios := map[string]string{
  166. "https://www.example.org/": "example.org",
  167. "https://example.org/": "example.org",
  168. "https://www.sub.example.org/": "sub.example.org",
  169. "https://example|org/": "https://example|org/",
  170. }
  171. for input, expected := range scenarios {
  172. actual := DomainWithoutWWW(input)
  173. if actual != expected {
  174. t.Errorf(`Unexpected result, got %q instead of %q`, actual, expected)
  175. }
  176. }
  177. }
  178. func TestJoinBaseURLAndPath(t *testing.T) {
  179. type args struct {
  180. baseURL string
  181. path string
  182. }
  183. tests := []struct {
  184. name string
  185. args args
  186. want string
  187. wantErr bool
  188. }{
  189. {"empty base url", args{"", "/api/bookmarks/"}, "", true},
  190. {"empty path", args{"https://example.com", ""}, "", true},
  191. {"invalid base url", args{"incorrect url", ""}, "", true},
  192. {"valid", args{"https://example.com", "/api/bookmarks/"}, "https://example.com/api/bookmarks/", false},
  193. {"valid", args{"https://example.com/subfolder", "/api/bookmarks/"}, "https://example.com/subfolder/api/bookmarks/", false},
  194. }
  195. for _, tt := range tests {
  196. t.Run(tt.name, func(t *testing.T) {
  197. got, err := JoinBaseURLAndPath(tt.args.baseURL, tt.args.path)
  198. if (err != nil) != tt.wantErr {
  199. t.Errorf("JoinBaseURLAndPath error = %v, wantErr %v", err, tt.wantErr)
  200. return
  201. }
  202. if got != tt.want {
  203. t.Errorf("JoinBaseURLAndPath = %v, want %v", got, tt.want)
  204. }
  205. })
  206. }
  207. }
  208. func TestIsNonPublicIP(t *testing.T) {
  209. testCases := []struct {
  210. name string
  211. ipString string
  212. want bool
  213. }{
  214. {"nil", "", true},
  215. {"private IPv4", "192.168.1.10", true},
  216. {"shared address space IPv4", "100.64.0.1", true},
  217. {"loopback IPv4", "127.0.0.1", true},
  218. {"link-local IPv4", "169.254.42.1", true},
  219. {"multicast IPv4", "224.0.0.1", true},
  220. {"unspecified IPv6", "::", true},
  221. {"loopback IPv6", "::1", true},
  222. {"multicast IPv6", "ff02::1", true},
  223. {"public IPv4", "93.184.216.34", false},
  224. {"public IPv6", "2001:4860:4860::8888", false},
  225. }
  226. for _, tc := range testCases {
  227. t.Run(tc.name, func(t *testing.T) {
  228. var ip net.IP
  229. if tc.ipString != "" {
  230. ip = net.ParseIP(tc.ipString)
  231. if ip == nil {
  232. t.Fatalf("unable to parse %q", tc.ipString)
  233. }
  234. }
  235. if got := IsNonPublicIP(ip); got != tc.want {
  236. t.Fatalf("unexpected result for %s: got %v want %v", tc.name, got, tc.want)
  237. }
  238. })
  239. }
  240. }