Skip to content

Commit

Permalink
read from queryString instead of rawQueryString (#360)
Browse files Browse the repository at this point in the history
* read from queryString instead of rawQueryString

* use decodeUtf8Lenient

* add test
  • Loading branch information
zachary822 authored Dec 17, 2023
1 parent 0d0c1d2 commit 4bdeb44
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
9 changes: 3 additions & 6 deletions Web/Scotty/Route.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import Control.Monad.IO.Class (MonadIO(..))
import UnliftIO (MonadUnliftIO(..))
import qualified Control.Monad.State as MS

import qualified Data.ByteString.Char8 as B

import Data.Maybe (fromMaybe)
import Data.String (fromString)
import qualified Data.Text as T

Expand Down Expand Up @@ -160,14 +157,14 @@ mkEnv :: MonadIO m => BodyInfo -> Request -> [Param] -> RouteOptions -> m Action
mkEnv bodyInfo req captureps opts = do
(formps, bodyFiles) <- liftIO $ getFormParamsAndFilesAction req bodyInfo opts
let
queryps = parseEncodedParams $ rawQueryString req
queryps = parseEncodedParams $ queryString req
bodyFiles' = [ (decodeUtf8Lenient k, fi) | (k,fi) <- bodyFiles ]
responseInit <- liftIO $ newTVarIO defaultScottyResponse
return $ Env req captureps formps queryps (getBodyAction bodyInfo opts) (getBodyChunkAction bodyInfo) bodyFiles' responseInit


parseEncodedParams :: B.ByteString -> [Param]
parseEncodedParams bs = [ (k, fromMaybe "" v) | (k,v) <- parseQueryText bs ]
parseEncodedParams :: Query -> [Param]
parseEncodedParams qs = [ ( decodeUtf8Lenient k, maybe "" decodeUtf8Lenient v) | (k,v) <- qs ]

{- | Match requests using a regular expression.
Named captures are not yet supported.
Expand Down
12 changes: 11 additions & 1 deletion test/Web/ScottySpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Data.String
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TLE
import Network.HTTP.Types
import Network.Wai (Application, responseLBS)
import Network.Wai (Application, Request(queryString), responseLBS)
import qualified Control.Exception.Lifted as EL
import qualified Control.Exception as E

Expand Down Expand Up @@ -174,6 +174,16 @@ spec = do
it "Responds with a 302 Redirect" $ do
get "/a" `shouldRespondWith` 302 { matchHeaders = ["Location" <:> "/b"] }

describe "middleware" $ do
context "rewrites the query string" $ do
withApp (do
Scotty.middleware $ \app req sendResponse ->
app req{queryString = [("query", Just "haskell")]} sendResponse
Scotty.matchAny "/search" $ queryParam "query" >>= text
) $ do
it "returns query parameter with given name" $ do
get "/search" `shouldRespondWith` "haskell"

describe "captureParam" $ do
withApp (
do
Expand Down

0 comments on commit 4bdeb44

Please sign in to comment.