|
| 1 | +{-| |
| 2 | +Module: Test.IDE.ToolServer.MiddlewareSpec |
| 3 | +Tests for ToolServer middleware stack. |
| 4 | +
|
| 5 | +This test verifies that the ToolServer middleware stack is correctly configured, |
| 6 | +particularly that requestBodyMiddleware is present so controllers can read form params. |
| 7 | +
|
| 8 | +If requestBodyMiddleware is accidentally removed from ToolServer.hs, this test will fail. |
| 9 | +-} |
| 10 | +module Test.IDE.ToolServer.MiddlewareSpec where |
| 11 | + |
| 12 | +import IHP.Prelude |
| 13 | +import Test.Hspec |
| 14 | +import Network.Wai |
| 15 | +import Network.Wai.Test |
| 16 | +import Network.HTTP.Types |
| 17 | +import qualified Data.ByteString.Lazy as LBS |
| 18 | + |
| 19 | +import IHP.FrameworkConfig |
| 20 | +import IHP.RequestVault (frameworkConfigMiddleware, RequestBody(..)) |
| 21 | +import IHP.RequestBodyMiddleware (requestBodyMiddleware, requestBodyVaultKey) |
| 22 | +import Network.Wai.Middleware.MethodOverridePost (methodOverridePost) |
| 23 | +import qualified Data.Vault.Lazy as Vault |
| 24 | +import IHP.Controller.Session (sessionVaultKey) |
| 25 | + |
| 26 | +-- | Build the middleware stack that mirrors ToolServer.hs |
| 27 | +-- This is the exact same structure as in ToolServer: |
| 28 | +-- methodOverridePost $ sessionMiddleware $ ... $ frameworkConfigMiddleware frameworkConfig |
| 29 | +-- $ requestBodyMiddleware frameworkConfig.parseRequestBodyOptions |
| 30 | +-- $ application |
| 31 | +buildToolServerMiddlewareStack :: FrameworkConfig -> Middleware |
| 32 | +buildToolServerMiddlewareStack frameworkConfig = |
| 33 | + methodOverridePost |
| 34 | + . addEmptySession |
| 35 | + . frameworkConfigMiddleware frameworkConfig |
| 36 | + . requestBodyMiddleware frameworkConfig.parseRequestBodyOptions |
| 37 | + where |
| 38 | + -- Add an empty session to the vault (simplified for testing) |
| 39 | + addEmptySession :: Middleware |
| 40 | + addEmptySession app req respond = |
| 41 | + let req' = req { vault = Vault.insert sessionVaultKey mempty req.vault } |
| 42 | + in app req' respond |
| 43 | + |
| 44 | +-- | A simple test application that reads a param from the parsed request body |
| 45 | +-- and echoes it back. This mimics what controllers do with paramOrDefault. |
| 46 | +-- |
| 47 | +-- If requestBodyMiddleware is missing, Vault.lookup will return Nothing |
| 48 | +-- and we'll return "MIDDLEWARE_MISSING" to make the test fail clearly. |
| 49 | +testApplication :: Application |
| 50 | +testApplication request respond = do |
| 51 | + let maybeBody = Vault.lookup requestBodyVaultKey request.vault |
| 52 | + let responseText = case maybeBody of |
| 53 | + Just (FormBody params _files) -> |
| 54 | + case lookup "testParam" params of |
| 55 | + Just value -> cs value |
| 56 | + Nothing -> "param_not_found" |
| 57 | + Just (JSONBody _ _) -> "unexpected_json_body" |
| 58 | + Nothing -> "MIDDLEWARE_MISSING" |
| 59 | + respond $ responseLBS status200 [(hContentType, "text/plain")] responseText |
| 60 | + |
| 61 | +-- | Make a POST request with form data |
| 62 | +postWithParams :: ByteString -> [(ByteString, ByteString)] -> Session SResponse |
| 63 | +postWithParams path params = srequest $ SRequest req body |
| 64 | + where |
| 65 | + body = LBS.fromStrict $ renderSimpleQuery False params |
| 66 | + req = defaultRequest |
| 67 | + { requestMethod = methodPost |
| 68 | + , pathInfo = filter (/= "") $ decodePathSegments path |
| 69 | + , rawPathInfo = path |
| 70 | + , requestHeaders = [(hContentType, "application/x-www-form-urlencoded")] |
| 71 | + } |
| 72 | + |
| 73 | +tests :: Spec |
| 74 | +tests = do |
| 75 | + describe "ToolServer Middleware Stack" $ do |
| 76 | + describe "requestBodyMiddleware" $ do |
| 77 | + it "parses form params and stores them in the request vault" $ do |
| 78 | + -- Build the framework config |
| 79 | + frameworkConfig <- buildFrameworkConfig (pure ()) |
| 80 | + |
| 81 | + -- Build the full middleware stack + application |
| 82 | + let app = buildToolServerMiddlewareStack frameworkConfig testApplication |
| 83 | + |
| 84 | + -- Make a POST request with a form parameter |
| 85 | + response <- runSession (postWithParams "/" [("testParam", "hello-world")]) app |
| 86 | + |
| 87 | + -- Verify the middleware parsed the param correctly |
| 88 | + simpleBody response `shouldBe` "hello-world" |
| 89 | + |
| 90 | + it "handles missing params gracefully" $ do |
| 91 | + frameworkConfig <- buildFrameworkConfig (pure ()) |
| 92 | + let app = buildToolServerMiddlewareStack frameworkConfig testApplication |
| 93 | + |
| 94 | + response <- runSession (postWithParams "/" []) app |
| 95 | + |
| 96 | + -- Should indicate param was not found (not that middleware is missing) |
| 97 | + simpleBody response `shouldBe` "param_not_found" |
| 98 | + |
| 99 | + it "fails if requestBodyMiddleware is removed from the stack" $ do |
| 100 | + -- This test demonstrates what happens without the middleware |
| 101 | + frameworkConfig <- buildFrameworkConfig (pure ()) |
| 102 | + |
| 103 | + -- Build middleware stack WITHOUT requestBodyMiddleware |
| 104 | + let brokenMiddlewareStack = |
| 105 | + methodOverridePost |
| 106 | + . frameworkConfigMiddleware frameworkConfig |
| 107 | + -- Note: requestBodyMiddleware is intentionally missing here! |
| 108 | + |
| 109 | + let app = brokenMiddlewareStack testApplication |
| 110 | + |
| 111 | + response <- runSession (postWithParams "/" [("testParam", "test")]) app |
| 112 | + |
| 113 | + -- Without the middleware, the vault won't have the parsed body |
| 114 | + simpleBody response `shouldBe` "MIDDLEWARE_MISSING" |
0 commit comments