From 1144ba72d50c00d0588bf26f2c4ec63b74733aeb Mon Sep 17 00:00:00 2001 From: ianhong Date: Thu, 5 Mar 2015 14:29:52 -0800 Subject: [PATCH] Porting HttpUnauthorizedResult --- .../ActionResults/HttpUnauthorizedResult.cs | 21 +++++++++++++++++++ src/Microsoft.AspNet.Mvc.Core/Controller.cs | 10 +++++++++ .../Filters/AuthorizationFilterAttribute.cs | 2 +- .../HttpUnauthorizedResultTests.cs | 21 +++++++++++++++++++ .../ControllerTests.cs | 13 ++++++++++++ .../Filters/AuthorizeFilterTest.cs | 2 +- 6 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 src/Microsoft.AspNet.Mvc.Core/ActionResults/HttpUnauthorizedResult.cs create mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/HttpUnauthorizedResultTests.cs diff --git a/src/Microsoft.AspNet.Mvc.Core/ActionResults/HttpUnauthorizedResult.cs b/src/Microsoft.AspNet.Mvc.Core/ActionResults/HttpUnauthorizedResult.cs new file mode 100644 index 0000000000..e4a6bd9716 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/ActionResults/HttpUnauthorizedResult.cs @@ -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 +{ + /// + /// Represents an that when + /// executed will produce an Unauthorized (401) response. + /// + public class HttpUnauthorizedResult : HttpStatusCodeResult + { + /// + /// Creates a new instance. + /// + public HttpUnauthorizedResult() : base(StatusCodes.Status401Unauthorized) + { + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/Controller.cs b/src/Microsoft.AspNet.Mvc.Core/Controller.cs index 5a0030650a..fd12c596e4 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Controller.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Controller.cs @@ -682,6 +682,16 @@ namespace Microsoft.AspNet.Mvc return new FilePathResult(fileName, contentType) { FileDownloadName = fileDownloadName }; } + /// + /// Creates an that produces an Unauthorized (401) response. + /// + /// The created for the response. + [NonAction] + public virtual HttpUnauthorizedResult HttpUnauthorized() + { + return new HttpUnauthorizedResult(); + } + /// /// Creates an that produces a Not Found (404) response. /// diff --git a/src/Microsoft.AspNet.Mvc.Core/Filters/AuthorizationFilterAttribute.cs b/src/Microsoft.AspNet.Mvc.Core/Filters/AuthorizationFilterAttribute.cs index 378a811e4a..8d265936ba 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Filters/AuthorizationFilterAttribute.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Filters/AuthorizationFilterAttribute.cs @@ -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(); } } } diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/HttpUnauthorizedResultTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/HttpUnauthorizedResultTests.cs new file mode 100644 index 0000000000..1a7cdd1f4f --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/HttpUnauthorizedResultTests.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ControllerTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ControllerTests.cs index dd81d2e303..9ef9fab232 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ControllerTests.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ControllerTests.cs @@ -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(result); + Assert.Equal(StatusCodes.Status401Unauthorized, result.StatusCode); + } [Fact] public void HttpNotFound_SetsStatusCode() diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Filters/AuthorizeFilterTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Filters/AuthorizeFilterTest.cs index 997648b3ae..241b6dfd76 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Filters/AuthorizeFilterTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Filters/AuthorizeFilterTest.cs @@ -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);