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

Make RequestTransformContext.DestinationPrefix read-write #2311

Merged
merged 3 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion src/ReverseProxy/Transforms/RequestTransformContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ public QueryTransformContext Query
/// <summary>
/// The URI prefix for the proxy request. This includes the scheme and host and can optionally include a
/// port and path base. The 'Path' and 'Query' properties will be appended to this after the transforms have run.
/// Changing this value can have side effects on load balancing and health checks.
/// </summary>
public string DestinationPrefix { get; init; } = default!;
public string DestinationPrefix { get; set; } = default!;

/// <summary>
/// A <see cref="CancellationToken"/> indicating that the request is being aborted.
Expand Down
3 changes: 3 additions & 0 deletions test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,12 @@
httpContext.Response.Body = proxyResponseStream;

var destinationPrefix = "https://localhost:123/a/b/";
Uri? originalRequestUri = null;

Check failure on line 156 in test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci (Build macOS latest)

test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs#L156

test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs(156,12): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 156 in test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci (Build macOS latest)

test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs#L156

test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs(156,12): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 156 in test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci (Build macOS latest)

test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs#L156

test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs(156,12): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 156 in test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci (Build Ubuntu)

test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs#L156

test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs(156,12): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 156 in test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci (Build Ubuntu)

test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs#L156

test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs(156,12): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 156 in test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci (Build Ubuntu)

test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs#L156

test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs(156,12): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 156 in test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci

test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs#L156

test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs(156,12): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 156 in test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci

test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs#L156

test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs(156,12): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 156 in test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci

test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs#L156

test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs(156,12): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 156 in test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci

test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs#L156

test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs(156,12): error CS8632: (NETCORE_ENGINEERING_TELEMETRY=Build) The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
var transforms = new DelegateHttpTransforms()
{
OnRequest = (context, request, destination) =>
{
originalRequestUri = request.RequestUri;
request.RequestUri = new Uri(destination + "prefix"
+ context.Request.Path + context.Request.QueryString);
request.Headers.Remove("transformHeader");
Expand Down Expand Up @@ -214,6 +216,7 @@

var proxyError = await sut.SendAsync(httpContext, destinationPrefix, client, ForwarderRequestConfig.Empty, transforms);

Assert.Null(originalRequestUri); // Should only be set by the transformer
Assert.Equal(ForwarderError.None, proxyError);
Assert.Equal(234, httpContext.Response.StatusCode);
var reasonPhrase = httpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase;
Expand Down
23 changes: 23 additions & 0 deletions test/ReverseProxy.Tests/Forwarder/HttpTransformerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.Net.Http.Headers;
using Xunit;
using Yarp.ReverseProxy.Transforms;
using Yarp.ReverseProxy.Transforms.Builder;
using Yarp.ReverseProxy.Transforms.Builder.Tests;
using Yarp.Tests.Common;

Expand Down Expand Up @@ -116,6 +117,28 @@ public async Task TransformRequestAsync_ContentLengthAndTransferEncoding_Content
Assert.False(proxyRequest.Headers.TryGetValues(HeaderNames.TransferEncoding, out var _));
}

[Fact]
public async Task TransformRequestAsync_SetDestinationPrefix()
{
const string updatedDestinationPrefix = "https://contoso.com";
var transformer = TransformBuilderTests.CreateTransformBuilder().CreateInternal(context =>
{
context.AddRequestTransform(transformContext =>
{
transformContext.DestinationPrefix = updatedDestinationPrefix;
return ValueTask.CompletedTask;
});
});
var httpContext = new DefaultHttpContext();
var proxyRequest = new HttpRequestMessage(HttpMethod.Get, requestUri: (string)null)
{
Content = new ByteArrayContent(Array.Empty<byte>()),
};

await transformer.TransformRequestAsync(httpContext, proxyRequest, "https://localhost", CancellationToken.None);
Assert.Equal(new Uri(updatedDestinationPrefix), proxyRequest.RequestUri);
}

[Theory]
[InlineData(HttpStatusCode.Continue)]
[InlineData(HttpStatusCode.SwitchingProtocols)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Threading.Tasks;
using Xunit;

namespace Yarp.ReverseProxy.Transforms.Tests;

public class DestinationPrefixTransformTests
{
[Fact]
public async Task UpdateDestinationPrefix()
benjaminpetit marked this conversation as resolved.
Show resolved Hide resolved
{
const string newDestinationPrefix = "http://localhost:8080";
var context = new RequestTransformContext()
{
DestinationPrefix = "http://contoso.com:5000"
};
var transform = new DestinationPrefixTransform(newDestinationPrefix);
await transform.ApplyAsync(context);
}

private class DestinationPrefixTransform(string newDestinationPrefix) : RequestTransform
{
public override ValueTask ApplyAsync(RequestTransformContext context)
{
context.DestinationPrefix = newDestinationPrefix;
return ValueTask.CompletedTask;
}
}
}
Loading