-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Port 3rd party contribution to ASP.NET (#205). * Update dependencies.
- Loading branch information
1 parent
dbcc01d
commit 1c1e81e
Showing
21 changed files
with
321 additions
and
112 deletions.
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
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
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
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
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
106 changes: 106 additions & 0 deletions
106
Okta.AspNet.WebApi.IntegrationTest/MiddlewareWithCustomAuthTypeShould.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,106 @@ | ||
// <copyright file="MiddlewareShould.cs" company="Okta, Inc"> | ||
// Copyright (c) 2018-present Okta, Inc. All rights reserved. | ||
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information. | ||
// </copyright> | ||
|
||
using System; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Threading.Tasks; | ||
using System.Web.Http; | ||
using System.Web.Http.Dispatcher; | ||
using Microsoft.Owin.Testing; | ||
using Owin; | ||
using Xunit; | ||
|
||
namespace Okta.AspNet.WebApi.IntegrationTest | ||
{ | ||
public class MiddlewareWithCustomAuthTypeShould : IDisposable | ||
{ | ||
private TestServer _server; | ||
|
||
private string BaseUrl { get; set; } | ||
|
||
private string ProtectedEndpoint { get; set; } | ||
|
||
private MockHttpMessageHandler MockHttpHandler { get; set; } | ||
|
||
public MiddlewareWithCustomAuthTypeShould() | ||
{ | ||
BaseUrl = "http://localhost:8080"; | ||
ProtectedEndpoint = string.Format("{0}/api/messages", BaseUrl); | ||
MockHttpHandler = new MockHttpMessageHandler(); | ||
|
||
_server = TestServer.Create(app => | ||
{ | ||
var startup = new StartupWithCustomAuthType(); | ||
startup.HttpMessageHandler = MockHttpHandler; | ||
startup.Configuration(app); | ||
|
||
HttpConfiguration config = new HttpConfiguration(); | ||
config.Services.Replace(typeof(IAssembliesResolver), new WebApiResolver()); | ||
config.MapHttpAttributeRoutes(); | ||
app.UseWebApi(config); | ||
}); | ||
|
||
_server.BaseAddress = new Uri(BaseUrl); | ||
} | ||
|
||
[Fact] | ||
public async Task Returns401WhenAccessToProtectedRouteWithoutTokenAsync() | ||
{ | ||
using (var client = new HttpClient(_server.Handler)) | ||
{ | ||
var response = await client.GetAsync(ProtectedEndpoint); | ||
Assert.True(response.StatusCode == System.Net.HttpStatusCode.Unauthorized); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task Returns401WhenAccessToProtectedRouteWithInvalidTokenAsync() | ||
{ | ||
var accessToken = "thisIsAnInvalidToken"; | ||
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, ProtectedEndpoint); | ||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); | ||
|
||
using (var client = new HttpClient(_server.Handler)) | ||
{ | ||
var response = await client.SendAsync(request); | ||
Assert.True(response.StatusCode == System.Net.HttpStatusCode.Unauthorized); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task InvokeCustomEventsAsync() | ||
{ | ||
var accessToken = "thisIsAnInvalidToken"; | ||
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, ProtectedEndpoint); | ||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); | ||
|
||
using (var client = new HttpClient(_server.Handler)) | ||
{ | ||
var response = await client.SendAsync(request); | ||
Assert.True(response.Headers.Contains("myCustomHeader")); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task InvokeCustomHandlerAsync() | ||
{ | ||
var accessToken = "thisIsAnInvalidToken"; | ||
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, ProtectedEndpoint); | ||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); | ||
|
||
using (var client = new HttpClient(_server.Handler)) | ||
{ | ||
var response = await client.SendAsync(request); | ||
Assert.True(MockHttpHandler.NumberOfCalls > 0); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_server.Dispose(); | ||
} | ||
} | ||
} |
Oops, something went wrong.