Skip to content

Commit

Permalink
Specify Allow header as content header in HttpResponse
Browse files Browse the repository at this point in the history
Fixes #80

Signed-off-by: Jon Skeet <[email protected]>
  • Loading branch information
jskeet committed May 21, 2021
1 parent 2988a0c commit e197ef0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/CloudNative.CloudEvents/Http/HttpClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ public static async Task<HttpResponseMessage> HandleAsWebHookValidationRequest(
(request, headerName) => request.Headers.TryGetValues(headerName, out var values) ? values.FirstOrDefault() : null,
validateOrigin, validateRate);

var message = new HttpResponseMessage(statusCode);
// Note: it's a little odd to create an empty ByteArrayContent, but the Allow header is a content header, so we need content.
var message = new HttpResponseMessage(statusCode) { Content = new ByteArrayContent(Array.Empty<byte>()) };
if (allowedOrigin is object)
{
message.Headers.Add("Allow", "POST");
message.Content.Headers.Add("Allow", "POST");
message.Headers.Add("WebHook-Allowed-Origin", allowedOrigin);
if (allowedRate is object)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -60,6 +61,20 @@ public class HttpClientExtensionsTest : HttpTestBase
}
};

[Theory]
[InlineData("validorigin", HttpStatusCode.OK)]
[InlineData("notvalidorigin", HttpStatusCode.MethodNotAllowed)]
public async Task HandleAsWebHookValidationRequest_Simple(string origin, HttpStatusCode expectedResponseCode)
{
var request = new HttpRequestMessage
{
Method = HttpMethod.Options,
Headers = { { "WebHook-Request-Origin", origin } }
};
var response = await request.HandleAsWebHookValidationRequest(actualOrigin => actualOrigin == "validorigin", null);
Assert.Equal(expectedResponseCode, response.StatusCode);
}

[Theory]
[MemberData(nameof(SingleCloudEventMessages))]
public void IsCloudEvent_True(string description, HttpContent content, IDictionary<string, string> headers)
Expand Down

0 comments on commit e197ef0

Please sign in to comment.