Skip to content

Commit

Permalink
refactored basic auth test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
soenkehahn committed Apr 6, 2016
1 parent 14ff219 commit 6df3429
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions servant-server/servant-server.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ test-suite spec
base == 4.*
, base-compat
, aeson
, base64-bytestring
, bytestring
, bytestring-conversion
, directory
Expand Down
10 changes: 6 additions & 4 deletions servant-server/test/Servant/ServerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6df3429

Please sign in to comment.