From 6ff6c8ee64960c7434c37312272dfc2ae45d45dc Mon Sep 17 00:00:00 2001 From: Mateusz Korszun Date: Wed, 13 Nov 2019 09:20:39 +0100 Subject: [PATCH] Give possibility to override all metadata fields (#129) Including `app_id`, `content_type` and `timestamp`. At the moment these values can not be modified by the user. **Implementation details:** Just reverse metadata merging, so custom values can override default ones. --- lib/publisher.ex | 8 ++++---- test/gen_rmq_publisher_test.exs | 13 +++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/publisher.ex b/lib/publisher.ex index e18c3c5..153958a 100644 --- a/lib/publisher.ex +++ b/lib/publisher.ex @@ -334,10 +334,10 @@ defmodule GenRMQ.Publisher do # Put standard metadata fields to top level, everything else into headers defp merge_metadata(base, custom) do {metadata, headers} = custom |> Keyword.split(@metadata_fields) - # take "standard" fields and put them into metadata top-level - metadata - # put default values, override custom values on conflict - |> Keyword.merge(base) + # take "base" fields and put them into metadata top-level + base + # put custom values, override base values on conflict + |> Keyword.merge(metadata) # put all custom fields in the headers |> Keyword.merge(headers: headers) end diff --git a/test/gen_rmq_publisher_test.exs b/test/gen_rmq_publisher_test.exs index f47b99a..5e6fe4d 100644 --- a/test/gen_rmq_publisher_test.exs +++ b/test/gen_rmq_publisher_test.exs @@ -102,6 +102,19 @@ defmodule GenRMQ.PublisherTest do assert [{"header1", :longstr, "value"}] == meta[:headers] end + test "should override default metadata fields", %{publisher: publisher_pid} = context do + message = %{"msg" => "msg"} + sent_metadata = [app_id: "custom_id", content_type: "text/plain", timestamp: 1] + :ok = GenRMQ.Publisher.publish(publisher_pid, Jason.encode!(message), "some.routing.key", sent_metadata) + + Assert.repeatedly(fn -> assert out_queue_count(context) >= 1 end) + {:ok, _received_message, received_metadata} = get_message_from_queue(context) + + assert received_metadata[:app_id] == sent_metadata[:app_id] + assert received_metadata[:content_type] == sent_metadata[:content_type] + assert received_metadata[:timestamp] == sent_metadata[:timestamp] + end + test "should publish a message with priority", %{publisher: publisher_pid} = context do message = %{"msg" => "with prio"}