Skip to content

Commit

Permalink
feat: adjusts default header behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
abxsantos committed Jan 22, 2025
1 parent ef16753 commit da8eb6d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
10 changes: 6 additions & 4 deletions django_outbox_pattern/headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ def generate_headers(message):


def get_message_headers(published):
if not published.headers:
default_headers = import_string(settings.DEFAULT_GENERATE_HEADERS)
return json.loads(json.dumps(default_headers(published), cls=DjangoJSONEncoder))
return published.headers
default_headers = import_string(settings.DEFAULT_GENERATE_HEADERS)(published)
return json.loads(
json.dumps(
default_headers if not published.headers else published.headers | default_headers, cls=DjangoJSONEncoder
)
)
2 changes: 1 addition & 1 deletion tests/unit/test_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from stomp.exception import StompException

from django_outbox_pattern.choices import StatusChoice
from django_outbox_pattern.consumers import _get_or_create_correlation_id, Consumer
from django_outbox_pattern.consumers import _get_or_create_correlation_id
from django_outbox_pattern.factories import factory_consumer
from django_outbox_pattern.payloads import Payload

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ def test_published_should_add_correlation_id_header_from_current_request_id(self
published = Published.objects.create(destination="destination", body={"message": "Message test"})
self.assertEqual(published.headers["dop-correlation-id"], request_id)

def test_published_should_not_add_correlation_id_given_custom_header(self):
def test_published_should_add_correlation_id_given_custom_header(self):
request_id = str(uuid4())
local_threading.request_id = request_id
published = Published.objects.create(
destination="destination", body={"message": "Message test"}, headers={"custom": "xpto-lalala"}
)
self.assertNotIn("dop-correlation-id", published.headers)
self.assertIn("dop-correlation-id", published.headers)
2 changes: 1 addition & 1 deletion tests/unit/test_producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_producer_send_with_header(self):
self.producer.stop()
self.assertEqual(self.producer.connection.send.call_count, 1)
self.assertTrue(published.headers is not None)
self.assertEqual(published.headers, headers)
self.assertEqual(published.headers["key"], headers["key"])

def test_producer_send_event(self):
self.producer.start()
Expand Down

0 comments on commit da8eb6d

Please sign in to comment.