url_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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
  5. import "testing"
  6. func TestGetAbsoluteURLWithAbsolutePath(t *testing.T) {
  7. expected := `https://example.org/path/file.ext`
  8. input := `/path/file.ext`
  9. output, err := AbsoluteURL("https://example.org/folder/", input)
  10. if err != nil {
  11. t.Error(err)
  12. }
  13. if expected != output {
  14. t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
  15. }
  16. }
  17. func TestGetAbsoluteURLWithRelativePath(t *testing.T) {
  18. expected := `https://example.org/folder/path/file.ext`
  19. input := `path/file.ext`
  20. output, err := AbsoluteURL("https://example.org/folder/", input)
  21. if err != nil {
  22. t.Error(err)
  23. }
  24. if expected != output {
  25. t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
  26. }
  27. }
  28. func TestGetAbsoluteURLWithRelativePaths(t *testing.T) {
  29. expected := `https://example.org/path/file.ext`
  30. input := `path/file.ext`
  31. output, err := AbsoluteURL("https://example.org/folder", input)
  32. if err != nil {
  33. t.Error(err)
  34. }
  35. if expected != output {
  36. t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
  37. }
  38. }
  39. func TestWhenInputIsAlreadyAbsolute(t *testing.T) {
  40. expected := `https://example.org/path/file.ext`
  41. input := `https://example.org/path/file.ext`
  42. output, err := AbsoluteURL("https://example.org/folder/", input)
  43. if err != nil {
  44. t.Error(err)
  45. }
  46. if expected != output {
  47. t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
  48. }
  49. }
  50. func TestGetAbsoluteURLWithProtocolRelative(t *testing.T) {
  51. expected := `https://static.example.org/path/file.ext`
  52. input := `//static.example.org/path/file.ext`
  53. output, err := AbsoluteURL("https://www.example.org/", input)
  54. if err != nil {
  55. t.Error(err)
  56. }
  57. if expected != output {
  58. t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
  59. }
  60. }
  61. func TestGetRootURL(t *testing.T) {
  62. expected := `https://example.org/`
  63. input := `https://example.org/path/file.ext`
  64. output := RootURL(input)
  65. if expected != output {
  66. t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
  67. }
  68. }
  69. func TestGetRootURLWithProtocolRelativePath(t *testing.T) {
  70. expected := `https://static.example.org/`
  71. input := `//static.example.org/path/file.ext`
  72. output := RootURL(input)
  73. if expected != output {
  74. t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
  75. }
  76. }
  77. func TestIsHTTPS(t *testing.T) {
  78. if !IsHTTPS("https://example.org/") {
  79. t.Error("Unable to recognize HTTPS URL")
  80. }
  81. if IsHTTPS("http://example.org/") {
  82. t.Error("Unable to recognize HTTP URL")
  83. }
  84. if IsHTTPS("") {
  85. t.Error("Unable to recognize malformed URL")
  86. }
  87. }
  88. func TestGetDomain(t *testing.T) {
  89. expected := `static.example.org`
  90. input := `http://static.example.org/`
  91. output := Domain(input)
  92. if expected != output {
  93. t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
  94. }
  95. }