servant/servant-client/test/Servant/Common/BaseUrlSpec.hs

86 lines
2.9 KiB
Haskell
Raw Normal View History

{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Servant.Common.BaseUrlSpec where
2015-04-20 19:52:29 +02:00
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative
2015-04-20 19:52:29 +02:00
#endif
import Control.DeepSeq
import Test.Hspec
import Test.QuickCheck
import Servant.Common.BaseUrl
spec :: Spec
spec = do
2015-10-07 18:01:47 +02:00
let parse = parseBaseUrl :: String -> Maybe BaseUrl
describe "showBaseUrl" $ do
it "shows a BaseUrl" $ do
showBaseUrl (BaseUrl Http "foo.com" 80 "") `shouldBe` "http://foo.com"
it "shows a https BaseUrl" $ do
showBaseUrl (BaseUrl Https "foo.com" 443 "") `shouldBe` "https://foo.com"
it "shows the path of a BaseUrl" $ do
showBaseUrl (BaseUrl Http "foo.com" 80 "api") `shouldBe` "http://foo.com/api"
it "shows the path of an https BaseUrl" $ do
showBaseUrl (BaseUrl Https "foo.com" 443 "api") `shouldBe` "https://foo.com/api"
it "handles leading slashes in path" $ do
showBaseUrl (BaseUrl Https "foo.com" 443 "/api") `shouldBe` "https://foo.com/api"
describe "httpBaseUrl" $ do
it "allows to construct default http BaseUrls" $ do
BaseUrl Http "bar" 80 "" `shouldBe` BaseUrl Http "bar" 80 ""
describe "parseBaseUrl" $ do
it "is total" $ do
property $ \ string ->
2015-10-07 18:01:47 +02:00
deepseq (fmap show (parse string )) True
it "is the inverse of showBaseUrl" $ do
property $ \ baseUrl -> counterexample (showBaseUrl baseUrl) $
parse (showBaseUrl baseUrl) === Just baseUrl
context "trailing slashes" $ do
it "allows trailing slashes" $ do
2015-10-07 18:01:47 +02:00
parse "foo.com/" `shouldBe` Just (BaseUrl Http "foo.com" 80 "")
it "allows trailing slashes in paths" $ do
2015-10-07 18:01:47 +02:00
parse "foo.com/api/" `shouldBe` Just (BaseUrl Http "foo.com" 80 "api")
context "urls without scheme" $ do
it "assumes http" $ do
2015-10-07 18:01:47 +02:00
parse "foo.com" `shouldBe` Just (BaseUrl Http "foo.com" 80 "")
it "allows port numbers" $ do
2015-10-07 18:01:47 +02:00
parse "foo.com:8080" `shouldBe` Just (BaseUrl Http "foo.com" 8080 "")
it "can parse paths" $ do
2015-10-07 18:01:47 +02:00
parse "http://foo.com/api" `shouldBe` Just (BaseUrl Http "foo.com" 80 "api")
it "rejects ftp urls" $ do
parse "ftp://foo.com" `shouldBe` Nothing
instance Arbitrary BaseUrl where
arbitrary = BaseUrl <$>
elements [Http, Https] <*>
hostNameGen <*>
portGen <*>
pathGen
where
letters = ['a' .. 'z'] ++ ['A' .. 'Z']
-- this does not perfectly mirror the url standard, but I hope it's good
-- enough.
hostNameGen = do
first <- elements letters
middle <- listOf1 $ elements (letters ++ ['0' .. '9'] ++ ['.', '-'])
2015-10-07 18:01:47 +02:00
last' <- elements letters
return (first : middle ++ [last'])
portGen = frequency $
(1, return 80) :
(1, return 443) :
(1, choose (1, 20000)) :
[]
pathGen = listOf1 . elements $ letters
isLeft :: Either a b -> Bool
isLeft = either (const True) (const False)