|
| 1 | +{-# LANGUAGE CPP #-} |
| 2 | +{-# LANGUAGE ConstraintKinds #-} |
| 3 | +{-# LANGUAGE DataKinds #-} |
| 4 | +{-# LANGUAGE FlexibleContexts #-} |
| 5 | +{-# LANGUAGE FlexibleInstances #-} |
| 6 | +{-# LANGUAGE GADTs #-} |
| 7 | +{-# LANGUAGE MultiParamTypeClasses #-} |
| 8 | +{-# LANGUAGE OverloadedStrings #-} |
| 9 | +{-# LANGUAGE PolyKinds #-} |
| 10 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 11 | +{-# LANGUAGE TypeFamilies #-} |
| 12 | +{-# LANGUAGE TypeOperators #-} |
| 13 | +{-# LANGUAGE UndecidableInstances #-} |
| 14 | +{-# OPTIONS_GHC -freduction-depth=100 #-} |
| 15 | +{-# OPTIONS_GHC -fno-warn-orphans #-} |
| 16 | +{-# OPTIONS_GHC -fno-warn-name-shadowing #-} |
| 17 | + |
| 18 | +module Servant.BasicAuthSpec (spec) where |
| 19 | + |
| 20 | +import Prelude () |
| 21 | +import Prelude.Compat |
| 22 | + |
| 23 | +import Control.Arrow |
| 24 | + (left) |
| 25 | +import Data.Monoid () |
| 26 | +import qualified Network.HTTP.Types as HTTP |
| 27 | +import Test.Hspec |
| 28 | + |
| 29 | +import Servant.API |
| 30 | + (BasicAuthData (..)) |
| 31 | +import Servant.Client |
| 32 | +import Servant.ClientTestUtils |
| 33 | + |
| 34 | +spec :: Spec |
| 35 | +spec = describe "Servant.BasicAuthSpec" $ do |
| 36 | + basicAuthSpec |
| 37 | + |
| 38 | +basicAuthSpec :: Spec |
| 39 | +basicAuthSpec = beforeAll (startWaiApp basicAuthServer) $ afterAll endWaiApp $ do |
| 40 | + context "Authentication works when requests are properly authenticated" $ do |
| 41 | + |
| 42 | + it "Authenticates a BasicAuth protected server appropriately" $ \(_,baseUrl) -> do |
| 43 | + let getBasic = client basicAuthAPI |
| 44 | + let basicAuthData = BasicAuthData "servant" "server" |
| 45 | + left show <$> runClient (getBasic basicAuthData) baseUrl `shouldReturn` Right alice |
| 46 | + |
| 47 | + context "Authentication is rejected when requests are not authenticated properly" $ do |
| 48 | + |
| 49 | + it "Authenticates a BasicAuth protected server appropriately" $ \(_,baseUrl) -> do |
| 50 | + let getBasic = client basicAuthAPI |
| 51 | + let basicAuthData = BasicAuthData "not" "password" |
| 52 | + Left (FailureResponse _ r) <- runClient (getBasic basicAuthData) baseUrl |
| 53 | + responseStatusCode r `shouldBe` HTTP.Status 403 "Forbidden" |
0 commit comments