url_test.go 9.0 KB

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