Skip to content
This repository has been archived by the owner on Jul 3, 2021. It is now read-only.

Commit

Permalink
Support for AspNetCore 3 (#165)
Browse files Browse the repository at this point in the history
* Support for AspNetCore 3

- Multi-target main lib as netstandard2.0 and netcoreapp3.0
- Use conditional ItemGroup references so netcoreapp3.0 can use FrameworkReference and netstandard can use nuget packages
- Multi-target test lib as netcoreapp2.1 and netcoreapp3.0
- Test lib use newer version of SignalR
  • Loading branch information
damianh committed Oct 1, 2019
1 parent d4ae77a commit 7753480
Show file tree
Hide file tree
Showing 20 changed files with 189 additions and 49 deletions.
15 changes: 15 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
**/bin/
**/obj/
**/global.json
**/Dockerfile*
**/.dockerignore*
**/*.user
**/*.Custom.json
**/*.CustomDotSettings
.idea/
.vs/
.vscode/
.git/
**/node_modules
artifacts/
**/_NCrunch_*
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ language: csharp
mono: none
sudo: false
dist: xenial
dotnet: 2.2
services:
- docker
script: "./build.sh --travis"
git:
depth: false
Expand Down
2 changes: 2 additions & 0 deletions build-local.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo Off
dotnet run --project build -- %*
3 changes: 3 additions & 0 deletions build-local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -euo pipefail
dotnet run --project build -- "$@"
24 changes: 22 additions & 2 deletions build.cmd
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
@echo Off
dotnet run --project build -- %*
@ECHO OFF

docker build ^
-f build.Dockerfile ^
--tag proxykit-build .

if errorlevel 1 (
echo Docker build failed: Exit code is %errorlevel%
exit /b %errorlevel%
)

docker run --rm --name proxykit-build ^
-v %cd%/artifacts:/repo/artifacts ^
-v %cd%/.git:/repo/.git ^
-e MYGET_API_KEY=$MYGET_API_KEY ^
proxykit-build ^
dotnet run -p /repo/build/build.csproj -c Release -- %*

if errorlevel 1 (
echo Docker build failed: Exit code is %errorlevel%
exit /b %errorlevel%
)
29 changes: 29 additions & 0 deletions build.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM mcr.microsoft.com/dotnet/core/sdk:3.0.100-alpine3.9

# Install DotNet Core 2.1
RUN wget -O dotnet.tar.gz https://dotnetcli.blob.core.windows.net/dotnet/Sdk/2.1.802/dotnet-sdk-2.1.802-linux-musl-x64.tar.gz \
&& dotnet_sha512='69fac356dd7ee7445e640326a6eedfe95d93d901437fdb6f30de80cb23274ea645cf172d656e72e5a11be5ebd8022a8d9ef7931e5de59d7521331fcbf51b7c15' \
&& echo "$dotnet_sha512 dotnet.tar.gz" | sha512sum -c - \
&& mkdir -p /usr/share/dotnet \
&& tar -C /usr/share/dotnet -xzf dotnet.tar.gz \
&& rm dotnet.tar.gz

RUN apk add git=2.20.1-r0

WORKDIR /repo

# Copy slns, csprojs and do a dotnet restore
COPY ./build/*.sln ./build/
COPY ./build/*.csproj ./build/
WORKDIR /repo/build
RUN dotnet restore

WORKDIR /repo
COPY ./*.sln ./
COPY ./src/*/*.csproj ./src/
RUN for file in $(ls src/*.csproj); do mkdir -p ./${file%.*}/ && mv $file ./${file%.*}/; done
RUN dotnet restore

# Copy source files
COPY ./build ./build/
COPY ./src ./src/
14 changes: 12 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
#!/usr/bin/env bash
set -euo pipefail
dotnet run --project build -- "$@"

docker build \
--build-arg MYGET_API_KEY=$MYGET_API_KEY \
-f build.dockerfile \
--tag proxykit-build .

docker run --rm --name proxykit-build \
-v $PWD/artifacts:/repo/artifacts \
-v $PWD/.git:/repo/.git \
-e MYGET_API_KEY=$MYGET_API_KEY \
proxykit-build \
dotnet run -p /repo/build/build.csproj -c Release -- "$@"
3 changes: 1 addition & 2 deletions src/Paths/Paths.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp2.1</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<AssemblyName>ProxyKit.Recipe.Simple</AssemblyName>
<RootNamespace>ProxyKit.Recipe.Simple</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand Down
10 changes: 9 additions & 1 deletion src/ProxyKit.Tests/CacheCowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Xunit;
using Xunit.Abstractions;

namespace ProxyKit
{
public class CacheCowTests
{
private readonly ITestOutputHelper _testOutputHelper;

public CacheCowTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}

[Fact]
public async Task Should_return_cached_item()
{
using (var server = RealStartup.BuildKestrelBasedServerOnRandomPort())
using (var server = RealStartup.BuildKestrelBasedServerOnRandomPort(_testOutputHelper))
{
await server.StartAsync();
var port = server.GetServerPort();
Expand Down
36 changes: 23 additions & 13 deletions src/ProxyKit.Tests/EndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Logging;
using ProxyKit.Infra;
using Shouldly;
using Xunit;
using Xunit.Abstractions;

namespace ProxyKit
{
public class EndToEndTests
{
private readonly ITestOutputHelper _testOutputHelper;

public EndToEndTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}

[Fact]
public async Task Can_get_proxied_route()
{
Expand All @@ -33,15 +43,19 @@ public async Task Can_get_proxied_route()
[Fact]
public async Task Responses_from_real_server_are_handled_correctly()
{
using (var server = RealStartup.BuildKestrelBasedServerOnRandomPort())
using (var server = RealStartup.BuildKestrelBasedServerOnRandomPort(_testOutputHelper))
{
await server.StartAsync();
var port = server.GetServerPort();

using (var testServer = new TestServer(new WebHostBuilder()
.UseSetting("port", port.ToString())
.UseSetting("timeout", "4")
.UseStartup<ProxyStartup>()))
.UseSetting("timeout", "2") // 2 seconds
.UseStartup<ProxyStartup>()
.ConfigureLogging(builder =>
{
builder.AddProvider(new XunitLoggerProvider(_testOutputHelper, "Proxy"));
})))
{
var client = testServer.CreateClient();
client.BaseAddress = new Uri("http://example.com:8080");
Expand All @@ -58,14 +72,10 @@ public async Task Responses_from_real_server_are_handled_correctly()
result = await client.GetAsync("/realserver/error");
result.StatusCode.ShouldBe(HttpStatusCode.InternalServerError);

// server timeouts should be returned as gateway timeouts
result = await client.GetAsync("/realserver/slow");
result.StatusCode.ShouldBe(HttpStatusCode.GatewayTimeout);

// server timeouts should be 'delayed'
// server timeouts should return GatewayTimeout
using (var cts = new CancellationTokenSource())
{
cts.CancelAfter(TimeSpan.FromMilliseconds(1000));
cts.CancelAfter(TimeSpan.FromSeconds(4)); // should be longer than timeout
result = await client.GetAsync("/realserver/slow", cts.Token);
result.StatusCode.ShouldBe(HttpStatusCode.GatewayTimeout);
}
Expand All @@ -86,7 +96,7 @@ public async Task Responses_from_real_server_are_handled_correctly()
[Fact]
public async Task When_upstream_host_is_not_running_then_should_get_service_unavailable()
{
using (var server = RealStartup.BuildKestrelBasedServerOnRandomPort())
using (var server = RealStartup.BuildKestrelBasedServerOnRandomPort(_testOutputHelper))
{
await server.StartAsync();
var port = server.GetServerPort();
Expand All @@ -111,7 +121,7 @@ public async Task When_upstream_host_is_not_running_then_should_get_service_unav
[Fact]
public async Task When_upstream_host_is_not_running_and_timeout_is_small_then_operation_cancelled_is_service_unavailable()
{
using (var server = RealStartup.BuildKestrelBasedServerOnRandomPort())
using (var server = RealStartup.BuildKestrelBasedServerOnRandomPort(_testOutputHelper))
{
await server.StartAsync();
var port = server.GetServerPort();
Expand All @@ -132,7 +142,7 @@ public async Task When_upstream_host_is_not_running_and_timeout_is_small_then_op
[Fact]
public async Task Can_proxy_websockets()
{
using (var server = RealStartup.BuildKestrelBasedServerOnRandomPort())
using (var server = RealStartup.BuildKestrelBasedServerOnRandomPort(_testOutputHelper))
{
await server.StartAsync();
var port = server.GetServerPort();
Expand All @@ -155,7 +165,7 @@ public async Task Can_proxy_websockets()
[Fact]
public async Task Can_proxy_websockets_with_request_customization()
{
using (var server = RealStartup.BuildKestrelBasedServerOnRandomPort())
using (var server = RealStartup.BuildKestrelBasedServerOnRandomPort(_testOutputHelper))
{
await server.StartAsync();
var port = server.GetServerPort();
Expand Down
24 changes: 16 additions & 8 deletions src/ProxyKit.Tests/ProxyKit.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.0</TargetFrameworks>
<RootNamespace>ProxyKit</RootNamespace>
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CacheCow.Client" Version="2.4.4" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />
<PackageReference Include="Shouldly" Version="3.0.2" />
<PackageReference Include="xunit.assert" Version="2.4.1" />
<PackageReference Include="xunit.core" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1'">
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.0.4" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="1.0.4" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.1.1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ProxyKit\ProxyKit.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<ProjectConfiguration>
<Settings>
<HiddenComponentWarnings>
<Value>AspNetTestHostCompatibility</Value>
</HiddenComponentWarnings>
</Settings>
</ProjectConfiguration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<ProjectConfiguration>
<Settings>
<HiddenComponentWarnings>
<Value>AspNetTestHostCompatibility</Value>
</HiddenComponentWarnings>
</Settings>
</ProjectConfiguration>
16 changes: 15 additions & 1 deletion src/ProxyKit.Tests/RealStartup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Linq;
using System.Net;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
Expand All @@ -9,6 +11,10 @@
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ProxyKit.Infra;
using Xunit.Abstractions;

#pragma warning disable 1998

namespace ProxyKit
Expand All @@ -21,6 +27,9 @@ public void Configure(IApplicationBuilder app)
{
var options = new ForwardedHeadersOptions();
options.AllowedHosts.Add("*");
options.KnownProxies.Add(IPAddress.Loopback);
options.KnownProxies.Add(IPAddress.IPv6Loopback);
options.KnownProxies.Add(IPAddress.Parse("::ffff:127.0.0.1"));
options.ForwardedHeaders = ForwardedHeaders.All;
app.UseXForwardedHeaders(options);

Expand Down Expand Up @@ -97,12 +106,17 @@ public void Configure(IApplicationBuilder app)
});
}

public static IWebHost BuildKestrelBasedServerOnRandomPort()
public static IWebHost BuildKestrelBasedServerOnRandomPort(ITestOutputHelper testOutputHelper)
{
return new WebHostBuilder()
.UseKestrel()
.UseUrls("http://*:0")
.UseStartup<RealStartup>()
.ConfigureLogging(builder =>
{
builder.AddProvider(new XunitLoggerProvider(testOutputHelper, "Upstream"));
builder.SetMinimumLevel(LogLevel.Debug);
})
.Build();
}

Expand Down
13 changes: 7 additions & 6 deletions src/ProxyKit/ForwardContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,18 @@ internal ForwardContext(
public Task<HttpResponseMessage> Execute() => Send();

/// <summary>
/// Sends the forward request to the upstream host.
/// Sends the upstream request to the upstream host.
/// </summary>
/// <returns>An HttpResponseMessage </returns>
/// <returns>An <see cref="HttpResponseMessage"/> the represents the proxy response.</returns>
public async Task<HttpResponseMessage> Send()
{
try
{
return await _httpClient.SendAsync(
UpstreamRequest,
HttpCompletionOption.ResponseHeadersRead,
HttpContext.RequestAborted)
return await _httpClient
.SendAsync(
UpstreamRequest,
HttpCompletionOption.ResponseHeadersRead,
HttpContext.RequestAborted)
.ConfigureAwait(false);
}
catch (TaskCanceledException ex) when (ex.InnerException is IOException)
Expand Down
Loading

0 comments on commit 7753480

Please sign in to comment.