@@ -62,6 +62,7 @@ spec = describe "Servant.Client" $ do
6262 sucessSpec
6363 failSpec
6464 wrappedApiSpec
65+ basicAuthSpec
6566
6667-- * test data types
6768
@@ -148,6 +149,29 @@ failServer = serve failApi (
148149 :<|> (\_request respond -> respond $ responseLBS ok200 [("content-type", "fooooo")] "")
149150 )
150151
152+
153+-- * auth stuff
154+
155+type BasicAuthAPI =
156+ BasicAuth "foo-realm" () :> "private" :> "basic" :> Get '[JSON] Person
157+
158+basicAuthAPI :: Proxy BasicAuthAPI
159+basicAuthAPI = Proxy
160+
161+basicAuthHandler :: BasicAuthCheck ()
162+basicAuthHandler =
163+ let check (BasicAuthData username password) =
164+ if username == "servant" && password == "server"
165+ then return (Authorized ())
166+ else return Unauthorized
167+ in BasicAuthCheck check
168+
169+serverConfig :: Config '[ BasicAuthCheck () ]
170+serverConfig = basicAuthHandler :. EmptyConfig
171+
172+basicAuthServer :: Application
173+basicAuthServer = serve basicAuthAPI serverConfig (const (return alice))
174+
151175{-# NOINLINE manager #-}
152176manager :: C.Manager
153177manager = unsafePerformIO $ C.newManager C.defaultManagerSettings
@@ -292,6 +316,22 @@ data WrappedApi where
292316 HasClient api, Client api ~ ExceptT ServantError IO ()) =>
293317 Proxy api -> WrappedApi
294318
319+basicAuthSpec :: Spec
320+basicAuthSpec = beforeAll (startWaiApp basicAuthServer) $ afterAll endWaiApp $ do
321+ context "Authentication works when requests are properly authenticated" $ do
322+
323+ it "Authenticates a BasicAuth protected server appropriately" $ \(_,baseUrl) -> do
324+ let getBasic = client basicAuthAPI baseUrl manager
325+ let basicAuthData = BasicAuthData "servant" "server"
326+ (left show <$> runExceptT (getBasic basicAuthData)) `shouldReturn` Right alice
327+
328+ context "Authentication is rejected when requests are not authenticated properly" $ do
329+
330+ it "Authenticates a BasicAuth protected server appropriately" $ \(_,baseUrl) -> do
331+ let getBasic = client basicAuthAPI baseUrl manager
332+ let basicAuthData = BasicAuthData "not" "password"
333+ Left FailureResponse{..} <- runExceptT (getBasic basicAuthData)
334+ responseStatus `shouldBe` Status 403 "Forbidden"
295335
296336-- * utils
297337