Skip to content

Commit 5cc1c70

Browse files
authored
Propagating Content-Type property from transport to Core (#1666) (#1668)
* Handling Content-Type and other properties * Leaving Content-Type only (cherry picked from commit 67a77d0)
1 parent 1fadcfe commit 5cc1c70

File tree

2 files changed

+52
-94
lines changed

2 files changed

+52
-94
lines changed

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

Lines changed: 45 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -37,130 +37,61 @@ class TestingBasicProperties : IBasicProperties
3737

3838
DeliveryModes IReadOnlyBasicProperties.DeliveryMode => throw new NotImplementedException();
3939

40-
public void ClearAppId()
41-
{
42-
throw new NotSupportedException();
43-
}
40+
public void ClearAppId() => throw new NotSupportedException();
4441

45-
public void ClearClusterId()
46-
{
47-
throw new NotSupportedException();
48-
}
42+
public void ClearClusterId() => throw new NotSupportedException();
4943

50-
public void ClearContentEncoding()
51-
{
52-
throw new NotSupportedException();
53-
}
44+
public void ClearContentEncoding() => throw new NotSupportedException();
5445

55-
public void ClearContentType()
56-
{
57-
throw new NotSupportedException();
58-
}
46+
public void ClearContentType() => throw new NotSupportedException();
5947

60-
public void ClearCorrelationId()
61-
{
62-
throw new NotSupportedException();
63-
}
48+
public void ClearCorrelationId() => throw new NotSupportedException();
6449

65-
public void ClearDeliveryMode()
66-
{
67-
throw new NotSupportedException();
68-
}
50+
public void ClearDeliveryMode() => throw new NotSupportedException();
6951

70-
public void ClearExpiration()
71-
{
72-
throw new NotSupportedException();
73-
}
52+
public void ClearExpiration() => throw new NotSupportedException();
7453

75-
public void ClearHeaders()
76-
{
77-
throw new NotSupportedException();
78-
}
54+
public void ClearHeaders() => throw new NotSupportedException();
7955

80-
public void ClearMessageId()
81-
{
82-
throw new NotSupportedException();
83-
}
56+
public void ClearMessageId() => throw new NotSupportedException();
8457

85-
public void ClearPriority()
86-
{
87-
throw new NotSupportedException();
88-
}
58+
public void ClearPriority() => throw new NotSupportedException();
8959

90-
public void ClearReplyTo()
91-
{
92-
throw new NotSupportedException();
93-
}
60+
public void ClearReplyTo() => throw new NotSupportedException();
9461

95-
public void ClearTimestamp()
96-
{
97-
throw new NotSupportedException();
98-
}
62+
public void ClearTimestamp() => throw new NotSupportedException();
9963

100-
public void ClearType()
101-
{
102-
throw new NotSupportedException();
103-
}
64+
public void ClearType() => throw new NotSupportedException();
10465

105-
public void ClearUserId()
106-
{
107-
throw new NotSupportedException();
108-
}
66+
public void ClearUserId() => throw new NotSupportedException();
10967

110-
public bool IsAppIdPresent()
111-
{
112-
throw new NotSupportedException();
113-
}
68+
public bool IsAppIdPresent() => throw new NotSupportedException();
11469

115-
public bool IsClusterIdPresent()
116-
{
117-
throw new NotSupportedException();
118-
}
70+
public bool IsClusterIdPresent() => throw new NotSupportedException();
11971

120-
public bool IsContentEncodingPresent()
121-
{
122-
throw new NotSupportedException();
123-
}
72+
public bool IsContentEncodingPresent() => throw new NotSupportedException();
12473

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

13076
public bool IsCorrelationIdPresent() => !string.IsNullOrEmpty(CorrelationId);
13177

13278
public bool IsDeliveryModePresent() => DeliveryMode != 0;
13379

134-
public bool IsExpirationPresent()
135-
{
136-
throw new NotSupportedException();
137-
}
80+
public bool IsExpirationPresent() => throw new NotSupportedException();
13881

139-
public bool IsHeadersPresent()
140-
{
141-
throw new NotSupportedException();
142-
}
82+
public bool IsHeadersPresent() => throw new NotSupportedException();
14383

14484
public bool IsMessageIdPresent() => !string.IsNullOrEmpty(MessageId);
14585

146-
public bool IsPriorityPresent()
147-
{
148-
throw new NotSupportedException();
149-
}
86+
public bool IsPriorityPresent() => throw new NotSupportedException();
15087

15188
public bool IsReplyToPresent() => !string.IsNullOrEmpty(ReplyTo);
15289

153-
public bool IsTimestampPresent()
154-
{
155-
throw new NotSupportedException();
156-
}
90+
public bool IsTimestampPresent() => throw new NotSupportedException();
15791

15892
public bool IsTypePresent() => !string.IsNullOrEmpty(Type);
15993

160-
public bool IsUserIdPresent()
161-
{
162-
throw new NotSupportedException();
163-
}
94+
public bool IsUserIdPresent() => throw new NotSupportedException();
16495
}
16596

16697
MessageConverter converter = new MessageConverter(MessageConverter.DefaultMessageIdStrategy);
@@ -401,5 +332,27 @@ public void TestCanHandleTablesListHeader()
401332
Assert.That(Convert.ToString(headers["Foo"]), Is.EqualTo("key1=value1,key2=value2"));
402333
});
403334
}
335+
336+
[Test]
337+
public void Should_handle_content_type()
338+
{
339+
var basicProperties = new TestingBasicProperties
340+
{
341+
MessageId = "Blah",
342+
ContentType = "content_type"
343+
};
344+
345+
var message = new BasicDeliverEventArgs(default, default, default, default, default, basicProperties, default);
346+
347+
var headers = converter.RetrieveHeaders(message);
348+
var messageId = converter.RetrieveMessageId(message, headers);
349+
350+
Assert.Multiple(() =>
351+
{
352+
Assert.That(messageId, Is.Not.Null);
353+
Assert.That(headers, Is.Not.Null);
354+
Assert.That(headers[NServiceBus.Headers.ContentType], Is.EqualTo(basicProperties.ContentType));
355+
});
356+
}
404357
}
405358
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ public Dictionary<string, string> RetrieveHeaders(BasicDeliverEventArgs message)
4444
messageHeaders.Remove(Constants.PublishSequenceNumberHeader);
4545
}
4646

47-
// Leaving space for ReplyTo, CorrelationId, DeliveryMode, EnclosedMessageTypes conditionally
47+
// Leaving space for ReplyTo, CorrelationId, DeliveryMode, EnclosedMessageTypes, ContentType conditionally
4848
// added below. This is a bit cumbersome and need to be changed when things are conditionally added below
4949
// but it prevents the header dictionary from growing and relocating which creates quite a bit of
5050
// memory allocations and eats up CPU cycles.
51-
const int extraCapacity = 4;
51+
const int extraCapacity = 5;
5252
var deserializedHeaders = DeserializeHeaders(messageHeaders, extraCapacity);
5353

5454
if (properties.IsReplyToPresent())
@@ -77,6 +77,11 @@ 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+
8085
//These headers need to be removed so that they won't be copied to an outgoing message if this message gets forwarded
8186
//They can't be removed before deserialization because the value is used by the message pump
8287
deserializedHeaders.Remove("x-delivery-count");

0 commit comments

Comments
 (0)