-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Partial AOT support - Change from MVC to Minimal API - Change from Newtonsoft.Json to System.Text.Json - Ability to configure the packages filter - Add REST API tests
- Loading branch information
Showing
37 changed files
with
920 additions
and
536 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NUnit.Framework; | ||
using UnityNuGet.Npm; | ||
|
||
namespace UnityNuGet.Server.Tests | ||
{ | ||
public class ApiControllerTests | ||
{ | ||
private readonly UnityNuGetWebApplicationFactory _webApplicationFactory; | ||
|
||
public ApiControllerTests() | ||
{ | ||
_webApplicationFactory = new UnityNuGetWebApplicationFactory(); | ||
} | ||
|
||
[OneTimeTearDown] | ||
public void OneTimeTearDown() | ||
{ | ||
_webApplicationFactory.Dispose(); | ||
} | ||
|
||
[Test] | ||
public async Task Home_Success() | ||
{ | ||
using HttpClient client = _webApplicationFactory.CreateDefaultClient(); | ||
|
||
HttpResponseMessage response = await client.GetAsync("/"); | ||
|
||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Found)); | ||
Assert.That(response.Headers.Location, Is.EqualTo(new Uri("/-/all", UriKind.Relative))); | ||
|
||
string responseContent = await response.Content.ReadAsStringAsync(); | ||
|
||
Assert.That(responseContent, Is.Empty); | ||
} | ||
|
||
[Test] | ||
public async Task GetAll_Success() | ||
{ | ||
using HttpClient httpClient = _webApplicationFactory.CreateDefaultClient(); | ||
|
||
await WaitForInitialization(_webApplicationFactory.Services); | ||
|
||
HttpResponseMessage response = await httpClient.GetAsync("/-/all"); | ||
|
||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); | ||
|
||
string responseContent = await response.Content.ReadAsStringAsync(); | ||
|
||
NpmPackageListAllResponse npmPackageListAllResponse = JsonSerializer.Deserialize(responseContent, UnityNugetJsonSerializerContext.Default.NpmPackageListAllResponse)!; | ||
|
||
Assert.That(npmPackageListAllResponse.Packages, Has.Count.EqualTo(1)); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
string packageName = $"org.nuget.{UnityNuGetWebApplicationFactory.PackageName.ToLowerInvariant()}"; | ||
|
||
Assert.That(npmPackageListAllResponse.Packages.ContainsKey(packageName), Is.True); | ||
Assert.That(npmPackageListAllResponse.Packages[packageName].Name, Is.EqualTo(packageName)); | ||
Assert.That(npmPackageListAllResponse.Packages[packageName].Description, Is.Not.Null); | ||
Assert.That(npmPackageListAllResponse.Packages[packageName].Author, Is.Not.Null); | ||
}); | ||
} | ||
|
||
[Test] | ||
public async Task GetPackage_NotFound() | ||
{ | ||
using HttpClient httpClient = _webApplicationFactory.CreateDefaultClient(); | ||
|
||
await WaitForInitialization(_webApplicationFactory.Services); | ||
|
||
HttpResponseMessage response = await httpClient.GetAsync($"/InvalidPackageName"); | ||
|
||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); | ||
|
||
string responseContent = await response.Content.ReadAsStringAsync(); | ||
|
||
NpmError npmError = JsonSerializer.Deserialize(responseContent, UnityNugetJsonSerializerContext.Default.NpmError)!; | ||
|
||
Assert.That(npmError.Error, Is.EqualTo("not_found")); | ||
} | ||
|
||
[Test] | ||
public async Task GetPackage_Success() | ||
{ | ||
using HttpClient httpClient = _webApplicationFactory.CreateDefaultClient(); | ||
|
||
await WaitForInitialization(_webApplicationFactory.Services); | ||
|
||
string packageName = $"org.nuget.{UnityNuGetWebApplicationFactory.PackageName.ToLowerInvariant()}"; | ||
|
||
HttpResponseMessage response = await httpClient.GetAsync($"/{packageName}"); | ||
|
||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); | ||
|
||
string responseContent = await response.Content.ReadAsStringAsync(); | ||
|
||
NpmPackage npmPackage = JsonSerializer.Deserialize(responseContent, UnityNugetJsonSerializerContext.Default.NpmPackage)!; | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(npmPackage.Id, Is.EqualTo(packageName)); | ||
Assert.That(npmPackage.Revision, Is.Not.Null); | ||
Assert.That(npmPackage.Name, Is.EqualTo(packageName)); | ||
Assert.That(npmPackage.License, Is.Not.Null); | ||
Assert.That(npmPackage.Description, Is.Not.Null); | ||
}); | ||
} | ||
|
||
[Test] | ||
[TestCase("org.nuget.newtonsoft.json", "InvalidFile")] | ||
[TestCase("InvalidId", "org.nuget.newtonsoft.json-11.0.1.tgz")] | ||
[TestCase("org.nuget.newtonsoft.json", "org.nuget.newtonsoft.json_11.0.1.tgz")] | ||
[TestCase("org.nuget.newtonsoft.json", "org.nuget.newtonsoft.json-11.0.1.InvalidExtension")] | ||
public async Task DownloadPackage_NotFound(string id, string file) | ||
{ | ||
using HttpClient httpClient = _webApplicationFactory.CreateDefaultClient(); | ||
|
||
await WaitForInitialization(_webApplicationFactory.Services); | ||
|
||
HttpResponseMessage response = await httpClient.GetAsync($"/{id}/-/{file}"); | ||
|
||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); | ||
|
||
string responseContent = await response.Content.ReadAsStringAsync(); | ||
|
||
NpmError npmError = JsonSerializer.Deserialize(responseContent, UnityNugetJsonSerializerContext.Default.NpmError)!; | ||
|
||
Assert.That(npmError.Error, Is.EqualTo("not_found")); | ||
} | ||
|
||
[Test] | ||
public async Task DownloadPackage_Head_Success() | ||
{ | ||
using HttpClient httpClient = _webApplicationFactory.CreateDefaultClient(); | ||
|
||
await WaitForInitialization(_webApplicationFactory.Services); | ||
|
||
string packageName = $"org.nuget.{UnityNuGetWebApplicationFactory.PackageName.ToLowerInvariant()}"; | ||
|
||
HttpRequestMessage httpRequestMessage = new() | ||
{ | ||
RequestUri = new Uri($"/{packageName}/-/{packageName}-11.0.1.tgz", UriKind.Relative), | ||
Method = HttpMethod.Head | ||
}; | ||
|
||
HttpResponseMessage response = await httpClient.SendAsync(httpRequestMessage); | ||
|
||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); | ||
|
||
byte[] responseContent = await response.Content.ReadAsByteArrayAsync(); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(responseContent, Is.Empty); | ||
|
||
Assert.That(response.Content.Headers.ContentType!.MediaType, Is.EqualTo("application/octet-stream")); | ||
Assert.That(response.Content.Headers.ContentLength, Is.GreaterThan(0)); | ||
}); | ||
} | ||
|
||
[Test] | ||
public async Task DownloadPackage_Get_Success() | ||
{ | ||
using HttpClient httpClient = _webApplicationFactory.CreateDefaultClient(); | ||
|
||
await WaitForInitialization(_webApplicationFactory.Services); | ||
|
||
string packageName = $"org.nuget.{UnityNuGetWebApplicationFactory.PackageName.ToLowerInvariant()}"; | ||
|
||
HttpResponseMessage response = await httpClient.GetAsync($"/{packageName}/-/{packageName}-11.0.1.tgz"); | ||
|
||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); | ||
|
||
byte[] responseContent = await response.Content.ReadAsByteArrayAsync(); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(responseContent, Is.Not.Empty); | ||
|
||
Assert.That(response.Content.Headers.ContentType!.MediaType, Is.EqualTo("application/octet-stream")); | ||
Assert.That(response.Content.Headers.ContentLength, Is.GreaterThan(0)); | ||
}); | ||
} | ||
|
||
private static async Task WaitForInitialization(IServiceProvider serviceProvider) | ||
{ | ||
RegistryCacheSingleton registryCacheSingleton = serviceProvider.GetRequiredService<RegistryCacheSingleton>(); | ||
|
||
while (registryCacheSingleton.Instance == null) | ||
{ | ||
await Task.Delay(25); | ||
} | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/UnityNuGet.Server.Tests/UnityNuGet.Server.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
<IsPublishable>false</IsPublishable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" /> | ||
<PackageReference Include="nunit" /> | ||
<PackageReference Include="NUnit.Analyzers"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="NUnit3TestAdapter" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\UnityNuGet.Server\UnityNuGet.Server.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
34 changes: 34 additions & 0 deletions
34
src/UnityNuGet.Server.Tests/UnityNuGetWebApplicationFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace UnityNuGet.Server.Tests | ||
{ | ||
internal class UnityNuGetWebApplicationFactory : WebApplicationFactory<Program> | ||
{ | ||
public const string PackageName = "Newtonsoft.Json"; | ||
|
||
protected override void ConfigureWebHost(IWebHostBuilder builder) | ||
{ | ||
base.ConfigureWebHost(builder); | ||
|
||
builder.ConfigureAppConfiguration(builder => | ||
{ | ||
builder.AddInMemoryCollection(new Dictionary<string, string?> | ||
{ | ||
{ WebHostDefaults.ServerUrlsKey, "http://localhost" } | ||
}); | ||
}); | ||
|
||
builder.ConfigureServices(services => | ||
{ | ||
services.Configure<RegistryOptions>(options => | ||
{ | ||
options.Filter = PackageName; | ||
}); | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.