url_test.go 2.6 KB

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