Skip to content

Commit 5f3979a

Browse files
committed
Fix handling of missing header case in UploadHelper (#6491)
Also, clean up the parsed GUID to avoid weird casing or leading space issues Fix #6490
1 parent b8eb013 commit 5f3979a

3 files changed

Lines changed: 60 additions & 3 deletions

File tree

src/NuGetGallery/Helpers/UploadHelper.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.Collections.Specialized;
76

87
namespace NuGetGallery.Helpers
@@ -20,9 +19,9 @@ public static string GetUploadTracingKey(NameValueCollection headers)
2019
try
2120
{
2221
uploadTracingKey = headers[CoreConstants.UploadTracingKeyHeaderName];
23-
Guid.Parse(uploadTracingKey);
22+
uploadTracingKey = Guid.Parse(uploadTracingKey).ToString();
2423
}
25-
catch (Exception ex) when (ex is FormatException || ex is KeyNotFoundException)
24+
catch (Exception ex) when (ex is FormatException || ex is ArgumentNullException)
2625
{
2726
// An upload tracing key was not found
2827
// Simultaneous UI uploads might have strange behaviour.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 Xunit;
6+
7+
namespace NuGetGallery.Helpers
8+
{
9+
public class UploadHelperFacts
10+
{
11+
public class TheGetUploadTracingKeyMethod
12+
{
13+
private const string EmptyGuid = "00000000-0000-0000-0000-000000000000";
14+
15+
[Fact]
16+
public void ReturnsEmptyGuidForMissingHeader()
17+
{
18+
var headers = new NameValueCollection();
19+
20+
var result = UploadHelper.GetUploadTracingKey(headers);
21+
22+
Assert.Equal(EmptyGuid, result);
23+
}
24+
25+
[Theory]
26+
[InlineData(null)]
27+
[InlineData("")]
28+
[InlineData("not-a-guid")]
29+
[InlineData("00000000-0000-0000-0000-000000000000")]
30+
[InlineData(" 00000000-0000-0000-0000-000000000000 ")]
31+
public void ReturnsEmptyGuidForInvalidOrEmptyGuid(string value)
32+
{
33+
var headers = new NameValueCollection();
34+
headers["upload-id"] = value;
35+
36+
var result = UploadHelper.GetUploadTracingKey(headers);
37+
38+
Assert.Equal(EmptyGuid, result);
39+
}
40+
41+
[Theory]
42+
[InlineData("3bcf7d12-eb0a-46c9-98a8-c160801e8134")]
43+
[InlineData("3bcf7d12eb0a46c998a8c160801e8134")]
44+
[InlineData("3BCF7D12-EB0A-46C9-98A8-C160801E8134")]
45+
[InlineData(" 3bcf7d12-eb0a-46c9-98a8-c160801e8134 ")]
46+
public void ReturnsGuidForValidGuid(string value)
47+
{
48+
var headers = new NameValueCollection();
49+
headers["upload-id"] = value;
50+
51+
var result = UploadHelper.GetUploadTracingKey(headers);
52+
53+
Assert.Equal("3bcf7d12-eb0a-46c9-98a8-c160801e8134", result);
54+
}
55+
}
56+
}
57+
}

tests/NuGetGallery.Facts/NuGetGallery.Facts.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<Compile Include="Framework\MemberDataHelper.cs" />
8989
<Compile Include="Helpers\ObfuscationHelperFacts.cs" />
9090
<Compile Include="Helpers\PackageHelperTests.cs" />
91+
<Compile Include="Helpers\UploadHelperFacts.cs" />
9192
<Compile Include="Infrastructure\Authentication\ApiKeyV3Facts.cs" />
9293
<Compile Include="Infrastructure\Authentication\ApiKeyV4Facts.cs" />
9394
<Compile Include="Infrastructure\Authentication\V3HasherTests.cs" />

0 commit comments

Comments
 (0)