web_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package web
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. // Not a Test function, a test helper.
  7. func testSetupGlobals() {
  8. globals = Globals{
  9. AssetsBaseURL: "https://cdn.osinet.fr",
  10. AssetsVersion: 1,
  11. AssetsPath: "web/public",
  12. SiteBaseURL: "http://sut.kurz.osinet.fr",
  13. RefreshDelay: 1,
  14. SiteName: "Kurz test site",
  15. }
  16. }
  17. func TestURLForAsset(t *testing.T) {
  18. testSetupGlobals()
  19. type check struct {
  20. Expected string
  21. Name string
  22. NS string
  23. OK bool
  24. Path string
  25. }
  26. checks := []check{
  27. {"https://cdn.osinet.fr/favicon.ico?v=1", "empty favicon", "", true, "favicon.ico"},
  28. {"https://cdn.osinet.fr/favicon.ico?v=1", "root favicon", "/", false, "favicon.ico"},
  29. {"https://cdn.osinet.fr/css?v=1", "empty css path", "css", true, ""},
  30. {"https://cdn.osinet.fr/css?v=1", "root css path", "css", true, "/"},
  31. }
  32. for _, check := range checks {
  33. t.Run(check.Name, func(t *testing.T) {
  34. actual, err := URLForAsset(check.NS, check.Path)
  35. if !check.OK {
  36. if err == nil {
  37. t.Errorf("Should have failed for: %s %s but did not\n", check.NS, check.Path)
  38. t.FailNow()
  39. }
  40. return
  41. }
  42. if check.OK && err != nil {
  43. t.Errorf("Error building asset Expected: %v\n", err)
  44. t.FailNow()
  45. } else if actual != check.Expected || err != nil {
  46. t.Errorf("Incorrect asset expecting <%s>, got <%s>\n", check.Expected, actual)
  47. t.Fail()
  48. }
  49. })
  50. }
  51. }
  52. func TestURLForAssetSad(t *testing.T) {
  53. testSetupGlobals()
  54. globals.AssetsBaseURL = ":::"
  55. defer func() {
  56. if r := recover(); r == nil {
  57. fmt.Println("Invalid AssetsBaseURL did not cause asset URL generation to fail")
  58. }
  59. }()
  60. URLForAsset("", "")
  61. }