From 6df3429b6860f3e4b21d1382f6c50b0bec9c0d96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Hahn?= Date: Wed, 6 Apr 2016 15:24:30 +0800 Subject: [PATCH] refactored basic auth test cases --- servant-server/servant-server.cabal | 1 + servant-server/test/Servant/ServerSpec.hs | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/servant-server/servant-server.cabal b/servant-server/servant-server.cabal index 5ba00c65..0dfbfe14 100644 --- a/servant-server/servant-server.cabal +++ b/servant-server/servant-server.cabal @@ -111,6 +111,7 @@ test-suite spec base == 4.* , base-compat , aeson + , base64-bytestring , bytestring , bytestring-conversion , directory diff --git a/servant-server/test/Servant/ServerSpec.hs b/servant-server/test/Servant/ServerSpec.hs index 31bdadd1..b3f021e1 100644 --- a/servant-server/test/Servant/ServerSpec.hs +++ b/servant-server/test/Servant/ServerSpec.hs @@ -19,8 +19,10 @@ import Control.Applicative ((<$>)) import Control.Monad (forM_, when, unless) import Control.Monad.Trans.Except (ExceptT, throwE) import Data.Aeson (FromJSON, ToJSON, decode', encode) +import qualified Data.ByteString.Base64 as Base64 import Data.ByteString.Conversion () import Data.Char (toUpper) +import Data.Monoid import Data.Proxy (Proxy (Proxy)) import Data.String (fromString) import Data.String.Conversions (cs) @@ -569,17 +571,17 @@ basicAuthSpec = do with (return (serveWithContext basicAuthApi basicAuthContext basicAuthServer)) $ do context "Basic Authentication" $ do + let basicAuthHeaders user password = + [("Authorization", "Basic " <> Base64.encode (user <> ":" <> password))] it "returns 401 when no credentials given" $ do get "/basic" `shouldRespondWith` 401 it "returns 403 when invalid credentials given" $ do - let invalid = [("Authorization", "Basic bbbbbbbbbDpzZXJ2ZXI=")] -- fixme: how do I create basic auth tokens? - THW.request methodGet "/basic" invalid "" + THW.request methodGet "/basic" (basicAuthHeaders "servant" "wrong") "" `shouldRespondWith` 403 it "returns 200 with the right password" $ do - let validCredentials = [("Authorization", "Basic c2VydmFudDpzZXJ2ZXI=")] - THW.request methodGet "/basic" validCredentials "" + THW.request methodGet "/basic" (basicAuthHeaders "servant" "server") "" `shouldRespondWith` 200 it "plays nice with subsequent Raw endpoints" $ do