ui_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package ui
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/gorilla/mux"
  6. )
  7. /* Not a Test function, a test helper.
  8. AssetsPath is "public" instead of "web/public" because tests normally run from the
  9. package directory, not the project directory.
  10. */
  11. func testSetupGlobals() {
  12. globals = Globals{
  13. AssetsBaseURL: "https://cdn.osinet.fr",
  14. AssetsVersion: 1,
  15. AssetsPath: "public",
  16. SiteBaseURL: "http://sut.kurz.osinet.fr",
  17. RefreshDelay: 1,
  18. SiteName: "Kurz test site",
  19. }
  20. }
  21. func TestURLForAsset(t *testing.T) {
  22. testSetupGlobals()
  23. type check struct {
  24. Expected string
  25. Name string
  26. NS string
  27. OK bool
  28. Path string
  29. }
  30. checks := []check{
  31. {"https://cdn.osinet.fr/favicon.ico?v=1", "empty favicon", "", true, "favicon.ico"},
  32. {"https://cdn.osinet.fr/favicon.ico?v=1", "root favicon", "/", false, "favicon.ico"},
  33. {"https://cdn.osinet.fr/css?v=1", "empty css path", "css", true, ""},
  34. {"https://cdn.osinet.fr/css?v=1", "root css path", "css", true, "/"},
  35. }
  36. for _, check := range checks {
  37. t.Run(check.Name, func(t *testing.T) {
  38. actual, err := URLForAsset(check.NS, check.Path)
  39. if !check.OK {
  40. if err == nil {
  41. t.Errorf("Should have failed for: %s %s but did not\n", check.NS, check.Path)
  42. t.FailNow()
  43. }
  44. return
  45. }
  46. if check.OK && err != nil {
  47. t.Errorf("Error building asset Expected: %v\n", err)
  48. t.FailNow()
  49. } else if actual != check.Expected || err != nil {
  50. t.Errorf("Incorrect asset expecting <%s>, got <%s>\n", check.Expected, actual)
  51. t.Fail()
  52. }
  53. })
  54. }
  55. }
  56. func TestURLForAssetSad(t *testing.T) {
  57. testSetupGlobals()
  58. globals.AssetsBaseURL = ":::"
  59. defer func() {
  60. if r := recover(); r == nil {
  61. fmt.Println("Invalid AssetsBaseURL did not cause asset URL generation to fail")
  62. }
  63. }()
  64. URLForAsset("", "")
  65. }
  66. func TestURLFromRoute(t *testing.T) {
  67. testSetupGlobals()
  68. router := mux.NewRouter()
  69. SetupUI(router, globals.AssetsPath)
  70. type strings map[string]string
  71. type check struct {
  72. Expected string
  73. OK bool
  74. Params strings
  75. Route string
  76. }
  77. checks := []check{
  78. {globals.SiteBaseURL + "/", true, nil, RouteGetRoot},
  79. {globals.SiteBaseURL + "/foo", true, strings{"short": "foo"}, RouteGetShort},
  80. {"", false, nil, "NoSuchRoute"},
  81. {"", false, nil, RouteGetShort},
  82. }
  83. for _, check := range checks {
  84. actual, err := URLFromRoute(router, check.Route, check.Params)
  85. if check.OK && err != nil {
  86. t.Errorf("Error building route %s: %v\n", check.Route, err)
  87. t.FailNow()
  88. } else if actual != check.Expected {
  89. t.Errorf("Route %s failure. Expected %s, got %s\n", RouteGetRoot, check.Expected, actual)
  90. t.Fail()
  91. }
  92. }
  93. }