diff --git a/test/Fencer/Server/Test.hs b/test/Fencer/Server/Test.hs index e9f4d15..f8466c6 100644 --- a/test/Fencer/Server/Test.hs +++ b/test/Fencer/Server/Test.hs @@ -81,17 +81,6 @@ test_serverResponseRulesNotLoaded = expectError (unknownError "no rate limit configuration loaded") response - where - request :: Proto.RateLimitRequest - request = Proto.RateLimitRequest - { Proto.rateLimitRequestDomain = "domain" - , Proto.rateLimitRequestDescriptors = - fromList $ - [ Proto.RateLimitDescriptor $ - fromList [Proto.RateLimitDescriptor_Entry "key" "value"] - ] - , Proto.rateLimitRequestHitsAddend = 0 - } -- | Test that when Fencer is started and there is no configuration -- directory, requests to Fencer will be responded to with OK. @@ -121,17 +110,6 @@ test_serverResponseNoConfigurationDirectory = -- Test that having a sequence of requests does not -- change the server state replicateM_ 5 communicationRoundtrip - where - request :: Proto.RateLimitRequest - request = Proto.RateLimitRequest - { Proto.rateLimitRequestDomain = "domain" - , Proto.rateLimitRequestDescriptors = - fromList $ - [ Proto.RateLimitDescriptor $ - fromList [Proto.RateLimitDescriptor_Entry "key" "value"] - ] - , Proto.rateLimitRequestHitsAddend = 0 - } -- | Test that when Fencer is started and there are no rules after -- loading (i.e., 'reloadRules' has been ran, but there is no @@ -154,17 +132,6 @@ test_serverResponseNoRules = -- Test that having a sequence of requests does not change the -- server state replicateM_ 5 communicationRoundtrip - where - request :: Proto.RateLimitRequest - request = Proto.RateLimitRequest - { Proto.rateLimitRequestDomain = "domain" - , Proto.rateLimitRequestDescriptors = - fromList $ - [ Proto.RateLimitDescriptor $ - fromList [Proto.RateLimitDescriptor_Entry "key" "value"] - ] - , Proto.rateLimitRequestHitsAddend = 0 - } -- | Test that requests with an empty domain name result in an error. -- @@ -177,7 +144,8 @@ test_serverResponseEmptyDomain = atomically (setRules (serverAppState server) rules) withService server $ \service -> do response <- Proto.rateLimitServiceShouldRateLimit service $ - Grpc.ClientNormalRequest request 1 mempty + Grpc.ClientNormalRequest + (request {Proto.rateLimitRequestDomain = ""}) 1 mempty expectError (unknownError "rate limit domain must not be empty") response @@ -185,17 +153,6 @@ test_serverResponseEmptyDomain = rules :: [(DomainId, RuleTree)] rules = [domainToRuleTree domainDefinitionWithoutRules] - request :: Proto.RateLimitRequest - request = Proto.RateLimitRequest - { Proto.rateLimitRequestDomain = "" - , Proto.rateLimitRequestDescriptors = - fromList $ - [ Proto.RateLimitDescriptor $ - fromList [Proto.RateLimitDescriptor_Entry "key" "value"] - ] - , Proto.rateLimitRequestHitsAddend = 0 - } - -- | Test that requests with an empty descriptor list result in an error. -- -- This behavior matches @lyft/ratelimit@. @@ -207,7 +164,10 @@ test_serverResponseEmptyDescriptorList = atomically (setRules (serverAppState server) rules) withService server $ \service -> do response <- Proto.rateLimitServiceShouldRateLimit service $ - Grpc.ClientNormalRequest request 1 mempty + Grpc.ClientNormalRequest + (request {Proto.rateLimitRequestDescriptors = mempty}) + 1 + mempty expectError (unknownError "rate limit descriptor list must not be empty") response @@ -215,13 +175,6 @@ test_serverResponseEmptyDescriptorList = rules :: [(DomainId, RuleTree)] rules = [domainToRuleTree domainDefinitionWithoutRules] - request :: Proto.RateLimitRequest - request = Proto.RateLimitRequest - { Proto.rateLimitRequestDomain = "domain" - , Proto.rateLimitRequestDescriptors = mempty - , Proto.rateLimitRequestHitsAddend = 0 - } - -- | Test that a request with a non-empty descriptor list results in an -- OK response in presence of a configuration file without read -- permissions. @@ -244,7 +197,7 @@ test_serverResponseReadPermissions = setRules (serverAppState server) (domainToRuleTree <$> rules) withService server $ \service -> do response <- Proto.rateLimitServiceShouldRateLimit service $ - Grpc.ClientNormalRequest request 1 mempty + Grpc.ClientNormalRequest request' 1 mempty expectSuccess (genericOKResponse, Grpc.StatusOk) response @@ -260,15 +213,13 @@ test_serverResponseReadPermissions = domainDescriptorKeyText ] - request :: Proto.RateLimitRequest - request = Proto.RateLimitRequest - { Proto.rateLimitRequestDomain = "domain" - , Proto.rateLimitRequestDescriptors = + request' :: Proto.RateLimitRequest + request' = request + { Proto.rateLimitRequestDescriptors = fromList $ [ Proto.RateLimitDescriptor $ fromList [Proto.RateLimitDescriptor_Entry "key" ""] ] - , Proto.rateLimitRequestHitsAddend = 0 } -- | A parameterized test that checks if a request with a non-empty @@ -300,22 +251,20 @@ test_serverResponseDuplicateDomainOrRule Left _ -> withService server $ \service -> do response <- Proto.rateLimitServiceShouldRateLimit service $ - Grpc.ClientNormalRequest request 1 mempty + Grpc.ClientNormalRequest request' 1 mempty expectError (unknownError "no rate limit configuration loaded") response Right _ -> assertFailure $ "Expected a failure, and got domain definitions instead" where - request :: Proto.RateLimitRequest - request = Proto.RateLimitRequest - { Proto.rateLimitRequestDomain = "domain1" - , Proto.rateLimitRequestDescriptors = + request' :: Proto.RateLimitRequest + request' = request + { Proto.rateLimitRequestDescriptors = fromList $ [ Proto.RateLimitDescriptor $ - fromList [Proto.RateLimitDescriptor_Entry "some key" ""] + fromList [Proto.RateLimitDescriptor_Entry "key" ""] ] - , Proto.rateLimitRequestHitsAddend = 0 } -- | Test that a request with a non-empty descriptor list results in a @@ -507,3 +456,15 @@ genericOKResponse = Proto.RateLimitResponse } , rateLimitResponseHeaders = Vector.empty } + +-- | A generic request useful for testing the server. +request :: Proto.RateLimitRequest +request = Proto.RateLimitRequest + { Proto.rateLimitRequestDomain = "domain" + , Proto.rateLimitRequestDescriptors = + fromList $ + [ Proto.RateLimitDescriptor $ + fromList [Proto.RateLimitDescriptor_Entry "key" "value"] + ] + , Proto.rateLimitRequestHitsAddend = 0 + }