零度框架 1.0 版本正式发布,共 184 项改进。

This commit is contained in:
administrator committed 2024-05-31 12:28:02 +08:00
1 parent c14a872b77
commit baf06cd88f
182 files changed
+2537 -1613

No files matched your search

@@ -0,0 +1,128 @@
// Copyright (c) HelloShop Corporation. All rights reserved.
// See the license file in the project root for more information.
using Microsoft.AspNetCore.Authentication.BearerToken;
using System.Dynamic;
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json.Nodes;
namespace HelloShop.FunctionalTests
{
public class FirstWebApiIntegrationTest
{
[Fact]
public async Task WebAppRootReturnsOkStatusCode()
{
// Arrange
IDistributedApplicationTestingBuilder appHost = await DistributedApplicationTestingBuilder.CreateAsync<Projects.HelloShop_AppHost>();
await using DistributedApplication app = await appHost.BuildAsync();
await app.StartAsync();
// Act
HttpClient httpClient = app.CreateHttpClient("webapp");
HttpResponseMessage response = await httpClient.GetAsync("/");
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
[Fact]
public async Task IdetityServiceAccountLoginReturnsSuccessStatusCode()
{
// Arrange
IDistributedApplicationTestingBuilder appHost = await DistributedApplicationTestingBuilder.CreateAsync<Projects.HelloShop_AppHost>();
await using DistributedApplication app = await appHost.BuildAsync();
await app.StartAsync();
// Act
HttpClient httpClient = app.CreateHttpClient("identityservice");
HttpResponseMessage response = await httpClient.PostAsJsonAsync("api/Account/Login", new
{
UserName = "guest",
Password = "guest"
});
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
[Fact]
public async Task IdetityServiceAccountLoginReturnsAccessToken()
{
// Arrange
IDistributedApplicationTestingBuilder appHost = await DistributedApplicationTestingBuilder.CreateAsync<Projects.HelloShop_AppHost>();
await using DistributedApplication app = await appHost.BuildAsync();
await app.StartAsync();
// Act
HttpClient httpClient = app.CreateHttpClient("identityservice");
HttpResponseMessage response = await httpClient.PostAsJsonAsync("api/Account/Login", new
{
UserName = "guest",
Password = "guest"
});
dynamic? result = await response.Content.ReadFromJsonAsync<ExpandoObject>();
// Assert
Assert.NotNull(result?.accessToken);
}
[Fact]
public async Task IdetityServiceAccountLoginReturnsTokenExpiresInSeconds()
{
// Arrange
IDistributedApplicationTestingBuilder appHost = await DistributedApplicationTestingBuilder.CreateAsync<Projects.HelloShop_AppHost>();
await using DistributedApplication app = await appHost.BuildAsync();
await app.StartAsync();
// Act
HttpClient httpClient = app.CreateHttpClient("identityservice");
HttpResponseMessage response = await httpClient.PostAsJsonAsync("api/Account/Login", new
{
UserName = "guest",
Password = "guest"
});
JsonNode? result = await response.Content.ReadFromJsonAsync<JsonNode>();
int expiresInSeconds = result?["expiresIn"]?.GetValue<int>() ?? default;
// Assert
Assert.Equal(3600, expiresInSeconds);
}
[Fact]
public async Task ProductServiceGetProductReturnsProductDetails()
{
// Arrange
IDistributedApplicationTestingBuilder appHost = await DistributedApplicationTestingBuilder.CreateAsync<Projects.HelloShop_AppHost>();
await using DistributedApplication app = await appHost.BuildAsync();
await app.StartAsync();
// Act
HttpClient identityServiceHttpClient = app.CreateHttpClient("identityservice");
HttpResponseMessage loginResponse = await identityServiceHttpClient.PostAsJsonAsync("api/Account/Login", new
{
UserName = "admin",
Password = "admin"
});
AccessTokenResponse? accessTokenResponse = await loginResponse.Content.ReadFromJsonAsync<AccessTokenResponse>();
HttpClient productServiceHttpClient = app.CreateHttpClient("productservice");
productServiceHttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessTokenResponse?.AccessToken);
HttpResponseMessage productDetailsResponse = await productServiceHttpClient.GetAsync("api/Products/1");
JsonNode? result = await productDetailsResponse.Content.ReadFromJsonAsync<JsonNode>();
string? productName = result?["Name"]?.GetValue<string>();
// Assert
Assert.NotNull(productName);
Assert.Equal("Product 1", productName);
}
}
}
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Aspire.Hosting.Testing" Version="8.0.0" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\HelloShop.AppHost\HelloShop.AppHost.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="Aspire.Hosting.Testing" />
<Using Include="Xunit" />
</ItemGroup>
</Project>