This repository has been archived by the owner on Dec 14, 2017. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added tests for introspection endpoint
- Loading branch information
1 parent
d58b2e2
commit 9a3674d
Showing
4 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
source/AccessTokenValidation.Tests/Integration Tests/Introspection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using AccessTokenValidation.Tests.Util; | ||
using FluentAssertions; | ||
using IdentityServer3.AccessTokenValidation; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace AccessTokenValidation.Tests.Integration_Tests | ||
{ | ||
public class Introspection | ||
{ | ||
IdentityServerBearerTokenAuthenticationOptions _options = new IdentityServerBearerTokenAuthenticationOptions | ||
{ | ||
Authority = "https://server/with/introspection", | ||
ValidationMode = ValidationMode.ValidationEndpoint, | ||
ClientId = "client", | ||
ClientSecret = "secret" | ||
}; | ||
|
||
[Fact] | ||
public async Task Unauthorized_Client() | ||
{ | ||
_options.IntrospectionHttpHandler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Unauthorized); | ||
|
||
var client = PipelineFactory.CreateHttpClient(_options); | ||
client.SetBearerToken("sometoken"); | ||
|
||
var result = await client.GetAsync("http://test"); | ||
result.StatusCode.Should().Be(HttpStatusCode.Unauthorized); | ||
} | ||
|
||
[Fact] | ||
public async Task ActiveToken() | ||
{ | ||
_options.IntrospectionHttpHandler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Active); | ||
|
||
var client = PipelineFactory.CreateHttpClient(_options); | ||
client.SetBearerToken("sometoken"); | ||
|
||
var result = await client.GetAsync("http://test"); | ||
result.StatusCode.Should().Be(HttpStatusCode.OK); | ||
} | ||
|
||
[Fact] | ||
public async Task InactiveToken() | ||
{ | ||
_options.IntrospectionHttpHandler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Inactive); | ||
|
||
var client = PipelineFactory.CreateHttpClient(_options); | ||
client.SetBearerToken("sometoken"); | ||
|
||
var result = await client.GetAsync("http://test"); | ||
result.StatusCode.Should().Be(HttpStatusCode.Unauthorized); | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
source/AccessTokenValidation.Tests/Util/IntrospectionEndpointHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Http.Formatting; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace AccessTokenValidation.Tests.Util | ||
{ | ||
class IntrospectionEndpointHandler : WebRequestHandler | ||
{ | ||
private readonly Behavior _behavior; | ||
|
||
public enum Behavior | ||
{ | ||
Active, | ||
Inactive, | ||
Unauthorized | ||
} | ||
|
||
public IntrospectionEndpointHandler(Behavior behavior) | ||
{ | ||
_behavior = behavior; | ||
} | ||
|
||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
if (_behavior == Behavior.Unauthorized) | ||
{ | ||
var response = new HttpResponseMessage(HttpStatusCode.Unauthorized); | ||
return Task.FromResult(response); | ||
} | ||
if (_behavior == Behavior.Active) | ||
{ | ||
var responseObject = new Dictionary<object, object> | ||
{ | ||
{ "active", true } | ||
}; | ||
|
||
var response = new HttpResponseMessage(HttpStatusCode.OK); | ||
response.Content = new ObjectContent<Dictionary<object, object>>( | ||
responseObject, new JsonMediaTypeFormatter()); | ||
|
||
return Task.FromResult(response); | ||
} | ||
if (_behavior == Behavior.Inactive) | ||
{ | ||
var responseObject = new Dictionary<object, object> | ||
{ | ||
{ "active", false } | ||
}; | ||
|
||
var response = new HttpResponseMessage(HttpStatusCode.OK); | ||
response.Content = new ObjectContent<Dictionary<object, object>>( | ||
responseObject, new JsonMediaTypeFormatter()); | ||
|
||
return Task.FromResult(response); | ||
} | ||
|
||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters