Skip to content

Commit e805b73

Browse files
committed
Added header to result, and test
1 parent 10dee3e commit e805b73

6 files changed

Lines changed: 61 additions & 1 deletion

File tree

src/NuGetGallery/Controllers/PackagesController.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Collections.Specialized;
67
using System.Diagnostics;
78
using System.Globalization;
89
using System.IO;
@@ -796,8 +797,10 @@ private static async Task<byte[]> ReadPackageFile(PackageArchiveReader packageAr
796797
}
797798
}
798799

800+
// This additional delete action addresses issue https://github.com/NuGet/Engineering/issues/2866 - we need to error out.
799801
[HttpDelete]
800-
public HttpStatusCodeResult DisplayPackage() => new HttpStatusCodeResult(HttpStatusCode.MethodNotAllowed);
802+
public HttpStatusCodeResult DisplayPackage()
803+
=> new HttpStatusCodeWithHeadersResult(HttpStatusCode.MethodNotAllowed, new NameValueCollection() { { "allow", "GET" } });
801804

802805
[HttpGet]
803806
public virtual async Task<ActionResult> DisplayPackage(string id, string version)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Collections.Specialized;
5+
using System.Net;
6+
using System.Web.Mvc;
7+
8+
namespace NuGetGallery
9+
{
10+
public class HttpStatusCodeWithHeadersResult : HttpStatusCodeResult
11+
{
12+
public readonly NameValueCollection Headers;
13+
14+
public HttpStatusCodeWithHeadersResult(HttpStatusCode statusCode, NameValueCollection headers)
15+
: base(statusCode)
16+
{
17+
Headers = headers;
18+
}
19+
20+
public override void ExecuteResult(ControllerContext context)
21+
{
22+
base.ExecuteResult(context);
23+
var response = context.RequestContext.HttpContext.Response;
24+
response.Headers.Add(Headers);
25+
}
26+
}
27+
}

src/NuGetGallery/NuGetGallery.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@
227227
<Compile Include="Infrastructure\ABTestEnrollmentFactory.cs" />
228228
<Compile Include="Infrastructure\CookieBasedABTestService.cs" />
229229
<Compile Include="Infrastructure\RequestValidationExceptionFilter.cs" />
230+
<Compile Include="Infrastructure\HttpStatusCodeWithHeadersResult.cs" />
230231
<Compile Include="Infrastructure\IABTestEnrollmentFactory.cs" />
231232
<Compile Include="Infrastructure\IABTestService.cs" />
232233
<Compile Include="Infrastructure\ILuceneDocumentFactory.cs" />

tests/NuGetGallery.Facts/Controllers/ControllerTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public void AllActionsHaveAntiForgeryTokenIfNotGet()
7474
new ControllerActionRuleException(typeof(ApiController), nameof(ApiController.DeletePackage)),
7575
new ControllerActionRuleException(typeof(ApiController), nameof(ApiController.PublishPackage)),
7676
new ControllerActionRuleException(typeof(ApiController), nameof(ApiController.DeprecatePackage)),
77+
new ControllerActionRuleException(typeof(PackagesController), nameof(PackagesController.DisplayPackage)),
7778
};
7879

7980
// Act

tests/NuGetGallery.Facts/Controllers/PackagesControllerFacts.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections;
66
using System.Collections.Generic;
7+
using System.Collections.Specialized;
78
using System.Globalization;
89
using System.IO;
910
using System.Linq;
@@ -498,6 +499,19 @@ public async Task GivenANonExistentPackageIt404s()
498499
ResultAssert.IsNotFound(result);
499500
}
500501

502+
[Fact]
503+
public void GivenADeleteMethodIt405sWithAllowHeader()
504+
{
505+
// Arrange
506+
var controller = CreateController(GetConfigurationService());
507+
508+
// Act
509+
var result = controller.DisplayPackage();
510+
511+
// Assert
512+
ResultAssert.IsStatusCodeWithHeaders(result, HttpStatusCode.MethodNotAllowed, new NameValueCollection() { { "allow", "GET" } });
513+
}
514+
501515
public static IEnumerable<PackageStatus> ValidatingPackageStatuses =
502516
new[] { PackageStatus.Validating, PackageStatus.FailedValidation };
503517

tests/NuGetGallery.Facts/TestUtils/ResultAssert.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
using System;
44
using System.Collections.Generic;
5+
using System.Collections.Specialized;
56
using System.Net;
67
using System.Web.Mvc;
78
using System.Web.Routing;
@@ -127,6 +128,19 @@ public static HttpStatusCodeResult IsStatusCode(ActionResult result, int statusC
127128
return statusCodeResult;
128129
}
129130

131+
public static HttpStatusCodeWithHeadersResult IsStatusCodeWithHeaders(ActionResult result, HttpStatusCode statusCode, NameValueCollection headers)
132+
{
133+
var statusCodeResult = Assert.IsAssignableFrom<HttpStatusCodeWithHeadersResult>(result);
134+
Assert.Equal((int)statusCode, statusCodeResult.StatusCode);
135+
136+
foreach (var key in headers.AllKeys)
137+
{
138+
Assert.Equal(headers.Get(key), statusCodeResult.Headers.Get(key));
139+
}
140+
141+
return statusCodeResult;
142+
}
143+
130144
public static EmptyResult IsEmpty(ActionResult result)
131145
{
132146
return Assert.IsType<EmptyResult>(result);

0 commit comments

Comments
 (0)