|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE FlexibleContexts #-} |
| 3 | +{-# LANGUAGE FlexibleInstances #-} |
| 4 | +{-# LANGUAGE GADTs #-} |
| 5 | +{-# LANGUAGE GeneralizedNewtypeDeriving #-} |
| 6 | +{-# LANGUAGE InstanceSigs #-} |
| 7 | +{-# LANGUAGE KindSignatures #-} |
| 8 | +{-# LANGUAGE MultiParamTypeClasses #-} |
| 9 | +{-# LANGUAGE RankNTypes #-} |
| 10 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 11 | +{-# LANGUAGE TypeFamilies #-} |
| 12 | +{-# LANGUAGE TypeOperators #-} |
| 13 | + |
| 14 | +-- | 'WaiSession' is limited because its only a 'Reader' of a single 'Application' |
| 15 | +-- This is a pain if you want to test multiple 'Application's that call each other |
| 16 | +-- 'WaiMultiSession' is very similar to 'WaiSession', but it is a 'Reader' of |
| 17 | +-- multiple 'Application's, each of which is tagged at the type level with a 'Symbol' |
| 18 | +-- |
| 19 | +-- This module actually has nothing to do with 'servant', but it is a pain to |
| 20 | +-- use multiple 'Application's without 'hspec-wai-servant's auto client generation |
| 21 | +-- so that's why it's in this library. |
| 22 | +module Test.Hspec.Wai.WaiMultiSession where |
| 23 | + |
| 24 | +import Network.Wai (Application) |
| 25 | +import Network.Wai.Test (runSession) |
| 26 | +import Test.Hspec.Core.Spec |
| 27 | +import Test.Hspec.Wai.Internal (WaiSession (..)) |
| 28 | + |
| 29 | +import Control.Monad.IO.Class (MonadIO) |
| 30 | +import Control.Monad.Reader (ReaderT (..)) |
| 31 | +import Data.Proxy |
| 32 | +import qualified GHC.Exts as GHCX |
| 33 | +import GHC.TypeLits |
| 34 | + |
| 35 | +-- | Run a given 'WaiSession' with the 'Application' tagged with @s@ in the |
| 36 | +-- 'WaiMultiSession' |
| 37 | +-- |
| 38 | +-- NOTE: 'ClientState' is NOT threaded through! It is just a wrapper around |
| 39 | +-- Cookies, so if you aren't using Cookies, you're good. |
| 40 | +-- FIXME: If @wai-extra@ exported 'ClientState', this would be able to be fixed. |
| 41 | +sendWaiSession :: forall (s :: Symbol) (tags :: [Symbol]) a |
| 42 | + . (GetApplication s tags) |
| 43 | + => Proxy s |
| 44 | + -> Proxy tags |
| 45 | + -> WaiSession a |
| 46 | + -> WaiMultiSession tags a |
| 47 | +sendWaiSession p _ (WaiSession session) = |
| 48 | + WaiMultiSession $ ReaderT $ \ma -> |
| 49 | + runSession session (getApplication p ma) |
| 50 | + |
| 51 | +newtype WaiMultiSession (tags :: [Symbol]) a = |
| 52 | + WaiMultiSession { unWaiMultiSession :: (ReaderT (MultiApplication tags) IO a) } |
| 53 | + deriving (Functor, Applicative, Monad, MonadIO) |
| 54 | + |
| 55 | +runWaiMultiSession :: WaiMultiSession tags a -> MultiApplication tags -> IO a |
| 56 | +runWaiMultiSession session app = runReaderT (unWaiMultiSession session) app |
| 57 | + |
| 58 | +type WaiMultiExpectation tags = WaiMultiSession tags () |
| 59 | + |
| 60 | +instance Example (WaiMultiExpectation tags) where |
| 61 | + type Arg (WaiMultiExpectation tags) = MultiApplication tags |
| 62 | + evaluateExample e p action = evaluateExample (action $ runWaiMultiSession e) p ($ ()) |
| 63 | + |
| 64 | +data MultiApplication (tags :: [Symbol]) where |
| 65 | + OneApp :: Application -> MultiApplication '[s] |
| 66 | + ManyApps :: Application -> MultiApplication xs -> MultiApplication (s ': xs) |
| 67 | + |
| 68 | +class GetApplication (s :: Symbol) (tags :: [Symbol]) where |
| 69 | + getApplication :: Proxy s -> MultiApplication tags -> Application |
| 70 | + |
| 71 | +instance forall (s :: Symbol). GetApplication s '[s] where |
| 72 | + getApplication _ (ManyApps _ _) = error "impossible" |
| 73 | + getApplication _ (OneApp app) = app |
| 74 | + |
| 75 | +instance {-# OVERLAPPABLE #-} forall (s :: Symbol) (xs :: [Symbol]). GetApplication s (s ': xs) where |
| 76 | + getApplication _ (ManyApps app _) = app |
| 77 | + getApplication _ (OneApp _) = error "impossible" |
| 78 | + |
| 79 | +instance {-# OVERLAPPABLE #-} forall (s :: Symbol) (xs :: [Symbol]) (x :: Symbol) |
| 80 | + . (GetApplication s xs) |
| 81 | + => GetApplication s (x ': xs) where |
| 82 | + getApplication p (ManyApps _ xs) = getApplication p xs |
| 83 | + getApplication _ (OneApp _) = error "impossible" |
| 84 | + |
| 85 | +instance KnownSymbol s => Show (MultiApplication '[s]) where |
| 86 | + show (OneApp _) = show $ symbolVal (Proxy :: Proxy s) |
| 87 | + show (ManyApps _ _) = error "impossible" |
| 88 | + |
| 89 | +instance {-# OVERLAPPABLE #-} forall (s :: Symbol) (xs :: [Symbol]) |
| 90 | + . (KnownSymbol s, Show (MultiApplication xs)) |
| 91 | + => Show (MultiApplication (s ': xs)) where |
| 92 | + show (ManyApps _ xs) = show (symbolVal (Proxy :: Proxy s)) ++ " : " ++ show xs |
| 93 | + show (OneApp _) = error "impossible" |
| 94 | + |
| 95 | +instance forall (s :: Symbol). GHCX.IsList (MultiApplication '[s]) where |
| 96 | + type Item (MultiApplication '[s]) = Application |
| 97 | + |
| 98 | + fromList [app] = OneApp app |
| 99 | + fromList (_ : _ : _) = error "malformed (too long) MultiApplication OverloadedList!" |
| 100 | + fromList [] = error "malformed (too short) MultiApplication OverloadedList!" |
| 101 | + |
| 102 | + toList (OneApp app) = [app] |
| 103 | + toList (ManyApps _ _) = error "impossible" |
| 104 | + |
| 105 | +instance {-# OVERLAPPABLE #-} forall (s :: Symbol) (xs :: [Symbol]) |
| 106 | + . ( GHCX.IsList (MultiApplication xs) |
| 107 | + , GHCX.Item (MultiApplication xs) ~ Application) |
| 108 | + => GHCX.IsList (MultiApplication (s ': xs)) where |
| 109 | + type Item (MultiApplication (s ': xs)) = Application |
| 110 | + |
| 111 | + fromList (app : xs) = ManyApps app (GHCX.fromList xs :: MultiApplication xs) |
| 112 | + fromList [] = error "malformed (too short) MultiApplication OverloadedList!" |
| 113 | + |
| 114 | + toList (ManyApps app xs) = app : GHCX.toList xs |
| 115 | + toList _ = error "impossible" |
0 commit comments