Skip to content

Commit

Permalink
EdgeHub: Fix Mqtt address parsing (#76)
Browse files Browse the repository at this point in the history
* Make / at end of topics optional

* Make / at end of topics optional

* Fix log messages
  • Loading branch information
varunpuranik authored Jul 27, 2018
1 parent 0366784 commit b19bbb4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.

namespace Microsoft.Azure.Devices.Edge.Hub.Mqtt
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,13 @@ public bool TryParseProtocolMessagePropsFromAddress(IProtocolGatewayMessage mess

if (matches.Count > 1)
{
this.logger.LogWarning($"Topic name {message.Address} matches more than one route. Picking first matching route.");
this.logger.LogWarning($"Message address {message.Address} matches more than one topic. Picking first matching topic.");
}
else if (matches.Count == 1)
else if (matches.Count == 0)
{
this.logger.LogWarning($"Message address {message.Address} does not match any topic.");
}
else
{
foreach (KeyValuePair<string, string> match in matches[0])
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
"devices/{deviceId}/messages/events/",
"devices/{deviceId}/modules/{moduleId}/messages/events/{params}/",
"devices/{deviceId}/modules/{moduleId}/messages/events/",
"devices/{deviceId}/messages/events/{params}",
"devices/{deviceId}/messages/events",
"devices/{deviceId}/modules/{moduleId}/messages/events/{params}",
"devices/{deviceId}/modules/{moduleId}/messages/events",
"$iothub/methods/res/{statusCode}/?$rid={correlationId}"
],
"OutboundTemplates": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,5 +499,34 @@ public void TestTryParseAddressWithParamsIntoMessagePropertiesFailsNoMatchMultip
Assert.False(status);
Assert.Equal(0, message.Properties.Count);
}

[Fact]
public void TestTryParseAddressWithTailingSlashOptional()
{
IList<string> input = new List<string> { "a/{b}/c/{d}", "a/{b}/c/{d}/" };
var config = new MessageAddressConversionConfiguration(
input,
DontCareOutput
);
var converter = new MessageAddressConverter(config);

string address = "a/bee/c/dee";
ProtocolGatewayMessage message = new ProtocolGatewayMessage.Builder(Payload, address)
.Build();
bool status = converter.TryParseProtocolMessagePropsFromAddress(message);
Assert.True(status);
Assert.Equal(2, message.Properties.Count);
Assert.Equal("bee", message.Properties["b"]);
Assert.Equal("dee", message.Properties["d"]);

string address2 = "a/bee/c/dee/";
ProtocolGatewayMessage message2 = new ProtocolGatewayMessage.Builder(Payload, address2)
.Build();
bool status2 = converter.TryParseProtocolMessagePropsFromAddress(message2);
Assert.True(status2);
Assert.Equal(2, message2.Properties.Count);
Assert.Equal("bee", message2.Properties["b"]);
Assert.Equal("dee", message2.Properties["d"]);
}
}
}

0 comments on commit b19bbb4

Please sign in to comment.