Skip to content

Commit f0b124f

Browse files
authored
Propagating Content-Type property from transport to Core (#1666) (#1669)
* Handling Content-Type and other properties * Leaving Content-Type only (cherry picked from commit 67a77d0) # Conflicts: # src/NServiceBus.Transport.RabbitMQ.Tests/MessageConverterTests.cs
1 parent 00c1b0b commit f0b124f

File tree

2 files changed

+59
-94
lines changed

2 files changed

+59
-94
lines changed

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

Lines changed: 52 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -33,130 +33,61 @@ class TestingBasicProperties : IBasicProperties
3333

3434
public string ProtocolClassName => throw new NotSupportedException();
3535

36-
public void ClearAppId()
37-
{
38-
throw new NotSupportedException();
39-
}
36+
public void ClearAppId() => throw new NotSupportedException();
4037

41-
public void ClearClusterId()
42-
{
43-
throw new NotSupportedException();
44-
}
38+
public void ClearClusterId() => throw new NotSupportedException();
4539

46-
public void ClearContentEncoding()
47-
{
48-
throw new NotSupportedException();
49-
}
40+
public void ClearContentEncoding() => throw new NotSupportedException();
5041

51-
public void ClearContentType()
52-
{
53-
throw new NotSupportedException();
54-
}
42+
public void ClearContentType() => throw new NotSupportedException();
5543

56-
public void ClearCorrelationId()
57-
{
58-
throw new NotSupportedException();
59-
}
44+
public void ClearCorrelationId() => throw new NotSupportedException();
6045

61-
public void ClearDeliveryMode()
62-
{
63-
throw new NotSupportedException();
64-
}
46+
public void ClearDeliveryMode() => throw new NotSupportedException();
6547

66-
public void ClearExpiration()
67-
{
68-
throw new NotSupportedException();
69-
}
48+
public void ClearExpiration() => throw new NotSupportedException();
7049

71-
public void ClearHeaders()
72-
{
73-
throw new NotSupportedException();
74-
}
50+
public void ClearHeaders() => throw new NotSupportedException();
7551

76-
public void ClearMessageId()
77-
{
78-
throw new NotSupportedException();
79-
}
52+
public void ClearMessageId() => throw new NotSupportedException();
8053

81-
public void ClearPriority()
82-
{
83-
throw new NotSupportedException();
84-
}
54+
public void ClearPriority() => throw new NotSupportedException();
8555

86-
public void ClearReplyTo()
87-
{
88-
throw new NotSupportedException();
89-
}
56+
public void ClearReplyTo() => throw new NotSupportedException();
9057

91-
public void ClearTimestamp()
92-
{
93-
throw new NotSupportedException();
94-
}
58+
public void ClearTimestamp() => throw new NotSupportedException();
9559

96-
public void ClearType()
97-
{
98-
throw new NotSupportedException();
99-
}
60+
public void ClearType() => throw new NotSupportedException();
10061

101-
public void ClearUserId()
102-
{
103-
throw new NotSupportedException();
104-
}
62+
public void ClearUserId() => throw new NotSupportedException();
10563

106-
public bool IsAppIdPresent()
107-
{
108-
throw new NotSupportedException();
109-
}
64+
public bool IsAppIdPresent() => throw new NotSupportedException();
11065

111-
public bool IsClusterIdPresent()
112-
{
113-
throw new NotSupportedException();
114-
}
66+
public bool IsClusterIdPresent() => throw new NotSupportedException();
11567

116-
public bool IsContentEncodingPresent()
117-
{
118-
throw new NotSupportedException();
119-
}
68+
public bool IsContentEncodingPresent() => throw new NotSupportedException();
12069

121-
public bool IsContentTypePresent()
122-
{
123-
throw new NotSupportedException();
124-
}
70+
public bool IsContentTypePresent() => ContentType != null;
12571

12672
public bool IsCorrelationIdPresent() => !string.IsNullOrEmpty(CorrelationId);
12773

12874
public bool IsDeliveryModePresent() => DeliveryMode != 0;
12975

130-
public bool IsExpirationPresent()
131-
{
132-
throw new NotSupportedException();
133-
}
76+
public bool IsExpirationPresent() => throw new NotSupportedException();
13477

135-
public bool IsHeadersPresent()
136-
{
137-
throw new NotSupportedException();
138-
}
78+
public bool IsHeadersPresent() => throw new NotSupportedException();
13979

14080
public bool IsMessageIdPresent() => !string.IsNullOrEmpty(MessageId);
14181

142-
public bool IsPriorityPresent()
143-
{
144-
throw new NotSupportedException();
145-
}
82+
public bool IsPriorityPresent() => throw new NotSupportedException();
14683

14784
public bool IsReplyToPresent() => !string.IsNullOrEmpty(ReplyTo);
14885

149-
public bool IsTimestampPresent()
150-
{
151-
throw new NotSupportedException();
152-
}
86+
public bool IsTimestampPresent() => throw new NotSupportedException();
15387

15488
public bool IsTypePresent() => !string.IsNullOrEmpty(Type);
15589

156-
public bool IsUserIdPresent()
157-
{
158-
throw new NotSupportedException();
159-
}
90+
public bool IsUserIdPresent() => throw new NotSupportedException();
16091
}
16192

16293
MessageConverter converter = new MessageConverter(MessageConverter.DefaultMessageIdStrategy);
@@ -403,5 +334,34 @@ public void TestCanHandleTablesListHeader()
403334
Assert.NotNull(headers);
404335
Assert.AreEqual("key1=value1,key2=value2", Convert.ToString(headers["Foo"]));
405336
}
337+
338+
[Test]
339+
public void Should_handle_content_type()
340+
{
341+
var basicProperties = new TestingBasicProperties
342+
{
343+
MessageId = "Blah",
344+
ContentType = "content_type"
345+
};
346+
347+
var message = new BasicDeliverEventArgs
348+
{
349+
BasicProperties = new TestingBasicProperties
350+
{
351+
MessageId = "Blah",
352+
ContentType = "content_type"
353+
}
354+
};
355+
356+
var headers = converter.RetrieveHeaders(message);
357+
var messageId = converter.RetrieveMessageId(message, headers);
358+
359+
Assert.Multiple(() =>
360+
{
361+
Assert.That(messageId, Is.Not.Null);
362+
Assert.That(headers, Is.Not.Null);
363+
Assert.That(headers[NServiceBus.Headers.ContentType], Is.EqualTo(basicProperties.ContentType));
364+
});
365+
}
406366
}
407367
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ public Dictionary<string, string> RetrieveHeaders(BasicDeliverEventArgs message)
3939
messageHeaders.Remove(BasicPropertiesExtensions.ConfirmationIdHeader);
4040
}
4141

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

4949
if (properties.IsReplyToPresent())
@@ -72,6 +72,11 @@ public Dictionary<string, string> RetrieveHeaders(BasicDeliverEventArgs message)
7272
deserializedHeaders[Headers.EnclosedMessageTypes] = properties.Type;
7373
}
7474

75+
if (properties.IsContentTypePresent())
76+
{
77+
deserializedHeaders[Headers.ContentType] = properties.ContentType;
78+
}
79+
7580
//These headers need to be removed so that they won't be copied to an outgoing message if this message gets forwarded
7681
//They can't be removed before deserialization because the value is used by the message pump
7782
deserializedHeaders.Remove("x-delivery-count");

0 commit comments

Comments
 (0)