Use new HttpContext.Features API.

This commit is contained in:
Chris R 2015-08-31 06:46:18 -07:00
parent 92185a1c27
commit 56315c441c
2 changed files with 88 additions and 14 deletions

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Http.Features.Authentication; using Microsoft.AspNet.Http.Features.Authentication;
using Microsoft.AspNet.Http.Features.Authentication.Internal; using Microsoft.AspNet.Http.Features.Authentication.Internal;
@ -11,11 +12,11 @@ namespace Microsoft.AspNet.Authentication
{ {
internal static IHttpAuthenticationFeature GetAuthentication(this HttpContext context) internal static IHttpAuthenticationFeature GetAuthentication(this HttpContext context)
{ {
var auth = context.GetFeature<IHttpAuthenticationFeature>(); var auth = context.Features.Get<IHttpAuthenticationFeature>();
if (auth == null) if (auth == null)
{ {
auth = new HttpAuthenticationFeature(); auth = new HttpAuthenticationFeature();
context.SetFeature<IHttpAuthenticationFeature>(auth); context.Features.Set<IHttpAuthenticationFeature>(auth);
} }
return auth; return auth;
} }

View File

@ -2,11 +2,15 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Http.Features.Authentication; using Microsoft.AspNet.Http.Features.Authentication;
using Microsoft.AspNet.Http.Internal; using Microsoft.AspNet.Http.Internal;
using Microsoft.Framework.Logging; using Microsoft.Framework.Logging;
using Microsoft.Framework.Primitives;
using Moq; using Moq;
using Xunit; using Xunit;
@ -92,10 +96,9 @@ namespace Microsoft.AspNet.Authentication
public static async Task<CountHandler> Create(string scheme) public static async Task<CountHandler> Create(string scheme)
{ {
var handler = new CountHandler(); var handler = new CountHandler();
var context = new Mock<HttpContext>(); var context = new DefaultHttpContext();
context.Setup(c => c.Request).Returns(new Mock<HttpRequest>().Object); context.Features.Set<IHttpResponseFeature>(new TestResponse());
context.Setup(c => c.Response).Returns(new Mock<HttpResponse>().Object); await handler.InitializeAsync(new CountOptions(), context, new LoggerFactory().CreateLogger("CountHandler"), Framework.WebEncoders.UrlEncoder.Default);
await handler.InitializeAsync(new CountOptions(), context.Object, new LoggerFactory().CreateLogger("CountHandler"), Framework.WebEncoders.UrlEncoder.Default);
handler.Options.AuthenticationScheme = scheme; handler.Options.AuthenticationScheme = scheme;
handler.Options.AutomaticAuthentication = true; handler.Options.AutomaticAuthentication = true;
return handler; return handler;
@ -116,10 +119,9 @@ namespace Microsoft.AspNet.Authentication
public static async Task<TestHandler> Create(string scheme) public static async Task<TestHandler> Create(string scheme)
{ {
var handler = new TestHandler(); var handler = new TestHandler();
var context = new Mock<HttpContext>(); var context = new DefaultHttpContext();
context.Setup(c => c.Request).Returns(new Mock<HttpRequest>().Object); context.Features.Set<IHttpResponseFeature>(new TestResponse());
context.Setup(c => c.Response).Returns(new Mock<HttpResponse>().Object); await handler.InitializeAsync(new TestOptions(), context, new LoggerFactory().CreateLogger("TestHandler"), Framework.WebEncoders.UrlEncoder.Default);
await handler.InitializeAsync(new TestOptions(), context.Object, new LoggerFactory().CreateLogger("TestHandler"), Framework.WebEncoders.UrlEncoder.Default);
handler.Options.AuthenticationScheme = scheme; handler.Options.AuthenticationScheme = scheme;
return handler; return handler;
} }
@ -147,10 +149,9 @@ namespace Microsoft.AspNet.Authentication
public static async Task<TestAutoHandler> Create(string scheme, bool auto) public static async Task<TestAutoHandler> Create(string scheme, bool auto)
{ {
var handler = new TestAutoHandler(); var handler = new TestAutoHandler();
var context = new Mock<HttpContext>(); var context = new DefaultHttpContext();
context.Setup(c => c.Request).Returns(new Mock<HttpRequest>().Object); context.Features.Set<IHttpResponseFeature>(new TestResponse());
context.Setup(c => c.Response).Returns(new Mock<HttpResponse>().Object); await handler.InitializeAsync(new TestAutoOptions(), context, new LoggerFactory().CreateLogger("TestAutoHandler"), Framework.WebEncoders.UrlEncoder.Default);
await handler.InitializeAsync(new TestAutoOptions(), context.Object, new LoggerFactory().CreateLogger("TestAutoHandler"), Framework.WebEncoders.UrlEncoder.Default);
handler.Options.AuthenticationScheme = scheme; handler.Options.AuthenticationScheme = scheme;
handler.Options.AutomaticAuthentication = auto; handler.Options.AutomaticAuthentication = auto;
return handler; return handler;
@ -161,5 +162,77 @@ namespace Microsoft.AspNet.Authentication
return Task.FromResult<AuthenticationTicket>(null); return Task.FromResult<AuthenticationTicket>(null);
} }
} }
private class TestResponse : IHttpResponseFeature
{
public Stream Body
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public bool HasStarted
{
get
{
throw new NotImplementedException();
}
}
public IDictionary<string, StringValues> Headers
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public string ReasonPhrase
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public int StatusCode
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public void OnCompleted(Func<object, Task> callback, object state)
{
throw new NotImplementedException();
}
public void OnStarting(Func<object, Task> callback, object state)
{
}
}
} }
} }