Skip to content

Commit 4e894d4

Browse files
committed
tests
1 parent 2d3b40d commit 4e894d4

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

servant-server/test/Servant/ServerSpec.hs

+18-5
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ import qualified Servant.Types.SourceT as S
6363
import Test.Hspec
6464
(Spec, context, describe, it, shouldBe, shouldContain)
6565
import Test.Hspec.Wai
66-
(get, liftIO, matchHeaders, matchStatus, shouldRespondWith,
66+
(get, liftIO, matchHeaders, MatchHeader(..), matchStatus, shouldRespondWith,
6767
with, (<:>))
6868
import qualified Test.Hspec.Wai as THW
6969

@@ -742,9 +742,9 @@ basicAuthServer =
742742
const (return jerry) :<|>
743743
(Tagged $ \ _ sendResponse -> sendResponse $ responseLBS imATeapot418 [] "")
744744

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) ->
748748
if usr == "servant" && pass == "server"
749749
then return (Authorized ())
750750
else return Unauthorized
@@ -753,14 +753,27 @@ basicAuthContext =
753753
basicAuthSpec :: Spec
754754
basicAuthSpec = do
755755
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
757767

758768
context "Basic Authentication" $ do
759769
let basicAuthHeaders user password =
760770
[("Authorization", "Basic " <> Base64.encode (user <> ":" <> password))]
761771
it "returns 401 when no credentials given" $ do
762772
get "/basic" `shouldRespondWith` 401
763773

774+
it "returns 401 WWW-Authenticate headers" $ do
775+
get "/basic" `shouldRespondWith` "" {matchStatus = 401, matchHeaders = ["WWW-Authenticate" <:> "Basic realm=\"foo\""]}
776+
764777
it "returns 403 when invalid credentials given" $ do
765778
THW.request methodGet "/basic" (basicAuthHeaders "servant" "wrong") ""
766779
`shouldRespondWith` 403

0 commit comments

Comments
 (0)