Skip to content

Commit 9cb6901

Browse files
committed
Handling Content-Type and other properties
1 parent f726131 commit 9cb6901

5 files changed

Lines changed: 68 additions & 20 deletions

File tree

src/NServiceBus.Transport.RabbitMQ.Tests/MessageConverterTests.cs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -107,34 +107,22 @@ public void ClearUserId()
107107
throw new NotSupportedException();
108108
}
109109

110-
public bool IsAppIdPresent()
111-
{
112-
throw new NotSupportedException();
113-
}
110+
public bool IsAppIdPresent() => AppId != null;
114111

115112
public bool IsClusterIdPresent()
116113
{
117114
throw new NotSupportedException();
118115
}
119116

120-
public bool IsContentEncodingPresent()
121-
{
122-
throw new NotSupportedException();
123-
}
117+
public bool IsContentEncodingPresent() => ContentEncoding != null;
124118

125-
public bool IsContentTypePresent()
126-
{
127-
throw new NotSupportedException();
128-
}
119+
public bool IsContentTypePresent() => ContentType != null;
129120

130121
public bool IsCorrelationIdPresent() => !string.IsNullOrEmpty(CorrelationId);
131122

132123
public bool IsDeliveryModePresent() => DeliveryMode != 0;
133124

134-
public bool IsExpirationPresent()
135-
{
136-
throw new NotSupportedException();
137-
}
125+
public bool IsExpirationPresent() => Expiration != null;
138126

139127
public bool IsHeadersPresent()
140128
{
@@ -157,10 +145,7 @@ public bool IsTimestampPresent()
157145

158146
public bool IsTypePresent() => !string.IsNullOrEmpty(Type);
159147

160-
public bool IsUserIdPresent()
161-
{
162-
throw new NotSupportedException();
163-
}
148+
public bool IsUserIdPresent() => UserId != null;
164149
}
165150

166151
MessageConverter converter = new MessageConverter(MessageConverter.DefaultMessageIdStrategy);
@@ -401,5 +386,29 @@ public void TestCanHandleTablesListHeader()
401386
Assert.That(Convert.ToString(headers["Foo"]), Is.EqualTo("key1=value1,key2=value2"));
402387
});
403388
}
389+
390+
[Test]
391+
public void Should_handle_basic_properties()
392+
{
393+
var basicProperties = new TestingBasicProperties
394+
{
395+
MessageId = "Blah",
396+
ContentType = "content_type",
397+
ContentEncoding = "content_encoding"
398+
};
399+
400+
var message = new BasicDeliverEventArgs(default, default, default, default, default, basicProperties, default);
401+
402+
var headers = converter.RetrieveHeaders(message);
403+
var messageId = converter.RetrieveMessageId(message, headers);
404+
405+
Assert.Multiple(() =>
406+
{
407+
Assert.That(messageId, Is.Not.Null);
408+
Assert.That(headers, Is.Not.Null);
409+
Assert.That(headers[NServiceBus.Headers.ContentType], Is.EqualTo(basicProperties.ContentType));
410+
Assert.That(headers[PropertiesToHeaderMapping.ContentEncoding], Is.EqualTo(basicProperties.ContentEncoding));
411+
});
412+
}
404413
}
405414
}

src/NServiceBus.Transport.RabbitMQ.Tests/When_sending_a_message_over_rabbitmq.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ public Task Should_transmit_all_transportMessage_headers()
110110
});
111111
}
112112

113+
[Test]
114+
public Task Should_set_the_content_encoding()
115+
{
116+
return Verify(new OutgoingMessageBuilder().WithHeader(PropertiesToHeaderMapping.ContentEncoding, "content-encoding"), received => Assert.That(received.BasicProperties.ContentEncoding, Is.EqualTo("content-encoding")));
117+
}
118+
113119
async Task Verify(OutgoingMessageBuilder builder, Action<IncomingMessage, BasicDeliverEventArgs> assertion, CancellationToken cancellationToken = default)
114120
{
115121
var operations = builder.SendTo(QueueToReceiveOn).Build();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace NServiceBus
2+
{
3+
static class PropertiesToHeaderMapping
4+
{
5+
public const string AppId = "NServiceBus.Transport.RabbitMQ.AppId";
6+
public const string ContentEncoding = "NServiceBus.Transport.RabbitMQ.ContentEncoding";
7+
}
8+
}

src/NServiceBus.Transport.RabbitMQ/Receiving/MessageConverter.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ public Dictionary<string, string> RetrieveHeaders(BasicDeliverEventArgs message)
7777
deserializedHeaders[Headers.EnclosedMessageTypes] = properties.Type;
7878
}
7979

80+
if (properties.IsContentTypePresent())
81+
{
82+
deserializedHeaders[Headers.ContentType] = properties.ContentType;
83+
}
84+
85+
if (properties.IsAppIdPresent())
86+
{
87+
deserializedHeaders[PropertiesToHeaderMapping.AppId] = properties.AppId;
88+
}
89+
90+
if (properties.IsContentEncodingPresent())
91+
{
92+
deserializedHeaders[PropertiesToHeaderMapping.ContentEncoding] = properties.ContentEncoding;
93+
}
94+
8095
//These headers need to be removed so that they won't be copied to an outgoing message if this message gets forwarded
8196
//They can't be removed before deserialization because the value is used by the message pump
8297
deserializedHeaders.Remove("x-delivery-count");

src/NServiceBus.Transport.RabbitMQ/Sending/BasicPropertiesExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ public static void Fill(this IBasicProperties properties, OutgoingMessage messag
7070
{
7171
properties.ReplyTo = replyToAddress;
7272
}
73+
74+
if (messageHeaders.TryGetValue(PropertiesToHeaderMapping.AppId, out var appId) && appId != null)
75+
{
76+
properties.AppId = appId;
77+
}
78+
79+
if (messageHeaders.TryGetValue(PropertiesToHeaderMapping.ContentEncoding, out var contentEncoding) && contentEncoding != null)
80+
{
81+
properties.ContentEncoding = contentEncoding;
82+
}
7383
}
7484

7585
static bool CalculateDelay(DispatchProperties dispatchProperties, out long delay)

0 commit comments

Comments
 (0)