Skip to content

Commit fe6fd30

Browse files
authored
controller redirects to licenses.nuget.org with license expression (#6726)
1 parent 1e8e9a9 commit fe6fd30

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

src/NuGetGallery/Controllers/PackagesController.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,11 @@ public virtual async Task<ActionResult> License(string id, string version)
677677
return HttpNotFound();
678678
}
679679

680+
if (!string.IsNullOrWhiteSpace(package.LicenseExpression))
681+
{
682+
return Redirect(LicenseExpressionRedirectUrlHelper.GetLicenseExpressionRedirectUrl(package.LicenseExpression));
683+
}
684+
680685
if (package.EmbeddedLicenseType == EmbeddedLicenseFileType.Absent)
681686
{
682687
return HttpNotFound();

tests/NuGetGallery.Facts/Controllers/PackagesControllerFacts.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7138,6 +7138,31 @@ public async Task GivenInvalidPackageReturns404()
71387138
Assert.IsType<HttpNotFoundResult>(result);
71397139
}
71407140

7141+
[Theory]
7142+
[InlineData("MIT", "https://licenses.nuget.org/MIT")]
7143+
[InlineData("TestLicenseExpression", "https://licenses.nuget.org/TestLicenseExpression")]
7144+
public async Task GivenValidPackageWithLicenseExpressionRedirectToLicenseUrl(string licenseExpression, string expectedRedirectUrl)
7145+
{
7146+
// arrange
7147+
var package = new Package
7148+
{
7149+
PackageRegistration = new PackageRegistration { Id = _packageId },
7150+
Version = _packageVersion,
7151+
LicenseExpression = licenseExpression
7152+
};
7153+
7154+
_packageService.Setup(p => p.FindPackageByIdAndVersionStrict(_packageId, _packageVersion)).Returns(package);
7155+
var controller = CreateController(
7156+
GetConfigurationService(),
7157+
packageService: _packageService);
7158+
7159+
// act
7160+
var result = await controller.License(_packageId, _packageVersion);
7161+
7162+
// assert
7163+
ResultAssert.IsRedirectTo(result, expectedRedirectUrl);
7164+
}
7165+
71417166
[Fact]
71427167
public async Task GivenPackageWithoutLicenseFileReturns404()
71437168
{

0 commit comments

Comments
 (0)