url_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/path/file.ext", "https://example.org/folder", "path/file.ext"},
  24. {"https://example.org/path/file.ext", "https://example.org/folder/", "https://example.org/path/file.ext"},
  25. {"https://static.example.org/path/file.ext", "https://www.example.org/", "//static.example.org/path/file.ext"},
  26. {"magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a", "https://www.example.org/", "magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a"},
  27. {"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"},
  28. }
  29. for _, scenario := range scenarios {
  30. actual, err := AbsoluteURL(scenario[1], scenario[2])
  31. if err != nil {
  32. t.Errorf(`Got error for (%q, %q): %v`, scenario[1], scenario[2], err)
  33. }
  34. if actual != scenario[0] {
  35. t.Errorf(`Unexpected result, got %q instead of %q for (%q, %q)`, actual, scenario[0], scenario[1], scenario[2])
  36. }
  37. }
  38. }
  39. func TestRootURL(t *testing.T) {
  40. scenarios := map[string]string{
  41. "https://example.org/path/file.ext": "https://example.org/",
  42. "//static.example.org/path/file.ext": "https://static.example.org/",
  43. "https://example|org/path/file.ext": "https://example|org/path/file.ext",
  44. }
  45. for input, expected := range scenarios {
  46. actual := RootURL(input)
  47. if actual != expected {
  48. t.Errorf(`Unexpected result, got %q instead of %q`, actual, expected)
  49. }
  50. }
  51. }
  52. func TestIsHTTPS(t *testing.T) {
  53. scenarios := map[string]bool{
  54. "https://example.org/": true,
  55. "http://example.org/": false,
  56. "https://example|org/": false,
  57. }
  58. for input, expected := range scenarios {
  59. actual := IsHTTPS(input)
  60. if actual != expected {
  61. t.Errorf(`Unexpected result, got %v instead of %v`, actual, expected)
  62. }
  63. }
  64. }
  65. func TestDomain(t *testing.T) {
  66. scenarios := map[string]string{
  67. "https://static.example.org/": "static.example.org",
  68. "https://example|org/": "https://example|org/",
  69. }
  70. for input, expected := range scenarios {
  71. actual := Domain(input)
  72. if actual != expected {
  73. t.Errorf(`Unexpected result, got %q instead of %q`, actual, expected)
  74. }
  75. }
  76. }