Skip to content

Commit

Permalink
Fixes #136 - filter location header properly (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronpowell authored May 23, 2024
1 parent 2f2fb6f commit 733ac3d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
40 changes: 38 additions & 2 deletions src/Teapot.Web.Tests/IntegrationTests/CustomHeaderTests.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
using Microsoft.AspNetCore.Mvc.Testing;
using System.Net.Http;

namespace Teapot.Web.Tests.IntegrationTests;

public class CustomHeaderTests
{
[OneTimeSetUp]
public void OneTimeSetUp()
{
_httpClient = new WebApplicationFactory<Program>().CreateDefaultClient();
}

[OneTimeTearDown]
public void OneTimeTearDown()
{
_httpClient.Dispose();
}

private HttpClient _httpClient = null!;

[Test]
public async Task CanSetCustomHeaders()
{
HttpClient httpClient = new WebApplicationFactory<Program>().CreateDefaultClient();
string uri = "/200";
string headerName = "Foo";
string headerValue = "bar";

using HttpRequestMessage request = new(HttpMethod.Get, uri);
request.Headers.Add($"{StatusExtensions.CUSTOM_RESPONSE_HEADER_PREFIX}{headerName}", headerValue);

using HttpResponseMessage response = await httpClient.SendAsync(request);
using HttpResponseMessage response = await _httpClient.SendAsync(request);

System.Net.Http.Headers.HttpResponseHeaders headers = response.Headers;
Assert.Multiple(() =>
Expand All @@ -28,4 +41,27 @@ public async Task CanSetCustomHeaders()
Assert.That(values!.First(), Is.EqualTo(headerValue));
});
}

[Test]
public async Task Redirects302ToCorrectLocation()
{
string uri = "/302";
string headerName = "Location";
string headerValue = "example.com";

using HttpRequestMessage request = new(HttpMethod.Get, uri);
request.Headers.Add($"{StatusExtensions.CUSTOM_RESPONSE_HEADER_PREFIX}{headerName}", headerValue);

using var response = await _httpClient.SendAsync(request);

var headers = response.Headers;
Assert.Multiple(() =>
{
Assert.That(headers.Contains(headerName), Is.True);
Assert.That(headers.TryGetValues(headerName, out var values), Is.True);
Assert.That(values, Is.Not.Null);
Assert.That(values!.Count(), Is.EqualTo(1));
Assert.That(values!.First(), Is.EqualTo(headerValue));
});
}
}
11 changes: 10 additions & 1 deletion src/Teapot.Web/CustomHttpStatusCodeResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Net.Http.Headers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Teapot.Web.Models;
Expand All @@ -19,6 +20,7 @@ public class CustomHttpStatusCodeResult(
{
private const int SLEEP_MIN = 0;
private const int SLEEP_MAX = 5 * 60 * 1000; // 5 mins in milliseconds
private static readonly string[] onlySingleHeader = ["Location"];

private static readonly MediaTypeHeaderValue jsonMimeType = new("application/json");

Expand Down Expand Up @@ -51,7 +53,14 @@ public async Task ExecuteAsync(HttpContext context)

foreach ((string header, StringValues values) in customResponseHeaders)
{
context.Response.Headers.Append(header, values);
if (onlySingleHeader.Contains(header))
{
context.Response.Headers[header] = values;
}
else
{
context.Response.Headers.Append(header, values);
}
}

if (metadata.ExcludeBody || suppressBody == true)
Expand Down

0 comments on commit 733ac3d

Please sign in to comment.