From f132fb9e4a7de7339c2644c601a4443e81092b40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christina=20Dahl=C3=A9n?= <85930202+kickster97@users.noreply.github.com> Date: Fri, 24 Nov 2023 11:46:37 +0100 Subject: [PATCH] filter out unsupported policies (#600) * filter out unsupported policies * add spec --- spec/policies_spec.cr | 17 +++++++++++++++++ src/lavinmq/policy.cr | 10 +++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/spec/policies_spec.cr b/spec/policies_spec.cr index 268a65a8ab..5ad7f97b42 100644 --- a/spec/policies_spec.cr +++ b/spec/policies_spec.cr @@ -4,6 +4,7 @@ describe LavinMQ::VHost do definitions = { "max-length" => JSON::Any.new(10_i64), "alternate-exchange" => JSON::Any.new("dead-letters"), + "unsupported" => JSON::Any.new("unsupported"), } vhost = Server.vhosts.create("add_policy") @@ -212,6 +213,22 @@ describe LavinMQ::VHost do ch.queue_declare(q.name, passive: true)[:message_count].should eq 2 end end + + it "effective_policy_definition should not include unsupported policies" do + supported_policies = {"max-length", "max-length-bytes", + "message-ttl", "expires", "overflow", + "dead-letter-exchange", "dead-letter-routing-key", + "federation-upstream", "federation-upstream-set", + "delivery-limit", "max-age", "alternate-exchange", + "delayed-message"} + vhost.queues["test"] = LavinMQ::Queue.new(vhost, "test") + vhost.add_policy("test", "^.*$", "all", definitions, -10_i8) + sleep 0.01 + vhost.queues["test"].details_tuple[:effective_policy_definition].as(Hash(String, JSON::Any)).each do |k, v| + supported_policies.includes?(k).should be_true + end + vhost.delete_policy("test") + end end describe "together with arguments" do diff --git a/src/lavinmq/policy.cr b/src/lavinmq/policy.cr index 7ffb073880..5c1b04a065 100644 --- a/src/lavinmq/policy.cr +++ b/src/lavinmq/policy.cr @@ -10,6 +10,10 @@ module LavinMQ end class Policy + SUPPORTED_POLICIES = {"max-length", "max-length-bytes", "message-ttl", "expires", "overflow", + "dead-letter-exchange", "dead-letter-routing-key", "federation-upstream", + "federation-upstream-set", "delivery-limit", "max-age", + "alternate-exchange", "delayed-message"} enum Target All Queues @@ -67,15 +71,15 @@ module LavinMQ merged[k] = v end end - merged + merged.select { |key, _| SUPPORTED_POLICIES.includes?(key) } end def self.merge_definitions(p1 : Nil, p2 : Policy) : Hash(String, JSON::Any) - p2.definition + p2.definition.select { |key, _| SUPPORTED_POLICIES.includes?(key) } end def self.merge_definitions(p1 : Policy, p2 : Nil) : Hash(String, JSON::Any) - p1.definition + p1.definition.select { |key, _| SUPPORTED_POLICIES.includes?(key) } end def self.merge_definitions(p1 : Nil, p2 : Nil) : Hash(String, JSON::Any)