wai-middleware-delegate 0.1.0.0 → 0.1.1.0
raw patch · 5 files changed
+61/−8 lines, 5 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
+ Network.Wai.Middleware.Delegate: [proxyRedirectCount] :: ProxySettings -> Int
- Network.Wai.Middleware.Delegate: ProxySettings :: SomeException -> Response -> Int -> ByteString -> ProxySettings
+ Network.Wai.Middleware.Delegate: ProxySettings :: SomeException -> Response -> Int -> ByteString -> Int -> ProxySettings
Files
- ChangeLog.md +4/−0
- src/Network/Wai/Middleware/Delegate.hs +6/−2
- test/IntegrationTest.hs +41/−5
- test/Test/TestRequests.hs +9/−0
- wai-middleware-delegate.cabal +1/−1
ChangeLog.md view
@@ -3,3 +3,7 @@ ## 0.1.0.0 -- 2018-07-28 * Initial version.++## 0.1.1.0 -- 2018-08-06++* Add configuration for the number of redirects
src/Network/Wai/Middleware/Delegate.hs view
@@ -2,7 +2,8 @@ {-| Module : Network.Wai.Middleware.Delegate Description :- Provides a Wai middleware that delegates handling of requests.+ Provides a Wai middleware1+that delegates handling of requests. - delegateTo: delegates handling of requests matching a predicate to a delegate Application@@ -87,6 +88,8 @@ , proxyTimeout :: Int -- | The host being proxied , proxyHost :: BS.ByteString+ -- | The number of redirects to follow. 0 means none, which is the default.+ , proxyRedirectCount :: Int } instance Default ProxySettings where@@ -98,6 +101,7 @@ -- default to 15 seconds , proxyTimeout = 15 , proxyHost = "localhost"+ , proxyRedirectCount = 0 } where onException :: SomeException -> Wai.Response@@ -135,7 +139,7 @@ { method = Wai.requestMethod req , requestHeaders = addHostHeader $ filter dropUpstreamHeaders $ Wai.requestHeaders req -- always pass redirects back to the client.- , redirectCount = 0+ , redirectCount = proxyRedirectCount settings , requestBody = case Wai.requestBodyLength req of Wai.ChunkedBody ->
test/IntegrationTest.hs view
@@ -24,6 +24,7 @@ import Test.HttpReply import Test.TestRequests (RequestBuilder (..), buildRequest,+ testOverRedirectedRequests, testNotProxiedRequests, testRequests) import Test.WithExtras (defaultTlsSettings,@@ -32,13 +33,19 @@ defaultTestSettings :: ProxySettings defaultTestSettings = def { proxyHost = "httpbin.org", proxyTimeout = 2 } +redirectTestSettings :: ProxySettings+redirectTestSettings = defaultTestSettings { proxyRedirectCount = 2 }+ main :: IO () main = do dumpDebug' <- lookupEnv "DEBUG" let dumpDebug = maybe False (const True) dumpDebug' hspec $ do+ insecureRedirectTest dumpDebug insecureProxyTest dumpDebug+ insecureNotProxiedTest dumpDebug secureProxyTest dumpDebug+ secureNotProxiedTest dumpDebug defaultTestDelegate :: ProxySettings -> IO Application defaultTestDelegate s = do@@ -52,6 +59,9 @@ testWithInsecureProxy :: (Port -> IO ()) -> IO () testWithInsecureProxy = testWithApplication (defaultTestDelegate defaultTestSettings) +testWithInsecureRedirects :: (Port -> IO ()) -> IO ()+testWithInsecureRedirects = testWithApplication (defaultTestDelegate redirectTestSettings)+ testWithSecureProxy :: (Port -> IO ()) -> IO () testWithSecureProxy = testWithTLSApplication defaultTlsSettings (defaultTestDelegate defaultTestSettings) @@ -81,18 +91,47 @@ print proxied f direct proxied +insecureNotProxiedTest :: Bool -> Spec+insecureNotProxiedTest debug =+ let scheme = "HTTP"+ desc = "Proxy on " ++ scheme ++ " should fail"+ assertNeq = onDirectAndProxy assertHttpRepliesDiffer debug+ in+ around testWithInsecureProxy $ describe desc $ do+ for_ testNotProxiedRequests $ \(title, modifier) ->+ it (scheme ++ " " ++ title) $ \port -> assertNeq port $ modifier def++insecureRedirectTest :: Bool -> Spec+insecureRedirectTest debug =+ let scheme = "HTTP"+ desc = "Proxy over " ++ scheme ++ " with too many redirects differs"+ assertNeq = onDirectAndProxy assertHttpRepliesDiffer debug+ in+ around testWithInsecureRedirects $ describe desc $ do+ for_ testOverRedirectedRequests $ \(title, modifier) ->+ it (scheme ++ " " ++ title) $ \port -> assertNeq port $ modifier def+ insecureProxyTest :: Bool -> Spec insecureProxyTest debug = let scheme = "HTTP" desc = "Simple " ++ scheme ++ " proxying:" assertEq = onDirectAndProxy assertHttpRepliesAreEq debug- assertNeq = onDirectAndProxy assertHttpRepliesDiffer debug in around testWithInsecureProxy $ describe desc $ do for_ testRequests $ \(title, modifier) -> it (scheme ++ " " ++ title) $ \port -> assertEq port $ modifier def++secureNotProxiedTest :: Bool -> Spec+secureNotProxiedTest debug =+ let+ scheme = "HTTPS"+ desc = "Proxy on " ++ scheme ++ " should fail"+ assertNeq = onDirectAndProxy assertHttpRepliesDiffer debug+ def' = def { rbSecure = True }+ in+ around testWithSecureProxy $ describe desc $ do for_ testNotProxiedRequests $ \(title, modifier) ->- it (scheme ++ " " ++ title) $ \port -> assertNeq port $ modifier def+ it (scheme ++ " " ++ title) $ \port -> assertNeq port $ modifier def' secureProxyTest :: Bool -> Spec secureProxyTest debug =@@ -100,11 +139,8 @@ scheme = "HTTPS" desc = "Simple " ++ scheme ++ " proxying:" assertEq = onDirectAndProxy assertHttpRepliesAreEq debug- assertNeq = onDirectAndProxy assertHttpRepliesDiffer debug def' = def { rbSecure = True } in around testWithSecureProxy $ describe desc $ do for_ testRequests $ \(title, modifier) -> it (scheme ++ " " ++ title) $ \port -> assertEq port $ modifier def'- for_ testNotProxiedRequests $ \(title, modifier) ->- it (scheme ++ " " ++ title) $ \port -> assertNeq port $ modifier def'
test/Test/TestRequests.hs view
@@ -8,6 +8,7 @@ , testGetRequests , testPostRequests , testNotProxiedRequests+ , testOverRedirectedRequests ) where import qualified Data.ByteString as BS@@ -49,6 +50,8 @@ , (\builder -> builder {rbPath = "/get"})) , ("GET (with a query)" , (\builder -> builder {rbPath = "/get?a=10&b=whatever"}))+ , ("GET (multiple redirects)"+ , (\builder -> builder {rbPath = "/redirect/3"})) , ("GET (with a body)" , (\builder -> builder { rbPath = "/get"@@ -64,6 +67,12 @@ testNotProxiedRequests = [ ("GET (funny resource - differs on proxy)" , (\builder -> builder {rbPath = "/status/418"}))+ ]++testOverRedirectedRequests :: [(String, RequestBuilder -> RequestBuilder)]+testOverRedirectedRequests =+ [ ("GET (multiple redirects)"+ , (\builder -> builder {rbPath = "/redirect/3"})) ] testPostRequests :: [(String, RequestBuilder -> RequestBuilder)]
wai-middleware-delegate.cabal view
@@ -1,5 +1,5 @@ name: wai-middleware-delegate-version: 0.1.0.0+version: 0.1.1.0 synopsis: WAI middleware that delegates handling of requests. description: WAI middleware to intercept requests that match a predicate and respond to them using other WAI Applications or proxied hosts. [WAI]