44{-# LANGUAGE FlexibleContexts #-}
55{-# LANGUAGE FlexibleInstances #-}
66{-# LANGUAGE LambdaCase #-}
7+ {-# LANGUAGE MultiParamTypeClasses #-}
78{-# LANGUAGE RankNTypes #-}
89{-# LANGUAGE ScopedTypeVariables #-}
910{-# LANGUAGE TupleSections #-}
1011{-# LANGUAGE TypeApplications #-}
12+ {-# LANGUAGE TypeFamilies #-}
1113{-# LANGUAGE TypeOperators #-}
14+ {-# LANGUAGE UndecidableInstances #-}
15+ {-# OPTIONS_GHC -Wno-orphans #-}
1216module ObsHelper where
1317
18+ import Control.Monad.Base (MonadBase (liftBase ))
19+ import Control.Monad.Trans.Control
1420import qualified Data.ByteString as BS
1521import qualified Data.ByteString.Base64 as B64
1622import qualified Data.ByteString.Lazy as BL
@@ -22,6 +28,7 @@ import qualified Jose.Jwa as JWT
2228import qualified Jose.Jws as JWT
2329import qualified Jose.Jwt as JWT
2430import Network.HTTP.Types
31+ import Network.Wai.Test
2532import qualified PostgREST.AppState as AppState
2633import PostgREST.Config (AppConfig (.. ),
2734 JSPathExp (.. ),
@@ -36,6 +43,13 @@ import Protolude hiding (get, toS)
3643import System.Timeout (timeout )
3744import Test.Hspec
3845import Test.Hspec.Expectations.Contrib (annotate )
46+ import Test.Hspec.Wai.Internal
47+ import qualified Toxiproxy
48+ import Toxiproxy (proxyEnabled ,
49+ proxyListen ,
50+ proxyName ,
51+ proxyToxics ,
52+ proxyUpstream )
3953
4054-- helpers used to produce observation diagnostics in waitForObs
4155-- Implementing the Show instance for Observation is hard due to having many different parameters so instead we use generic programming (`conName`) to obtain the constructor name as `Text`
@@ -52,10 +66,23 @@ instance (HasConstructor x, HasConstructor y) => HasConstructor (x :+: y) where
5266instance Constructor c => HasConstructor (C1 c f ) where
5367 genericConstrName = T. pack . conName
5468
69+ instance MonadBaseControl IO (WaiSession st ) where
70+ type StM (WaiSession st ) a = StM Session a
71+ liftBaseWith f = WaiSession $
72+ liftBaseWith $ \ runInBase ->
73+ f $ \ k -> runInBase (unWaiSession k)
74+ restoreM = WaiSession . restoreM
75+ {-# INLINE liftBaseWith #-}
76+ {-# INLINE restoreM #-}
77+
78+ instance MonadBase IO (WaiSession st ) where
79+ liftBase = liftIO
80+
5581data SpecState = SpecState {
56- specAppState :: AppState. AppState ,
57- specMetrics :: Metrics. MetricsState ,
58- specObsChan :: ObsChan
82+ specAppState :: AppState. AppState ,
83+ specMetrics :: Metrics. MetricsState ,
84+ specObsChan :: ObsChan ,
85+ specToxiProxy :: Toxiproxy. Proxy
5986}
6087
6188data StateCheck st m = forall a . StateCheck (st -> (String , m a )) (a -> a -> Expectation )
@@ -74,7 +101,7 @@ baseCfg = let secret = encodeUtf8 "reallyreallyreallyreallyverysafe" in
74101 , configClientErrorVerbosity = Verbose
75102 , configDbAggregates = False
76103 , configDbAnonRole = Just " postgrest_test_anonymous"
77- , configDbChannel = mempty
104+ , configDbChannel = " pgrst "
78105 , configDbChannelEnabled = True
79106 , configDbExtraSearchPath = []
80107 , configDbHoistedTxSettings = [" default_transaction_isolation" ," plan_filter.statement_cost_limit" ," statement_timeout" ]
@@ -126,14 +153,27 @@ baseCfg = let secret = encodeUtf8 "reallyreallyreallyreallyverysafe" in
126153testCfg :: AppConfig
127154testCfg = baseCfg
128155
129- testCfgJwtCache :: AppConfig
130- testCfgJwtCache =
131- baseCfg {
156+ testCfgJwtCache :: AppConfig -> AppConfig
157+ testCfgJwtCache base =
158+ base {
132159 configJwtSecret = Just generateSecret
133160 , configJWKS = rightToMaybe $ parseSecret generateSecret
134161 , configJwtCacheMaxEntries = 2
135162 }
136163
164+ testToxiProxy :: Text -> Text -> Text -> Toxiproxy. Proxy
165+ testToxiProxy name proxyPort pgPort = Toxiproxy. Proxy {
166+ proxyName = Toxiproxy. ProxyName name,
167+ proxyEnabled = True ,
168+ proxyToxics = mempty ,
169+ -- we don't create proxies
170+ -- as they are already created
171+ -- but we have to be careful not to override
172+ -- the values
173+ proxyListen = " localhost:" <> proxyPort,
174+ proxyUpstream = " localhost:" <> pgPort
175+ }
176+
137177authHeader :: BS. ByteString -> BS. ByteString -> Header
138178authHeader typ creds =
139179 (hAuthorization, typ <> " " <> creds)
0 commit comments