url_test.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 TestAbsoluteURL(t *testing.T) {
  7. scenarios := [][]string{
  8. []string{"https://example.org/path/file.ext", "https://example.org/folder/", "/path/file.ext"},
  9. []string{"https://example.org/folder/path/file.ext", "https://example.org/folder/", "path/file.ext"},
  10. []string{"https://example.org/path/file.ext", "https://example.org/folder", "path/file.ext"},
  11. []string{"https://example.org/path/file.ext", "https://example.org/folder/", "https://example.org/path/file.ext"},
  12. []string{"https://static.example.org/path/file.ext", "https://www.example.org/", "//static.example.org/path/file.ext"},
  13. }
  14. for _, scenario := range scenarios {
  15. actual, err := AbsoluteURL(scenario[1], scenario[2])
  16. if err != nil {
  17. t.Errorf(`Got error for (%q, %q): %v`, scenario[1], scenario[2], err)
  18. }
  19. if actual != scenario[0] {
  20. t.Errorf(`Unexpected result, got %q instead of %q for (%q, %q)`, actual, scenario[0], scenario[1], scenario[2])
  21. }
  22. }
  23. }
  24. func TestRootURL(t *testing.T) {
  25. scenarios := map[string]string{
  26. "https://example.org/path/file.ext": "https://example.org/",
  27. "//static.example.org/path/file.ext": "https://static.example.org/",
  28. "https://example|org/path/file.ext": "https://example|org/path/file.ext",
  29. }
  30. for input, expected := range scenarios {
  31. actual := RootURL(input)
  32. if actual != expected {
  33. t.Errorf(`Unexpected result, got %q instead of %q`, actual, expected)
  34. }
  35. }
  36. }
  37. func TestIsHTTPS(t *testing.T) {
  38. scenarios := map[string]bool{
  39. "https://example.org/": true,
  40. "http://example.org/": false,
  41. "https://example|org/": false,
  42. }
  43. for input, expected := range scenarios {
  44. actual := IsHTTPS(input)
  45. if actual != expected {
  46. t.Errorf(`Unexpected result, got %v instead of %v`, actual, expected)
  47. }
  48. }
  49. }
  50. func TestDomain(t *testing.T) {
  51. scenarios := map[string]string{
  52. "https://static.example.org/": "static.example.org",
  53. "https://example|org/": "https://example|org/",
  54. }
  55. for input, expected := range scenarios {
  56. actual := Domain(input)
  57. if actual != expected {
  58. t.Errorf(`Unexpected result, got %q instead of %q`, actual, expected)
  59. }
  60. }
  61. }