Skip to content

Commit 03597a6

Browse files
authored
Merge pull request #86 from NuGet/dev
Merge branch 'dev' into master
2 parents 97e3d99 + cfe584f commit 03597a6

7 files changed

Lines changed: 70 additions & 9 deletions

File tree

build.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ param (
99
[string]$SemanticVersion = '1.0.0-zlocal',
1010
[string]$Branch,
1111
[string]$CommitSHA,
12-
[string]$BuildBranch = '2d8feecabe3aeaed7f5b4d50b9be78c94faf39ec'
12+
[string]$BuildBranch = 'cb2b9e41b18cb77ee644a51951d8c8f24cde9adf'
1313
)
1414

1515
$msBuildVersion = 15;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.Serialization;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace NuGet.Server.Core
9+
{
10+
/// <summary>
11+
/// This exception is thrown when trying to add a package in a version that already exists on the server
12+
/// and <see cref="NuGet.Server.Core.Infrastructure.ServerPackageRepository.AllowOverrideExistingPackageOnPush"/> is set to false.
13+
/// </summary>
14+
public class DuplicatePackageException : Exception
15+
{
16+
public DuplicatePackageException()
17+
{
18+
}
19+
20+
public DuplicatePackageException(string message) : base(message)
21+
{
22+
}
23+
24+
public DuplicatePackageException(string message, Exception innerException) : base(message, innerException)
25+
{
26+
}
27+
28+
protected DuplicatePackageException(SerializationInfo info, StreamingContext context) : base(info, context)
29+
{
30+
}
31+
}
32+
}

src/NuGet.Server.Core/Infrastructure/ServerPackageRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ private bool CanPackageBeAddedWithoutLocking(IPackage package, bool shouldThrow)
400400

401401
if (shouldThrow)
402402
{
403-
throw new InvalidOperationException(message);
403+
throw new DuplicatePackageException(message);
404404
}
405405

406406
return false;

src/NuGet.Server.Core/NuGet.Server.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<Link>HashCodeCombiner.cs</Link>
6161
</Compile>
6262
<Compile Include="Core\Constants.cs" />
63+
<Compile Include="Core\DuplicatePackageException.cs" />
6364
<Compile Include="Core\FrameworkNameExtensions.cs" />
6465
<Compile Include="Core\NullFileSystem.cs" />
6566
<Compile Include="Core\PackageExtensions.cs" />

src/NuGet.Server.V2/Controllers/NuGetODataController.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,15 @@ public virtual async Task<HttpResponseMessage> UploadPackage(CancellationToken t
403403
HttpResponseMessage retValue;
404404
if (_authenticationService.IsAuthenticated(User, apiKey, package.Id))
405405
{
406-
await _serverRepository.AddPackageAsync(package, token);
407-
retValue = Request.CreateResponse(HttpStatusCode.Created);
406+
try
407+
{
408+
await _serverRepository.AddPackageAsync(package, token);
409+
retValue = Request.CreateResponse(HttpStatusCode.Created);
410+
}
411+
catch (DuplicatePackageException ex)
412+
{
413+
retValue = CreateStringResponse(HttpStatusCode.Conflict, ex.Message);
414+
}
408415
}
409416
else
410417
{

test/NuGet.Server.Core.Tests/ServerPackageRepositoryTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public async Task ServerPackageRepository_DuplicateAddAfterClearObservesOverride
203203
}
204204
else
205205
{
206-
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
206+
await Assert.ThrowsAsync<DuplicatePackageException>(async () =>
207207
await serverRepository.AddPackageAsync(CreatePackage("test", "1.2"), Token));
208208
}
209209
}
@@ -895,7 +895,7 @@ public async Task ServerPackageRepositoryAddPackageRejectsDuplicatesWithSemVer2(
895895
await serverRepository.AddPackageAsync(CreatePackage("Foo", "1.0.0-beta.1+foo"), Token);
896896

897897
// Act & Assert
898-
var actual = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
898+
var actual = await Assert.ThrowsAsync<DuplicatePackageException>(async () =>
899899
await serverRepository.AddPackageAsync(CreatePackage("Foo", "1.0.0-beta.1+bar"), Token));
900900
Assert.Equal(
901901
"Package Foo 1.0.0-beta.1 already exists. The server is configured to not allow overwriting packages that already exist.",

test/NuGet.Server.Tests/IntegrationTests.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -135,6 +135,27 @@ public async Task FilterOnFramework()
135135
}
136136
}
137137

138+
[Fact]
139+
public async Task PushDuplicatePackage()
140+
{
141+
// Arrange
142+
using (var tc = new TestContext(_output))
143+
{
144+
string apiKey = "foobar";
145+
tc.SetApiKey(apiKey);
146+
147+
var packagePath = Path.Combine(tc.TemporaryDirectory, "package.nupkg");
148+
TestData.CopyResourceToPath(TestData.PackageResource, packagePath);
149+
150+
// Act & Assert
151+
// 1. Push the package.
152+
await tc.PushPackageAsync(apiKey, packagePath);
153+
154+
// 2. Push the package again expecting a 409 as the Package already exists.
155+
await tc.PushPackageAsync(apiKey, packagePath, excepectedStatusCode: HttpStatusCode.Conflict);
156+
}
157+
}
158+
138159
[Fact]
139160
public async Task PushPackageThenReadPackages()
140161
{
@@ -462,7 +483,7 @@ public MultipartContent GetFileUploadContent(params string[] paths)
462483
return content;
463484
}
464485

465-
public async Task PushPackageAsync(string apiKey, string packagePath, string pushUrl = "/nuget")
486+
public async Task PushPackageAsync(string apiKey, string packagePath, string pushUrl = "/nuget", HttpStatusCode excepectedStatusCode = HttpStatusCode.Created)
466487
{
467488
using (var request = new HttpRequestMessage(HttpMethod.Put, pushUrl)
468489
{
@@ -474,7 +495,7 @@ public async Task PushPackageAsync(string apiKey, string packagePath, string pus
474495
})
475496
using (var response = await Client.SendAsync(request))
476497
{
477-
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
498+
Assert.Equal(excepectedStatusCode, response.StatusCode);
478499
}
479500
}
480501

0 commit comments

Comments
 (0)