EmbedProductionTest.hs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. {-# LANGUAGE TemplateHaskell, QuasiQuotes, TypeFamilies, OverloadedStrings #-}
  2. module EmbedProductionTest where
  3. -- Tests the production mode of the embedded static subsite by
  4. -- using a custom generator testGen. Also tests that the widget
  5. -- content is embedded properly.
  6. import Data.Maybe (isJust)
  7. import EmbedTestGenerator
  8. import Network.Wai.Test (SResponse(simpleHeaders))
  9. import Test.HUnit (assertFailure, assertBool)
  10. import Test.Hspec (Spec)
  11. import Yesod.Core
  12. import Yesod.EmbeddedStatic
  13. import Yesod.Test
  14. import qualified Data.ByteString as B
  15. import qualified Data.ByteString.Lazy as BL
  16. import qualified Data.Text.Lazy as TL
  17. import qualified Data.Text.Lazy.Encoding as TL
  18. mkEmbeddedStatic False "eProduction" [testGen]
  19. data MyApp = MyApp { getStatic :: EmbeddedStatic }
  20. mkYesod "MyApp" [parseRoutes|
  21. / HomeR GET
  22. /static StaticR EmbeddedStatic getStatic
  23. |]
  24. getHomeR :: Handler Html
  25. getHomeR = defaultLayout $ do
  26. toWidget [julius|console.log("Hello World");|]
  27. [whamlet|<h1>Hello|]
  28. instance Yesod MyApp where
  29. addStaticContent = embedStaticContent getStatic StaticR Right
  30. findEtag :: YesodExample site B.ByteString
  31. findEtag = withResponse $ \r ->
  32. case lookup "ETag" (simpleHeaders r) of
  33. Nothing -> liftIO (assertFailure "No etag found") >> error ""
  34. Just e -> return e
  35. hasCacheControl :: YesodExample site ()
  36. hasCacheControl = withResponse $ \r -> do
  37. liftIO $ assertBool "Cache-Control missing" $
  38. isJust $ lookup "Cache-Control" $ simpleHeaders r
  39. liftIO $ assertBool "Expires missing" $
  40. isJust $ lookup "Expires" $ simpleHeaders r
  41. embedProductionSpecs :: Spec
  42. embedProductionSpecs = yesodSpec (MyApp eProduction) $ do
  43. ydescribe "Embedded Production Entries" $ do
  44. yit "e1 loads" $ do
  45. get $ StaticR e1
  46. statusIs 200
  47. assertHeader "Content-Type" "text/plain"
  48. hasCacheControl
  49. bodyEquals "e1 production"
  50. tag <- findEtag
  51. request $ do
  52. setMethod "GET"
  53. setUrl $ StaticR e1
  54. addRequestHeader ("If-None-Match", tag)
  55. statusIs 304
  56. yit "e1 with custom built path" $ do
  57. get $ StaticR $ embeddedResourceR ["e1"] []
  58. statusIs 200
  59. assertHeader "Content-Type" "text/plain"
  60. hasCacheControl
  61. bodyEquals "e1 production"
  62. yit "e2 with simulated directory" $ do
  63. get $ StaticR e2
  64. statusIs 200
  65. assertHeader "Content-Type" "abcdef"
  66. hasCacheControl
  67. bodyEquals "e2 production"
  68. yit "e2 with custom built directory path" $ do
  69. get $ StaticR $ embeddedResourceR ["dir", "e2"] []
  70. statusIs 200
  71. assertHeader "Content-Type" "abcdef"
  72. hasCacheControl
  73. bodyEquals "e2 production"
  74. yit "e3 without haskell name" $ do
  75. get $ StaticR $ embeddedResourceR ["xxxx", "e3"] []
  76. statusIs 200
  77. assertHeader "Content-Type" "yyy"
  78. hasCacheControl
  79. bodyEquals "e3 production"
  80. yit "e4 is embedded" $ do
  81. get $ StaticR e4
  82. statusIs 200
  83. assertHeader "Content-Type" "text/plain"
  84. hasCacheControl
  85. bodyEquals "e4 production"
  86. yit "e4 extra development files are not embedded" $ do
  87. get $ StaticR $ embeddedResourceR ["dev1"] []
  88. statusIs 404
  89. ydescribe "Embedded Widget Content" $
  90. yit "Embedded Javascript" $ do
  91. get HomeR
  92. statusIs 200
  93. [script] <- htmlQuery "script"
  94. let src = BL.takeWhile (/= 34) $ BL.drop 1 $ BL.dropWhile (/= 34) script -- 34 is "
  95. get $ TL.toStrict $ TL.decodeUtf8 src
  96. statusIs 200
  97. hasCacheControl
  98. assertHeader "Content-Type" "application/javascript"
  99. bodyEquals "console.log(\"Hello World\");"