@@ -63,7 +63,7 @@ import qualified Servant.Types.SourceT as S
63
63
import Test.Hspec
64
64
(Spec , context , describe , it , shouldBe , shouldContain )
65
65
import Test.Hspec.Wai
66
- (get , liftIO , matchHeaders , matchStatus , shouldRespondWith ,
66
+ (get , liftIO , matchHeaders , MatchHeader ( .. ), matchStatus , shouldRespondWith ,
67
67
with , (<:>) )
68
68
import qualified Test.Hspec.Wai as THW
69
69
@@ -742,9 +742,9 @@ basicAuthServer =
742
742
const (return jerry) :<|>
743
743
(Tagged $ \ _ sendResponse -> sendResponse $ responseLBS imATeapot418 [] " " )
744
744
745
- basicAuthContext :: Context '[ BasicAuthCheck () ]
746
- basicAuthContext =
747
- let basicHandler = BasicAuthCheck True $ \ (BasicAuthData usr pass) ->
745
+ basicAuthContext :: Bool -> Context '[ BasicAuthCheck () ]
746
+ basicAuthContext withRealm =
747
+ let basicHandler = BasicAuthCheck withRealm $ \ (BasicAuthData usr pass) ->
748
748
if usr == " servant" && pass == " server"
749
749
then return (Authorized () )
750
750
else return Unauthorized
@@ -753,14 +753,27 @@ basicAuthContext =
753
753
basicAuthSpec :: Spec
754
754
basicAuthSpec = do
755
755
describe " Servant.API.BasicAuth" $ do
756
- with (return (serveWithContext basicAuthApi basicAuthContext basicAuthServer)) $ do
756
+ with (return (serveWithContext basicAuthApi (basicAuthContext False ) basicAuthServer)) $ do
757
+ context " Basic Authentication without realm" $ do
758
+ it " does not send WWW-Authenticate headers on 401" $ do
759
+ let noWWW =
760
+ MatchHeader $ \ headers _ ->
761
+ if " WWW-Authenticate" `elem` map fst headers
762
+ then Just " WWW-Authenticate header is unexpected, "
763
+ else Nothing
764
+ get " /basic" `shouldRespondWith` " " {matchStatus = 401 , matchHeaders = [noWWW]}
765
+
766
+ with (return (serveWithContext basicAuthApi (basicAuthContext True ) basicAuthServer)) $ do
757
767
758
768
context " Basic Authentication" $ do
759
769
let basicAuthHeaders user password =
760
770
[(" Authorization" , " Basic " <> Base64. encode (user <> " :" <> password))]
761
771
it " returns 401 when no credentials given" $ do
762
772
get " /basic" `shouldRespondWith` 401
763
773
774
+ it " returns 401 WWW-Authenticate headers" $ do
775
+ get " /basic" `shouldRespondWith` " " {matchStatus = 401 , matchHeaders = [" WWW-Authenticate" <:> " Basic realm=\" foo\" " ]}
776
+
764
777
it " returns 403 when invalid credentials given" $ do
765
778
THW. request methodGet " /basic" (basicAuthHeaders " servant" " wrong" ) " "
766
779
`shouldRespondWith` 403
0 commit comments