Skip to content

Commit

Permalink
Issue #26: factor out a common request in server tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Marko Dimjašević committed Nov 26, 2019
1 parent 1acf201 commit 6eeaab4
Showing 1 changed file with 27 additions and 66 deletions.
93 changes: 27 additions & 66 deletions test/Fencer/Server/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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.
--
Expand All @@ -177,25 +144,15 @@ 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
where
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@.
Expand All @@ -207,21 +164,17 @@ 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
where
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.
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 6eeaab4

Please sign in to comment.