git.iko.soy/typeable/servant
on master help sign in
fix error status bug in basic auth

Fixes #440.

commit14ff2197268c02cb47b311abae1d4591f1b34704 authorSönke Hahn <soenkehahn@gmail.com> dateWed 6 Apr 2016, 11:16 (11 years ago) parent4224c20
2 files changed+18−5
servant-server/src/Servant/Server/Internal/BasicAuth.hs+2−2 servant-server/test/Servant/ServerSpec.hs+16−3
servant-server/src/Servant/Server/Internal/BasicAuth.hs+2−2
@@ -64,6 +64,6 @@ runBasicAuth req realm (BasicAuthCheck ba) =
6464 Just e -> ba e >>= \res -> case res of
6565 BadPassword -> plzAuthenticate
6666 NoSuchUser -> plzAuthenticate
67− Unauthorized -> return $ Fail err403
67+ Unauthorized -> return $ FailFatal err403
6868 Authorized usr -> return $ Route usr
69− where plzAuthenticate = return $ Fail err401 { errHeaders = [mkBAChallengerHdr realm] }
69+ where plzAuthenticate = return $ FailFatal err401 { errHeaders = [mkBAChallengerHdr realm] }
servant-server/test/Servant/ServerSpec.hs+16−3
@@ -30,6 +30,7 @@ import Network.HTTP.Types (Status (..), hAccept, hContentType,
3030 methodDelete, methodGet,
3131 methodHead, methodPatch,
3232 methodPost, methodPut, ok200,
33+ imATeaPot418,
3334 parseQuery)
3435import Network.Wai (Application, Request, requestHeaders, pathInfo,
3536 queryString, rawQueryString,
@@ -542,13 +543,17 @@ miscCombinatorSpec = with (return $ serve miscApi miscServ) $
542543-- * Basic Authentication {{{
543544------------------------------------------------------------------------------
544545
545−type BasicAuthAPI = BasicAuth "foo" () :> "basic" :> Get '[JSON] Animal
546+type BasicAuthAPI =
547+ BasicAuth "foo" () :> "basic" :> Get '[JSON] Animal
548+ :<|> Raw
546549
547550basicAuthApi :: Proxy BasicAuthAPI
548551basicAuthApi = Proxy
549552
550553basicAuthServer :: Server BasicAuthAPI
551−basicAuthServer = const (return jerry)
554+basicAuthServer =
555+ const (return jerry) :<|>
556+ (\ _ respond -> respond $ responseLBS imATeaPot418 [] "")
552557
553558basicAuthContext :: Context '[ BasicAuthCheck () ]
554559basicAuthContext =
@@ -564,14 +569,22 @@ basicAuthSpec = do
564569 with (return (serveWithContext basicAuthApi basicAuthContext basicAuthServer)) $ do
565570
566571 context "Basic Authentication" $ do
567− it "returns 401 with bad password" $ do
572+ it "returns 401 when no credentials given" $ do
568573 get "/basic" `shouldRespondWith` 401
569574
575+ it "returns 403 when invalid credentials given" $ do
576+ let invalid = [("Authorization", "Basic bbbbbbbbbDpzZXJ2ZXI=")] -- fixme: how do I create basic auth tokens?
577+ THW.request methodGet "/basic" invalid ""
578+ `shouldRespondWith` 403
579+
570580 it "returns 200 with the right password" $ do
571581 let validCredentials = [("Authorization", "Basic c2VydmFudDpzZXJ2ZXI=")]
572582 THW.request methodGet "/basic" validCredentials ""
573583 `shouldRespondWith` 200
574584
585+ it "plays nice with subsequent Raw endpoints" $ do
586+ get "/foo" `shouldRespondWith` 418
587+
575588-- }}}
576589------------------------------------------------------------------------------
577590-- * General Authentication {{{