Skip to content
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
10 changes: 5 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ jobs:
uses: rocklan/dotnet-tests-report@master
with:
project_path: .\src\scissors.sln
report_name: http_request_interceptor_test_report
report_title: HttpRequestInterceptor Tests Report
report_name: scissors_test_report
report_title: Scissors Tests Report
github_token: ${{ secrets.GITHUB_TOKEN }}
gist_name: http_request_interceptor_tests.md
gist_name: scissors_tests.md
gist_badge_label: 'Tests passed: %Counters_passed%/%Counters_total%'
gist_token: ${{ secrets.GIST_TOKEN }}
set_check_status_from_test_outcome: true
fail_build_on_failed_tests: true
extra_test_parameters: '--configuration release --collect:"XPlat Code Coverage" --settings .\src\HttpRequestInterceptor.Test\runsettings.xml'
extra_test_parameters: '--configuration release --collect:"XPlat Code Coverage" --settings .\src\Scissors.Test\runsettings.xml'

- name: Sonar End
env:
Expand All @@ -76,6 +76,6 @@ jobs:
if: github.ref == 'refs/heads/branch-zero'
shell: pwsh
run: |
cd src\HttpRequestInterceptor
cd src\Scissors
dotnet pack --configuration Release
dotnet nuget push ".\bin\Release\*.nupkg" --source "https://nuget.pkg.github.com/MirzaMerdovic/index.json" --api-key "${{ secrets.SCISSORS_PAT }}"
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [1.0.1]
### Changes
- Switched from Nuget package source to GitHub
- Switched from Nuget package source to GitHub
- Renamed project to Scissors.HttpRequestInterceptor.*

## [2.0.0]
### Changes
- Changed namespace from Scissors.HttpRequestInterceptor to Scissors
- Renamed RequestsInterceptor to HttpRequestInterceptor
- Renamed HttpInterceptorOptions to HttpRequestInterceptorOptions
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Collections.Generic;

using static Scissors.HttpRequestInterceptor.Test.InMemoryConfigurationWriter;
using static Scissors.Test.InMemoryConfigurationWriter;

namespace Scissors.HttpRequestInterceptor.Test.ComplexRoutes
namespace Scissors.Test.ComplexRoutes
{
public sealed class ComplexRouteFixture : TestFixture
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Threading.Tasks;
using Xunit;

namespace Scissors.HttpRequestInterceptor.Test.ComplexRoutes
namespace Scissors.Test.ComplexRoutes
{
public sealed class ComplexRouteTests : IClassFixture<ComplexRouteFixture>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using static Scissors.HttpRequestInterceptor.Test.InMemoryConfigurationWriter;
using static Scissors.Test.InMemoryConfigurationWriter;

namespace Scissors.HttpRequestInterceptor.Test.HostRoutes
namespace Scissors.Test.HostRoutes
{
public sealed class HostRouteFixture : TestFixture
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Threading.Tasks;
using Xunit;

namespace Scissors.HttpRequestInterceptor.Test.HostRoutes
namespace Scissors.Test.HostRoutes
{
public class HostRouteTests : IClassFixture<HostRouteFixture>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Collections.Generic;

namespace Scissors.HttpRequestInterceptor.Test
namespace Scissors.Test
{
internal static class InMemoryConfigurationWriter
{
private const string Column = ":";
private const string Headers = "Headers";
private const string SectionName = nameof(HttpInterceptorOptions) + Column;
private const string SectionName = nameof(HttpRequestInterceptorOptions) + Column;

// HttpInterceptorOptions:0:
private static string BasePath(int index) => $"{SectionName}{index}{Column}";
Expand All @@ -16,34 +16,34 @@ internal static class InMemoryConfigurationWriter

// HttpInterceptorOptions:0:MethodName
public static KeyValuePair<string, string> BuildMethodNamePair(int index, string value) =>
new($"{BasePath(index)}{nameof(HttpInterceptorOptions.MethodName)}", value);
new($"{BasePath(index)}{nameof(HttpRequestInterceptorOptions.MethodName)}", value);

// HttpInterceptorOptions:0:Host
public static KeyValuePair<string, string> BuildHostPair(int index, string value) =>
new($"{BasePath(index)}{nameof(HttpInterceptorOptions.Host)}", value);
new($"{BasePath(index)}{nameof(HttpRequestInterceptorOptions.Host)}", value);

// HttpInterceptorOptions:0:Path
public static KeyValuePair<string, string> BuildPathPair(int index, string value) =>
new($"{BasePath(index)}{nameof(HttpInterceptorOptions.Path)}", value);
new($"{BasePath(index)}{nameof(HttpRequestInterceptorOptions.Path)}", value);

// HttpInterceptorOptions:0:ResponseStatusCode
public static KeyValuePair<string, string> BuildReturnStatusCodePair(int index, string value) =>
new($"{BasePath(index)}{nameof(HttpInterceptorOptions.ResponseStatusCode)}", value);
new($"{BasePath(index)}{nameof(HttpRequestInterceptorOptions.ResponseStatusCode)}", value);

// HttpInterceptorOptions:0:ResponseJsonContent
public static KeyValuePair<string, string> BuildReturnJsonContentPair(int index, string value) =>
new($"{BasePath(index)}{nameof(HttpInterceptorOptions.ResponseJsonContent)}", value);
new($"{BasePath(index)}{nameof(HttpRequestInterceptorOptions.ResponseJsonContent)}", value);

// HttpInterceptorOptions:0:Rank
public static KeyValuePair<string, string> BuildRankPair(int index, string value) =>
new($"{BasePath(index)}{nameof(HttpInterceptorOptions.Rank)}", value);
new($"{BasePath(index)}{nameof(HttpRequestInterceptorOptions.Rank)}", value);

// HttpInterceptorOptions:0:Headers:0:Name
public static KeyValuePair<string, string> BuildResponseHeaderNamePair(int parentIndex, int index, string value) =>
new($"{BasePath(parentIndex)}{BaseHeaderPath(index)}{nameof(HttpInterceptorOptions.HttpResponseHeader.Name)}", value);
new($"{BasePath(parentIndex)}{BaseHeaderPath(index)}{nameof(HttpRequestInterceptorOptions.HttpResponseHeader.Name)}", value);

// HttpInterceptorOptions:0:Headers:0:Value
public static KeyValuePair<string, string> BuildResponseHeaderValuePair(int parentIndex, int index, string value) =>
new($"{BasePath(parentIndex)}{BaseHeaderPath(index)}{nameof(HttpInterceptorOptions.HttpResponseHeader.Value)}", value);
new($"{BasePath(parentIndex)}{BaseHeaderPath(index)}{nameof(HttpRequestInterceptorOptions.HttpResponseHeader.Value)}", value);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using static Scissors.HttpRequestInterceptor.Test.InMemoryConfigurationWriter;
using static Scissors.Test.InMemoryConfigurationWriter;

namespace Scissors.HttpRequestInterceptor.Test.RankedRoutes
namespace Scissors.Test.RankedRoutes
{
public class RankedRouteFixture : TestFixture
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Threading.Tasks;
using Xunit;

namespace Scissors.HttpRequestInterceptor.Test.RankedRoutes
namespace Scissors.Test.RankedRoutes
{
public sealed class RankedRouteTests : IClassFixture<RankedRouteFixture>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using static Scissors.HttpRequestInterceptor.Test.InMemoryConfigurationWriter;
using static Scissors.Test.InMemoryConfigurationWriter;

namespace Scissors.HttpRequestInterceptor.Test.ResponseContents
namespace Scissors.Test.ResponseContents
{
public sealed class ResponseContentFixture : TestFixture
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Threading.Tasks;
using Xunit;

namespace Scissors.HttpRequestInterceptor.Test.ResponseContents
namespace Scissors.Test.ResponseContents
{
public class ResponseContentTests : IClassFixture<ResponseContentFixture>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using static Scissors.HttpRequestInterceptor.Test.InMemoryConfigurationWriter;
using static Scissors.Test.InMemoryConfigurationWriter;

namespace Scissors.HttpRequestInterceptor.Test.ResponseStatuses
namespace Scissors.Test.ResponseStatuses
{
public sealed class ResponseStatusFixture : TestFixture
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Threading.Tasks;
using Xunit;

namespace Scissors.HttpRequestInterceptor.Test.ResponseStatuses
namespace Scissors.Test.ResponseStatuses
{
public sealed class ResponseStatusTests : IClassFixture<ResponseStatusFixture>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

<ItemGroup>
<ProjectReference Include="..\HttpRequestInterceptor\Scissors.HttpRequestInterceptor.csproj" />
<ProjectReference Include="..\Scissors\Scissors.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Collections.Generic;

using static Scissors.HttpRequestInterceptor.Test.InMemoryConfigurationWriter;
using static Scissors.Test.InMemoryConfigurationWriter;

namespace Scissors.HttpRequestInterceptor.Test.SimpleRoutes
namespace Scissors.Test.SimpleRoutes
{
public sealed class SimpleRouteFixture : TestFixture
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Threading.Tasks;
using Xunit;

namespace Scissors.HttpRequestInterceptor.Test.SimpleRoutes
namespace Scissors.Test.SimpleRoutes
{
public class SimpleRouteTests : IClassFixture<SimpleRouteFixture>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Collections.ObjectModel;
using System.Net.Http;

namespace Scissors.HttpRequestInterceptor.Test
namespace Scissors.Test
{
public abstract class TestFixture : IDisposable
{
Expand All @@ -24,7 +24,7 @@ protected TestFixture(List<KeyValuePair<string, string>> configurationPairs)
_factory = _provider.GetRequiredService<IHttpClientFactory>();
}

public HttpClient GetClient() => _factory.CreateClient("test");
public HttpClient GetClient() => _factory.CreateClient("scissors");

private static ServiceProvider BuildServiceProvider(IEnumerable<KeyValuePair<string, string>> configurationPairs)
{
Expand All @@ -34,17 +34,17 @@ private static ServiceProvider BuildServiceProvider(IEnumerable<KeyValuePair<str

IServiceCollection services = new ServiceCollection();

services.Configure<Collection<HttpInterceptorOptions>>(configuration.GetSection("HttpInterceptorOptions"));
services.Configure<Collection<HttpRequestInterceptorOptions>>(configuration.GetSection(nameof(HttpRequestInterceptorOptions)));

services.AddTransient<RequestsInterceptor>();
services.AddTransient<HttpRequestInterceptor>();

services.AddHttpClient("test", c =>
services.AddHttpClient("scissors", c =>
{
c.BaseAddress = new Uri(BaseUrl);
c.Timeout = TimeSpan.FromSeconds(1);
c.DefaultRequestHeaders.Add("User-Agent", "RequestInterceptorTests");
c.DefaultRequestHeaders.Add("User-Agent", "Scissors");
})
.AddHttpMessageHandler<RequestsInterceptor>();
.AddHttpMessageHandler<HttpRequestInterceptor>();

return services.BuildServiceProvider();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
using System.Net.Http;
using System.Text;

namespace Scissors.HttpRequestInterceptor
namespace Scissors
{
internal static class Extensions
{
private static readonly Dictionary<HttpStatusCode, Func<HttpInterceptorOptions, HttpResponseMessage>> _handlers = new Dictionary<HttpStatusCode, Func<HttpInterceptorOptions, HttpResponseMessage>>
private static readonly Dictionary<HttpStatusCode, Func<HttpRequestInterceptorOptions, HttpResponseMessage>> _handlers = new Dictionary<HttpStatusCode, Func<HttpRequestInterceptorOptions, HttpResponseMessage>>
{
[HttpStatusCode.BadRequest] = x => x.CreateMessage(HttpStatusCode.BadRequest),
[HttpStatusCode.Unauthorized] = x => x.CreateMessage(HttpStatusCode.Unauthorized),
Expand All @@ -25,15 +25,15 @@ internal static class Extensions
[HttpStatusCode.Created] = x => x.CreateMessage(HttpStatusCode.Created)
};

internal static HttpResponseMessage TryCreateResponse(this HttpInterceptorOptions options)
internal static HttpResponseMessage TryCreateResponse(this HttpRequestInterceptorOptions options)
{
if (_handlers.TryGetValue((HttpStatusCode)options.ResponseStatusCode, out var handler))
return handler(options);

throw new NotSupportedException($"Response status: {options.ResponseStatusCode} is not supported.");
}

private static HttpResponseMessage CreateMessage(this HttpInterceptorOptions options, HttpStatusCode statusCode)
private static HttpResponseMessage CreateMessage(this HttpRequestInterceptorOptions options, HttpStatusCode statusCode)
{
var message = new HttpResponseMessage(statusCode);

Expand All @@ -45,9 +45,9 @@ private static HttpResponseMessage CreateMessage(this HttpInterceptorOptions opt
return message.AddResponseHeaders(options.Headers);
}

private static HttpResponseMessage AddResponseHeaders(this HttpResponseMessage message, Collection<HttpInterceptorOptions.HttpResponseHeader> headers)
private static HttpResponseMessage AddResponseHeaders(this HttpResponseMessage message, Collection<HttpRequestInterceptorOptions.HttpResponseHeader> headers)
{
foreach (HttpInterceptorOptions.HttpResponseHeader header in headers)
foreach (HttpRequestInterceptorOptions.HttpResponseHeader header in headers)
{
message.Headers.Add(header.Name, header.Value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@
using System.Threading;
using System.Threading.Tasks;

namespace Scissors.HttpRequestInterceptor
namespace Scissors
{
public sealed class RequestsInterceptor : DelegatingHandler
public sealed class HttpRequestInterceptor : DelegatingHandler
{
private const string Slash = "/";
private const string WildCard = "*";

private static readonly char[] _splitter = Slash.ToCharArray();

private readonly Collection<HttpInterceptorOptions> _options;
private readonly Collection<HttpRequestInterceptorOptions> _options;

public RequestsInterceptor(IOptions<Collection<HttpInterceptorOptions>> options)
public HttpRequestInterceptor(IOptions<Collection<HttpRequestInterceptorOptions>> options)
{
_options = options?.Value ?? new Collection<HttpInterceptorOptions>();
_options = options?.Value ?? new Collection<HttpRequestInterceptorOptions>();
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
Expand All @@ -38,7 +38,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
}

private static bool IsConfigurationMatch(HttpInterceptorOptions option, Uri requestUri)
private static bool IsConfigurationMatch(HttpRequestInterceptorOptions option, Uri requestUri)
{
var host = requestUri.Host;
var pathAndQuery = requestUri.PathAndQuery;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Collections.ObjectModel;

namespace Scissors.HttpRequestInterceptor
namespace Scissors
{
public class HttpInterceptorOptions
public class HttpRequestInterceptorOptions
{
/// <summary>
/// Gets or sets the host value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>Scissors</PackageId>
<Version>1.0.1</Version>
<Version>2.0.0</Version>
<Authors>Mirza Merdovic</Authors>
<PackageDescription>Highly configurable implementation of DelegatingHandler that can be used for mocking the behavior of requests sent to specific routes.</PackageDescription>
<RepositoryUrl>https://github.com/MirzaMerdovic/scissors</RepositoryUrl>
Expand Down
8 changes: 4 additions & 4 deletions src/WebApiExample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Scissors.HttpRequestInterceptor;
using Scissors;
using System;
using System.Collections.ObjectModel;

Expand All @@ -20,16 +20,16 @@ public Startup(IConfiguration configuration)

public void ConfigureServices(IServiceCollection services)
{
services.Configure<Collection<HttpInterceptorOptions>>(Configuration.GetSection("HttpInterceptorOptions"));
services.Configure<Collection<HttpRequestInterceptorOptions>>(Configuration.GetSection("HttpInterceptorOptions"));

services.AddTransient<RequestsInterceptor>();
services.AddTransient<HttpRequestInterceptor>();

services.AddHttpClient<ProductAdapter>(
x =>
{
x.BaseAddress = new Uri("http://localhost:5000");
x.DefaultRequestHeaders.Add("User-Agent", "RequestInterceptorTests");
}).AddHttpMessageHandler<RequestsInterceptor>();
}).AddHttpMessageHandler<HttpRequestInterceptor>();

services.AddTransient<IProductAdapter, ProductAdapter>();

Expand Down
1 change: 1 addition & 0 deletions src/WebApiExample/WebApiExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

<ItemGroup>
<ProjectReference Include="..\HttpRequestInterceptor\Scissors.HttpRequestInterceptor.csproj" />
<ProjectReference Include="..\Scissors\Scissors.csproj" />
</ItemGroup>

</Project>
Loading