Skip to content

Commit

Permalink
Handle quotes in version header field. (#283)
Browse files Browse the repository at this point in the history
* Handle quotes in version header field.

* Run dotnet format

* Add comments and fix failing test

Co-authored-by: Florian Grieskamp <florian.grieskamp@gdata.de>
  • Loading branch information
pkemkes and rngcntr authored Aug 3, 2021
1 parent 143aaf5 commit a4e1fa9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
2 changes: 1 addition & 1 deletion shared.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<Version>0.7.1</Version>
<Version>0.7.2</Version>
<LangVersion>9</LangVersion>
<Nullable>enable</Nullable>
<WarningsAsErrors>CS8600;CS8602;CS8625;CS8618;CS8604;CS8601</WarningsAsErrors>
Expand Down
16 changes: 12 additions & 4 deletions src/Motor.Extensions.Hosting.RabbitMQ/BasicPropertiesExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,18 @@ public static MotorCloudEvent<byte[]> ExtractCloudEvent(this IBasicProperties se
return cloudEvent;
}

var hasVersion = attributes.TryGetValue(MotorVersionExtension.MotorVersionAttribute.Name, out var versionObject);
var version = hasVersion && versionObject is byte[] versionBytes
? new Version(Encoding.UTF8.GetString(versionBytes))
: null;
var hasVersion =
attributes.TryGetValue(MotorVersionExtension.MotorVersionAttribute.Name, out var versionObject);
Version? version = null;
if (hasVersion && versionObject is byte[] versionBytes)
{
var versionString = Encoding.UTF8.GetString(versionBytes);
// If the version is enclosed in double quotes, that means it has passed an old Motor.NET version as an
// unknown header. Therefore, the version number should not be trusted and instead, the version is set
// to null.
version = versionString.StartsWith("\"") || versionString.EndsWith("\"") ? null : new Version(
Encoding.UTF8.GetString(versionBytes));
}

foreach (var (key, value) in attributes)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,45 @@ public void UpdateAndExtractCloudEvent_V0_6_0Header_ExtensionsAddedToCloudEvent(
}
}

[Fact]
public void UpdateAndExtractCloudEvent_V0_6_0HeaderWithIncorrectVersionField_ExtensionsAddedToCloudEvent()
{
var channel = _fixture.Connection.CreateModel();
var basicProperties = channel.CreateBasicProperties();
var publisherOptions = new RabbitMQPublisherOptions<byte[]>();
var content = new byte[] { 1, 2, 3 };
var inputCloudEvent = MotorCloudEvent.CreateTestCloudEvent(content);
var mockedApplicationNameService = Mock.Of<IApplicationNameService>();

basicProperties.Update(inputCloudEvent, publisherOptions);
// manipulate basic properties to simulate outdated version
basicProperties.Headers[
$"{BasicPropertiesExtensions.CloudEventPrefix}{MotorVersionExtension.MotorVersionAttribute.Name}"] =
Encoding.UTF8.GetBytes("\"0.7.1.0\"");
basicProperties.ContentEncoding = null;
basicProperties.Headers.Add(
$"{BasicPropertiesExtensions.CloudEventPrefix}{CloudEventsSpecVersion.V1_0.DataContentTypeAttribute.Name}",
Encoding.UTF8.GetBytes($"{basicProperties.ContentType}"));
foreach (var (key, value) in basicProperties.Headers)
{
if (value is byte[] byteValue)
{
basicProperties.Headers[key] = EscapeWithQuotes(byteValue);
}
}

var outputCloudEvent = basicProperties.ExtractCloudEvent(mockedApplicationNameService,
new ReadOnlyMemory<byte>(content));

// expecting all required attributes plus the (incorrect) version attribute
Assert.Equal(MotorCloudEventInfo.RequiredAttributes(Version.Parse("0.6.0.0")).Count() + 1,
outputCloudEvent.GetPopulatedAttributes().Count());
foreach (var requiredAttribute in MotorCloudEventInfo.RequiredAttributes(Version.Parse("0.6.0.0")))
{
Assert.Equal(inputCloudEvent[requiredAttribute], outputCloudEvent[requiredAttribute]);
}
}

private static Version CurrentMotorVersion => typeof(BasicPropertiesExtensionsTest).Assembly.GetName().Version;

private static byte[] EscapeWithQuotes(byte[] value)
Expand Down

0 comments on commit a4e1fa9

Please sign in to comment.