1 {-# LANGUAGE NoMonomorphismRestriction #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# LANGUAGE InstanceSigs #-}
4 module Hspec.Server.Router where
6 import Control.Monad (when)
7 import Data.Eq (Eq(..))
9 import Data.Maybe (Maybe(..))
10 import Data.Ord (Ord(..))
11 import Data.String (String, IsString(..))
13 import System.IO (IO, putStrLn)
14 import Test.Hspec.Wai (liftIO)
15 import Text.Show (Show(..), showString, showParen)
16 import qualified Data.ByteString as BS
17 import qualified Data.Text.Lazy as TL
18 import qualified Data.Text.Lazy.Encoding as TL
19 import qualified Data.ByteString.Base64 as BS64
20 import qualified Network.HTTP.Types as HTTP
21 import qualified Network.Wai.Handler.Warp as Warp
22 import qualified Test.Hspec.Wai as Wai
23 import qualified Data.Map.Strict as Map
24 import qualified Control.Monad.Classes as MC
26 import Control.Arrow (first)
27 import Data.Function (const)
29 import qualified Data.Map.Merge.Strict as Map
30 import qualified Data.List as List
33 import Symantic.HTTP.Server
38 "a" </> "b" </> "b" </> end
39 <!> "a" </> "b" </> "c" </> end
42 <!> "a" </> "c" </> end
45 "a" </> "aa" </> get @String @'[PlainText]
47 "b" </> "bb" </> get @Int @'[PlainText]
49 "c" </> "cc" </> get @Int @'[PlainText]
51 "a" </> "AA" </> get @String @'[PlainText]
53 "b" </> "bb" </> get @Int @'[PlainText]
56 end = get @String @'[PlainText]
58 "a" </> "b" </> "c" </> end
60 "a" </> "b" </> "c" </> end
61 <!> "b" </> "a" </> "c" </> end
63 "a" </> "b" </> "c" </> end
64 <!> "b" </> "a" </> "c" </> end
65 <!> "a" </> "c" </> "b" </> end
67 "a" </> "b" </> "c" </> end
68 <!> "b" </> "a" </> "c" </> end
69 <!> "a" </> "c" </> "b" </> end
70 <!> "c" </> "a" </> "b" </> end
72 "a" </> "b" </> "c" </> end
73 <!> "b" </> "a" </> "c" </> end
74 <!> "a" </> "c" </> "b" </> end
75 <!> "c" </> "a" </> "b" </> end
76 <!> "b" </> "c" </> "a" </> end
77 <!> "c" </> "b" </> "a" </> end
78 <!> "a" </> "a" </> "b" </> end
81 "a" </> ("b" </> "c" </> end
82 <!> "c" </> "b" </> end
83 <!> "a" </> "b" </> end)
84 <!> "b" </> ("a" </> "c" </> end
85 <!> "c" </> "a" </> end)
86 <!> "c" </> ("a" </> "b" </> end
87 <!> "b" </> "a" </> end)
97 MC.exec $ putStrLn "/a/aa"
100 MC.exec $ putStrLn "/b/bb"
103 MC.exec $ putStrLn "/c/cc"
106 MC.exec $ putStrLn "/a/AA"
109 MC.exec $ putStrLn "/b/bb'"
113 warp = Warp.run 8080 srv
115 instance Show (Router Server a b) where
122 showsPrec p (Map.toList ms)
144 r = show (router @Server api)
146 hspec = testSpecs $ describe "Router" $ Wai.with (return srv) $ do
148 it "call the right route" $ do
150 `Wai.shouldRespondWith` 200 { Wai.matchBody = mkBody $ fromString "0" }
151 it "call the right route" $ do
153 `Wai.shouldRespondWith` 200 { Wai.matchBody = mkBody $ fromString "3" }
155 mkBody b = Wai.MatchBody $ \_ b' ->
158 else Just $ TL.unpack $
159 "expecting: "<>TL.decodeUtf8 b<>
160 " but got: "<>TL.decodeUtf8 b'<>"\n"