Merge pull request #39 from TobbenTM/fix-http-header-value-mapping

Now URL-encoding and decoding header values
This commit is contained in:
Clemens Vasters 2020-01-15 10:03:14 +01:00 committed by GitHub
commit 0959b118e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 6 deletions

View File

@ -9,6 +9,7 @@ namespace CloudNative.CloudEvents
using Newtonsoft.Json; using Newtonsoft.Json;
using System; using System;
using System.IO; using System.IO;
using System.Net;
using System.Net.Mime; using System.Net.Mime;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -106,7 +107,7 @@ namespace CloudNative.CloudEvents
if (httpRequestHeader.StartsWith(HttpHeaderPrefix, StringComparison.InvariantCultureIgnoreCase)) if (httpRequestHeader.StartsWith(HttpHeaderPrefix, StringComparison.InvariantCultureIgnoreCase))
{ {
string headerValue = httpRequest.Headers[httpRequestHeader]; string headerValue = WebUtility.UrlDecode(httpRequest.Headers[httpRequestHeader]);
// maps in headers have been abolished in 1.0 // maps in headers have been abolished in 1.0
if (version != CloudEventsSpecVersion.V1_0 && if (version != CloudEventsSpecVersion.V1_0 &&
headerValue.StartsWith("{") && headerValue.EndsWith("}") || headerValue.StartsWith("{") && headerValue.EndsWith("}") ||

View File

@ -84,7 +84,7 @@ namespace CloudNative.CloudEvents
{ {
if (attribute.Value is string) if (attribute.Value is string)
{ {
Headers.Add("ce-" + attribute.Key, attribute.Value.ToString()); Headers.Add("ce-" + attribute.Key, WebUtility.UrlEncode(attribute.Value.ToString()));
} }
else if (attribute.Value is DateTime) else if (attribute.Value is DateTime)
{ {
@ -97,8 +97,9 @@ namespace CloudNative.CloudEvents
else else
{ {
Headers.Add("ce-" + attribute.Key, Headers.Add("ce-" + attribute.Key,
Encoding.UTF8.GetString(jsonFormatter.EncodeAttribute(cloudEvent.SpecVersion, attribute.Key, attribute.Value, WebUtility.UrlEncode(
cloudEvent.Extensions.Values))); Encoding.UTF8.GetString(jsonFormatter.EncodeAttribute(cloudEvent.SpecVersion, attribute.Key, attribute.Value,
cloudEvent.Extensions.Values))));
} }
} }
} }

View File

@ -29,7 +29,7 @@ namespace CloudNative.CloudEvents.IntegrationTests.AspNetCore
// Arrange // Arrange
var expectedExtensionKey = "comexampleextension1"; var expectedExtensionKey = "comexampleextension1";
var expectedExtensionValue = Guid.NewGuid().ToString(); var expectedExtensionValue = Guid.NewGuid().ToString();
var cloudEvent = new CloudEvent("test-type", new Uri("urn:integration-tests")) var cloudEvent = new CloudEvent("test-type-æøå", new Uri("urn:integration-tests"))
{ {
Id = Guid.NewGuid().ToString(), Id = Guid.NewGuid().ToString(),
}; };
@ -45,6 +45,7 @@ namespace CloudNative.CloudEvents.IntegrationTests.AspNetCore
// Assert // Assert
Assert.Equal(HttpStatusCode.OK, result.StatusCode); Assert.Equal(HttpStatusCode.OK, result.StatusCode);
Assert.Contains(cloudEvent.Id, await result.Content.ReadAsStringAsync()); Assert.Contains(cloudEvent.Id, await result.Content.ReadAsStringAsync());
Assert.Contains(cloudEvent.Type, await result.Content.ReadAsStringAsync());
Assert.Contains($"\"{expectedExtensionKey}\":\"{expectedExtensionValue}\"", await result.Content.ReadAsStringAsync()); Assert.Contains($"\"{expectedExtensionKey}\":\"{expectedExtensionValue}\"", await result.Content.ReadAsStringAsync());
} }
} }

View File

@ -276,7 +276,8 @@ namespace CloudNative.CloudEvents.UnitTests
var attrs = cloudEvent.GetAttributes(); var attrs = cloudEvent.GetAttributes();
attrs["comexampleextension1"] = "value"; attrs["comexampleextension1"] = "value";
attrs["utf8examplevalue"] = "æøå";
string ctx = Guid.NewGuid().ToString(); string ctx = Guid.NewGuid().ToString();
var content = new CloudEventContent(cloudEvent, ContentMode.Structured, new JsonEventFormatter()); var content = new CloudEventContent(cloudEvent, ContentMode.Structured, new JsonEventFormatter());
content.Headers.Add(testContextHeader, ctx); content.Headers.Add(testContextHeader, ctx);
@ -298,6 +299,8 @@ namespace CloudNative.CloudEvents.UnitTests
var attr = receivedCloudEvent.GetAttributes(); var attr = receivedCloudEvent.GetAttributes();
Assert.Equal("value", (string)attr["comexampleextension1"]); Assert.Equal("value", (string)attr["comexampleextension1"]);
Assert.Equal("%C3%A6%C3%B8%C3%A5", content.Headers.Single(h => h.Key == "ce-utf8examplevalue").Value.Single());
Assert.Equal("æøå", (string)attr["utf8examplevalue"]);
context.Response.StatusCode = (int)HttpStatusCode.NoContent; context.Response.StatusCode = (int)HttpStatusCode.NoContent;
} }
catch (Exception e) catch (Exception e)