Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing location header not respecting custom value #141

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading