Commits

Luke Plant committed 64d3e0d

Added 'addSlashRedirectProcessor' and tests

Comments (0)

Files changed (3)

src/Web/Framework/Processors.hs

+module Web.Framework.Processors
+    ( addSlashRedirectProcessor
+    )
+
+where
+
+import Data.List (isSuffixOf)
+import Web.Request
+import Web.Response
+
+addSlashRedirectProcessor view req =
+    if not ("/" `isSuffixOf` pathInfo req)
+    then
+        return $ Just $ redirectResponse (pathInfo req ++ "/")
+        else view req

testsuite/tests/Main.hs

 import qualified Tests.Web.Request as Request
 import qualified Tests.Web.Response as Response
 import qualified Tests.Web.Framework as Framework
+import qualified Tests.Web.Framework.Processors as Processors
 import Test.HUnit
 
 main = runTestTT (test [
                   , Request.tests
                   , Response.tests
                   , Framework.tests
+                  , Processors.tests
                   ])

testsuite/tests/Tests/Web/Framework/Processors.hs

+module Tests.Web.Framework.Processors
+
+where
+
+import Data.Maybe (fromJust)
+import Web.Framework.Processors
+import Web.Response
+import Test.HUnit
+import Tests.Web.Framework -- reuse view functions
+
+testAddSlashRedirectProcessor1 =
+    (do
+      resp <- addSlashRedirectProcessor alwaysSucceedView1 (mkGetReq "posts")
+      return (resp == (Just $ redirectResponse "posts/"))
+    ) ~? "addSlashRedirectProcessor should add a slash if not present at end"
+
+
+testAddSlashRedirectProcessor2 =
+    (do
+      resp <- addSlashRedirectProcessor alwaysSucceedView1 (mkGetReq "/posts/")
+      return (resp == (Just resp1))
+    ) ~? "addSlashRedirectProcessor should not redirect if slash present at end"
+
+
+tests = test [ testAddSlashRedirectProcessor1
+             , testAddSlashRedirectProcessor2
+             ]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.