@@ -2,8 +2,67 @@ local RoutingPolicy = require('apicast.policy.routing')
22local UpstreamSelector = require (' apicast.policy.routing.upstream_selector' )
33local Request = require (' apicast.policy.routing.request' )
44local Upstream = require (' apicast.upstream' )
5+ local mapping_rules_matcher = require (' apicast.mapping_rules_matcher' )
56
67describe (' Routing policy' , function ()
8+ describe (' .access' , function ()
9+ it (' assigns route_upstream_usage_cleanup to the context' , function ()
10+ local routing = RoutingPolicy .new ()
11+ local context = {}
12+ routing :access (context )
13+
14+ assert .is_function (context .route_upstream_usage_cleanup )
15+ end )
16+
17+ it (' assigns the same function reference on every call' , function ()
18+ local routing = RoutingPolicy .new ()
19+ local ctx1 = {}
20+ local ctx2 = {}
21+ routing :access (ctx1 )
22+ routing :access (ctx2 )
23+
24+ assert .equals (ctx1 .route_upstream_usage_cleanup , ctx2 .route_upstream_usage_cleanup )
25+ end )
26+ end )
27+
28+ describe (' route_upstream_usage_cleanup' , function ()
29+ it (' is a no-op when route_upstream is nil' , function ()
30+ local routing = RoutingPolicy .new ()
31+ local context = {}
32+ routing :access (context )
33+
34+ assert .has_no_errors (function ()
35+ context :route_upstream_usage_cleanup ({}, {})
36+ end )
37+ end )
38+
39+ it (' calls clean_usage_by_owner_id and merges usage when route_upstream exists' , function ()
40+ local routing = RoutingPolicy .new ()
41+ local context = {}
42+ routing :access (context )
43+
44+ local mock_owner_id = 42
45+ context .route_upstream = {
46+ has_owner_id = function () return mock_owner_id end ,
47+ }
48+
49+ local usage_diff = { some_metric = 1 }
50+ stub (mapping_rules_matcher , ' clean_usage_by_owner_id' ).returns (usage_diff )
51+
52+ local usage = { merge = function () end }
53+ stub (usage , ' merge' )
54+
55+ local matched_rules = { ' rule1' , ' rule2' }
56+
57+ context :route_upstream_usage_cleanup (usage , matched_rules )
58+
59+ assert .stub (mapping_rules_matcher .clean_usage_by_owner_id ).was_called_with (
60+ matched_rules , mock_owner_id
61+ )
62+ assert .stub (usage .merge ).was_called_with (usage , usage_diff )
63+ end )
64+ end )
65+
766 describe (' .content' , function ()
867 describe (' when there is an upstream that matches' , function ()
968 local upstream_that_matches = Upstream .new (' http://localhost' )
0 commit comments