Skip to content

Commit e7c8d5e

Browse files
committed
Update HttpSourceResourceProvider to create a new HttpClient when the credentials change (#16)
Added Credentials to the equality comparer for PackageSource so that when HttpSourceResourceProvider caches HttpClients, it creates a new client when the credentials change. Previously it was returning the same HttpClient (which already has credentials set) regardles of the PackageSource.Credentails value. This was a re-implementation of dd0d988#diff-2e2f5bc7aba32eb58bd35315bbc3322399bbf8efd6e84fe7ef1bd95b45dab6a3
1 parent ba44482 commit e7c8d5e

3 files changed

Lines changed: 75 additions & 1 deletion

File tree

‎src/NuGet.Core/NuGet.Configuration/PackageSource/PackageSource.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ public bool Equals(PackageSource? other)
186186
}
187187

188188
return Name.Equals(other.Name, StringComparison.CurrentCultureIgnoreCase) &&
189-
Source.Equals(other.Source, StringComparison.OrdinalIgnoreCase);
189+
Source.Equals(other.Source, StringComparison.OrdinalIgnoreCase) &&
190+
Equals(Credentials, other.Credentials);
190191
}
191192

192193
public override bool Equals(object? obj)

‎test/NuGet.Core.Tests/NuGet.Configuration.Test/PackageSourceTests.cs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,33 @@ public void CalculatedMembers_ChangingSource_UpdatesValues()
120120
source.IsLocal.Should().BeTrue();
121121
source.GetHashCode().Should().NotBe(hashCodeBefore);
122122
}
123+
124+
[Fact]
125+
public void Equals_WithDifferentCredentials_AreNotEqual()
126+
{
127+
var a = CreateSourceForPasswordEquality("Foo");
128+
var b = CreateSourceForPasswordEquality("Bar");
129+
130+
Assert.NotEqual(a, b);
131+
}
132+
133+
[Fact]
134+
public void Equals_WithSameCredentials_AreEqual()
135+
{
136+
var a = CreateSourceForPasswordEquality("Foo");
137+
var b = CreateSourceForPasswordEquality("Foo");
138+
139+
Assert.Equal(a, b);
140+
}
141+
private static PackageSource CreateSourceForPasswordEquality(string password)
142+
{
143+
var credentials = new PackageSourceCredential("SourceName", "username", password, isPasswordClearText: false, null);
144+
var source = new PackageSource("Source", "SourceName", isEnabled: false)
145+
{
146+
Credentials = credentials,
147+
ProtocolVersion = 43
148+
};
149+
return source;
150+
}
123151
}
124152
}

‎test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpSourceResourceProviderTests.cs‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,50 @@ public async Task WhenMaxHttpRequestsPerSourceIsConfiguredThenItsValueWillNotBeU
6262
Assert.NotNull(httpSourceResource);
6363
Assert.Equal(maxHttpRequestsPerSource, sourceRepository.PackageSource.MaxHttpRequestsPerSource);
6464
}
65+
66+
[Fact]
67+
public async Task WhenSourceRepositoryHasDifferentCredentialsPassword_ReturnsDifferentHttpSourceResources()
68+
{
69+
// Arrange
70+
var sourceRepositoryA = CreateSourceRepositoryForPasswordEquality("Foo");
71+
var sourceRepositoryB = CreateSourceRepositoryForPasswordEquality("Bar");
72+
73+
// Act
74+
var provider = new HttpSourceResourceProvider();
75+
var a = await provider.TryCreate(sourceRepositoryA, CancellationToken.None);
76+
var b = await provider.TryCreate(sourceRepositoryB, CancellationToken.None);
77+
78+
// Assert
79+
Assert.NotSame(a.Item2, b.Item2);
80+
}
81+
82+
[Fact]
83+
public async Task WhenSourceRepositoryHasTheSameCredentialsPassword_ReturnsTheSameHttpSourceResource()
84+
{
85+
// Arrange
86+
var sourceRepositoryA = CreateSourceRepositoryForPasswordEquality("Foo");
87+
var sourceRepositoryB = CreateSourceRepositoryForPasswordEquality("Foo");
88+
89+
// Act
90+
var provider = new HttpSourceResourceProvider();
91+
var a = await provider.TryCreate(sourceRepositoryA, CancellationToken.None);
92+
var b = await provider.TryCreate(sourceRepositoryB, CancellationToken.None);
93+
94+
// Assert
95+
Assert.Same(a.Item2, b.Item2);
96+
}
97+
98+
private SourceRepository CreateSourceRepositoryForPasswordEquality(string password)
99+
{
100+
return new SourceRepository(
101+
new PackageSource(_testPackageSourceURL)
102+
{
103+
Credentials = new PackageSourceCredential(_testPackageSourceURL, "user", password, true, null)
104+
},
105+
[
106+
new HttpSourceResourceProvider()
107+
]
108+
);
109+
}
65110
}
66111
}

0 commit comments

Comments
 (0)