EmbedDevelTest.hs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. {-# LANGUAGE TemplateHaskell, QuasiQuotes, TypeFamilies, OverloadedStrings #-}
  2. module EmbedDevelTest where
  3. -- Tests the development mode of the embedded static subsite by
  4. -- using a custom generator testGen.
  5. import Data.Maybe (isNothing)
  6. import EmbedTestGenerator
  7. import EmbedProductionTest (findEtag)
  8. import Network.Wai.Test (SResponse(simpleHeaders))
  9. import Test.HUnit (assertBool)
  10. import Test.Hspec (Spec)
  11. import Yesod.Core
  12. import Yesod.EmbeddedStatic
  13. import Yesod.Test
  14. mkEmbeddedStatic True "eDev" [testGen]
  15. data MyApp = MyApp { getStatic :: EmbeddedStatic }
  16. mkYesod "MyApp" [parseRoutes|
  17. /static StaticR EmbeddedStatic getStatic
  18. |]
  19. instance Yesod MyApp
  20. noCacheControl :: YesodExample site ()
  21. noCacheControl = withResponse $ \r -> do
  22. liftIO $ assertBool "Cache-Control exists" $
  23. isNothing $ lookup "Cache-Control" $ simpleHeaders r
  24. liftIO $ assertBool "Expires exists" $
  25. isNothing $ lookup "Expires" $ simpleHeaders r
  26. embedDevSpecs :: Spec
  27. embedDevSpecs = yesodSpec (MyApp eDev) $ do
  28. ydescribe "Embedded Development Entries" $ do
  29. yit "e1 loads" $ do
  30. get $ StaticR e1
  31. statusIs 200
  32. assertHeader "Content-Type" "text/plain"
  33. noCacheControl
  34. bodyEquals "e1 devel"
  35. tag <- findEtag
  36. request $ do
  37. setMethod "GET"
  38. setUrl $ StaticR e1
  39. addRequestHeader ("If-None-Match", tag)
  40. statusIs 304
  41. yit "e2 with simulated directory" $ do
  42. get $ StaticR e2
  43. statusIs 200
  44. assertHeader "Content-Type" "abcdef"
  45. noCacheControl
  46. bodyEquals "e2 devel"
  47. yit "e3 without haskell name" $ do
  48. get $ StaticR $ embeddedResourceR ["xxxx", "e3"] []
  49. statusIs 200
  50. assertHeader "Content-Type" "yyy"
  51. noCacheControl
  52. bodyEquals "e3 devel"
  53. yit "e4 loads" $ do
  54. get $ StaticR e4
  55. statusIs 200
  56. assertHeader "Content-Type" "text/plain"
  57. noCacheControl
  58. bodyEquals "e4 devel"
  59. yit "e4 extra development dev1" $ do
  60. get $ StaticR $ embeddedResourceR ["dev1"] []
  61. statusIs 200
  62. assertHeader "Content-Type" "mime"
  63. noCacheControl
  64. bodyEquals "dev1 content"
  65. tag <- findEtag
  66. request $ do
  67. setMethod "GET"
  68. setUrl $ StaticR $ embeddedResourceR ["dev1"] []
  69. addRequestHeader ("If-None-Match", tag)
  70. statusIs 304
  71. yit "e4 extra development with path" $ do
  72. get $ StaticR $ embeddedResourceR ["dir", "dev2"] []
  73. statusIs 200
  74. assertHeader "Content-Type" "mime2"
  75. noCacheControl
  76. bodyEquals "dev2 content"
  77. yit "extra development file 404" $ do
  78. get $ StaticR $ embeddedResourceR ["xxxxxxxxxx"] []
  79. statusIs 404