Skip to content

Commit 68179ce

Browse files
author
DanielePalaia
committed
adding a test when both publisher address and message address are set
1 parent ce91405 commit 68179ce

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

rabbitmq_amqp_python_client/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
StreamOptions,
1313
)
1414
from .environment import Environment
15-
from .exceptions import ArgumentOutOfRangeException
15+
from .exceptions import (
16+
ArgumentOutOfRangeException,
17+
ValidationCodeException,
18+
)
1619
from .management import Management
1720
from .publisher import Publisher
1821
from .qpid.proton._data import symbol # noqa: E402
@@ -62,6 +65,7 @@
6265
"AddressHelper",
6366
"AMQPMessagingHandler",
6467
"ArgumentOutOfRangeException",
68+
"ValidationCodeException",
6569
"SslConfigurationContext",
6670
"ClientCert",
6771
"ConnectionClosed",

rabbitmq_amqp_python_client/publisher.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
from typing import Optional
33

44
from .address_helper import validate_address
5-
from .exceptions import ArgumentOutOfRangeException
5+
from .exceptions import (
6+
ArgumentOutOfRangeException,
7+
ValidationCodeException,
8+
)
69
from .options import SenderOptionUnseattle
710
from .qpid.proton._delivery import Delivery
811
from .qpid.proton._message import Message
@@ -27,6 +30,11 @@ def _open(self) -> None:
2730
self._sender = self._create_sender(self._addr)
2831

2932
def publish(self, message: Message) -> Delivery:
33+
if (self._addr != "") and (message.address is not None):
34+
raise ValidationCodeException(
35+
"address specified in both message and publisher"
36+
)
37+
3038
if self._addr != "":
3139
if self._sender is not None:
3240
return self._sender.send(message)

tests/test_publisher.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
OutcomeState,
1111
QuorumQueueSpecification,
1212
StreamSpecification,
13+
ValidationCodeException,
1314
)
1415

1516
from .http_requests import delete_all_connections
@@ -160,6 +161,32 @@ def test_publish_per_message_to_invalid_destination(connection: Connection) -> N
160161
assert raised is True
161162

162163

164+
def test_publish_per_message_both_address(connection: Connection) -> None:
165+
166+
queue_name = "test-queue-1"
167+
raised = False
168+
169+
management = connection.management()
170+
management.declare_queue(QuorumQueueSpecification(name=queue_name))
171+
172+
message = Message(body="test")
173+
message = AddressHelper.message_to_address_helper(message, "/queues/" + queue_name)
174+
publisher = connection.publisher("/queues/" + queue_name)
175+
176+
try:
177+
publisher.publish(message)
178+
except ValidationCodeException:
179+
raised = True
180+
181+
if publisher is not None:
182+
publisher.close()
183+
184+
management.delete_queue(queue_name)
185+
management.close()
186+
187+
assert raised is True
188+
189+
163190
def test_publish_exchange(connection: Connection) -> None:
164191

165192
exchange_name = "test-exchange"

0 commit comments

Comments
 (0)