url_test.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package url // import "miniflux.app/url"
  5. import "testing"
  6. func TestIsAbsoluteURL(t *testing.T) {
  7. scenarios := map[string]bool{
  8. "https://example.org/file.pdf": true,
  9. "magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7": true,
  10. "invalid url": false,
  11. }
  12. for input, expected := range scenarios {
  13. actual := IsAbsoluteURL(input)
  14. if actual != expected {
  15. t.Errorf(`Unexpected result, got %v instead of %v for %q`, actual, expected, input)
  16. }
  17. }
  18. }
  19. func TestAbsoluteURL(t *testing.T) {
  20. scenarios := [][]string{
  21. {"https://example.org/path/file.ext", "https://example.org/folder/", "/path/file.ext"},
  22. {"https://example.org/folder/path/file.ext", "https://example.org/folder/", "path/file.ext"},
  23. {"https://example.org/", "https://example.org/path", "./"},
  24. {"https://example.org/folder/", "https://example.org/folder/", "./"},
  25. {"https://example.org/path/file.ext", "https://example.org/folder", "path/file.ext"},
  26. {"https://example.org/path/file.ext", "https://example.org/folder/", "https://example.org/path/file.ext"},
  27. {"https://static.example.org/path/file.ext", "https://www.example.org/", "//static.example.org/path/file.ext"},
  28. {"magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a", "https://www.example.org/", "magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a"},
  29. {"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"},
  30. }
  31. for _, scenario := range scenarios {
  32. actual, err := AbsoluteURL(scenario[1], scenario[2])
  33. if err != nil {
  34. t.Errorf(`Got error for (%q, %q): %v`, scenario[1], scenario[2], err)
  35. }
  36. if actual != scenario[0] {
  37. t.Errorf(`Unexpected result, got %q instead of %q for (%q, %q)`, actual, scenario[0], scenario[1], scenario[2])
  38. }
  39. }
  40. }
  41. func TestRootURL(t *testing.T) {
  42. scenarios := map[string]string{
  43. "https://example.org/path/file.ext": "https://example.org/",
  44. "//static.example.org/path/file.ext": "https://static.example.org/",
  45. "https://example|org/path/file.ext": "https://example|org/path/file.ext",
  46. }
  47. for input, expected := range scenarios {
  48. actual := RootURL(input)
  49. if actual != expected {
  50. t.Errorf(`Unexpected result, got %q instead of %q`, actual, expected)
  51. }
  52. }
  53. }
  54. func TestIsHTTPS(t *testing.T) {
  55. scenarios := map[string]bool{
  56. "https://example.org/": true,
  57. "http://example.org/": false,
  58. "https://example|org/": false,
  59. }
  60. for input, expected := range scenarios {
  61. actual := IsHTTPS(input)
  62. if actual != expected {
  63. t.Errorf(`Unexpected result, got %v instead of %v`, actual, expected)
  64. }
  65. }
  66. }
  67. func TestDomain(t *testing.T) {
  68. scenarios := map[string]string{
  69. "https://static.example.org/": "static.example.org",
  70. "https://example|org/": "https://example|org/",
  71. }
  72. for input, expected := range scenarios {
  73. actual := Domain(input)
  74. if actual != expected {
  75. t.Errorf(`Unexpected result, got %q instead of %q`, actual, expected)
  76. }
  77. }
  78. }