Porting HttpUnauthorizedResult

This commit is contained in:
ianhong 2015-03-05 14:29:52 -08:00
parent 7c18e666a3
commit 1144ba72d5
6 changed files with 67 additions and 2 deletions

View File

@ -0,0 +1,21 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.WebUtilities;
namespace Microsoft.AspNet.Mvc
{
/// <summary>
/// Represents an <see cref="HttpUnauthorizedResult"/> that when
/// executed will produce an Unauthorized (401) response.
/// </summary>
public class HttpUnauthorizedResult : HttpStatusCodeResult
{
/// <summary>
/// Creates a new <see cref="HttpUnauthorizedResult"/> instance.
/// </summary>
public HttpUnauthorizedResult() : base(StatusCodes.Status401Unauthorized)
{
}
}
}

View File

@ -682,6 +682,16 @@ namespace Microsoft.AspNet.Mvc
return new FilePathResult(fileName, contentType) { FileDownloadName = fileDownloadName };
}
/// <summary>
/// Creates an <see cref="HttpUnauthorizedResult"/> that produces an Unauthorized (401) response.
/// </summary>
/// <returns>The created <see cref="HttpUnauthorizedResult"/> for the response.</returns>
[NonAction]
public virtual HttpUnauthorizedResult HttpUnauthorized()
{
return new HttpUnauthorizedResult();
}
/// <summary>
/// Creates an <see cref="HttpNotFoundResult"/> that produces a Not Found (404) response.
/// </summary>

View File

@ -33,7 +33,7 @@ namespace Microsoft.AspNet.Mvc
protected virtual void Fail([NotNull] AuthorizationContext context)
{
context.Result = new HttpStatusCodeResult(StatusCodes.Status401Unauthorized);
context.Result = new HttpUnauthorizedResult();
}
}
}

View File

@ -0,0 +1,21 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.WebUtilities;
using Xunit;
namespace Microsoft.AspNet.Mvc
{
public class HttpUnauthorizedResultTests
{
[Fact]
public void HttpUnauthorizedResult_InitializesStatusCode()
{
// Arrange & act
var result = new HttpUnauthorizedResult();
// Assert
Assert.Equal(StatusCodes.Status401Unauthorized, result.StatusCode);
}
}
}

View File

@ -641,6 +641,19 @@ namespace Microsoft.AspNet.Mvc.Test
Assert.Equal("someDownloadName", result.FileDownloadName);
}
[Fact]
public void HttpUnauthorized_SetsStatusCode()
{
// Arrange
var controller = new TestableController();
// Act
var result = controller.HttpUnauthorized();
// Assert
Assert.IsType<HttpUnauthorizedResult>(result);
Assert.Equal(StatusCodes.Status401Unauthorized, result.StatusCode);
}
[Fact]
public void HttpNotFound_SetsStatusCode()

View File

@ -228,7 +228,7 @@ namespace Microsoft.AspNet.Mvc.Test
services.AddInstance(authorizationService.Object)
);
authorizationContext.Result = new HttpStatusCodeResult(StatusCodes.Status401Unauthorized);
authorizationContext.Result = new HttpUnauthorizedResult();
// Act
await authorizeFilter.OnAuthorizationAsync(authorizationContext);