Skip to content

Commit

Permalink
Tests for asp.net core context builder
Browse files Browse the repository at this point in the history
  • Loading branch information
magnus-tretton37 committed Mar 19, 2019
1 parent 884b318 commit 25b1eaa
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
6 changes: 3 additions & 3 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ image: Visual Studio 2017
install:
- ps: dotnet tool install coveralls.net --tool-path tools
build_script:
- cmd: dotnet build src/Castle.Sdk -c Release -f netstandard2.0
- cmd: dotnet build src/Castle.Sdk -c Release -f net461
- cmd: dotnet build src/Castle.Sdk
- cmd: dotnet build src/Tests -c Debug
test_script:
- ps: dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Include="[Castle.Sdk*]*" src/Tests/Tests.csproj
- ps: dotnet test --no-build /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Include="[Castle.Sdk*]*" src/Tests/Tests.csproj
- ps: .\tools\csmacnz.coveralls.exe --opencover -i src/Tests/coverage.opencover.xml --useRelativePaths --repoToken $env:COVERALLS_REPO_TOKEN --commitId $env:APPVEYOR_REPO_COMMIT --commitBranch $env:APPVEYOR_REPO_BRANCH --commitAuthor $env:APPVEYOR_REPO_COMMIT_AUTHOR --commitEmail $env:APPVEYOR_REPO_COMMIT_AUTHOR_EMAIL --commitMessage $env:APPVEYOR_REPO_COMMIT_MESSAGE --jobId $env:APPVEYOR_JOB_ID
14 changes: 9 additions & 5 deletions src/Castle.Sdk/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,22 @@ public static RequestContext FromHttpRequest(System.Web.HttpRequestBase request)
#if NETSTANDARD2_0
public static RequestContext FromHttpRequest(Microsoft.AspNetCore.Http.HttpRequest request)
{
var clientId = request.Headers.TryGetValue("X-Castle-Client-ID", out var headerId)
? headerId.First()
: request.Cookies["__cid"];

return new RequestContext()
{
ClientId = clientId,
ClientId = GetClientId(request.Headers, request.Cookies),
Headers = request.Headers.ToDictionary(x => x.Key, y => y.Value.FirstOrDefault()),
Ip = request.HttpContext.Connection.RemoteIpAddress.ToString(),
};
}

internal static string GetClientId(
IDictionary<string, Microsoft.Extensions.Primitives.StringValues> headers,
Microsoft.AspNetCore.Http.IRequestCookieCollection cookies)
{
return headers.TryGetValue("X-Castle-Client-ID", out var headerId)
? headerId.First()
: cookies["__cid"];
}
#endif
}
}
36 changes: 36 additions & 0 deletions src/Tests/When_creating_request_context.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections.Generic;
using AutoFixture.Xunit2;
using Castle;
using FluentAssertions;
using Microsoft.AspNetCore.Http.Internal;
using Microsoft.Extensions.Primitives;
using Xunit;

namespace Tests
{
public class When_creating_request_context
{
[Theory, AutoData]
public void Should_get_client_id_from_castle_header_if_present(
string castleHeaderValue,
string otherHeader,
string otherHeaderValue,
string cookieValue)
{
var headers = new Dictionary<string, StringValues>()
{
["X-Castle-Client-ID"] = castleHeaderValue,
[otherHeader] = otherHeaderValue
};

var cookies = new RequestCookieCollection(new Dictionary<string, string>()
{
["__cid"] = cookieValue
});

var result = Context.GetClientId(headers, cookies);

result.Should().Be(castleHeaderValue);
}
}
}

0 comments on commit 25b1eaa

Please sign in to comment.