测试 gRPC 购物车服务

This commit is contained in:
administrator committed 2024-07-11 20:57:59 +08:00
1 parent c192d209fd
commit 680ce10fa7
12 files changed
+494 -3

No files matched your search

@@ -0,0 +1,25 @@
// Copyright (c) HelloShop Corporation. All rights reserved.
// See the license file in the project root for more information.
using Grpc.Core;
using Grpc.Core.Interceptors;
using Microsoft.Net.Http.Headers;
namespace HelloShop.BasketService.FunctionalTests
{
internal class AuthenticatedInterceptor : Interceptor
{
public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(TRequest request, ClientInterceptorContext<TRequest, TResponse> context, AsyncUnaryCallContinuation<TRequest, TResponse> continuation)
{
const string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiIxIiwidW5pcXVlX25hbWUiOiJhZG1pbiIsInJvbGVpZCI6IjEiLCJuYmYiOjE3MjA1NzY3NDYsImV4cCI6MTc0MjYwODc0NiwiaWF0IjoxNzIwNTc2NzQ2fQ.ju_D3zeGLKqJYVckbb8Y3yNkp40nOqRAJrdOsISs4d4";
Metadata headers = [new Metadata.Entry(HeaderNames.Authorization, $"Bearer {token}")];
var newOptions = context.Options.WithHeaders(headers);
var newContext = new ClientInterceptorContext<TRequest, TResponse>(context.Method, context.Host, newOptions);
return base.AsyncUnaryCall(request, newContext, continuation);
}
}
}
@@ -0,0 +1,69 @@
// Copyright (c) HelloShop Corporation. All rights reserved.
// See the license file in the project root for more information.
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using Grpc.Core.Interceptors;
using Grpc.Net.Client;
using HelloShop.BasketService.Protos;
namespace HelloShop.BasketService.FunctionalTests
{
public class BasketServiceIntegrationTest
{
private readonly Basket.BasketClient _client;
public BasketServiceIntegrationTest()
{
GrpcChannel channel = GrpcChannel.ForAddress(GrpcConstants.GrpcAddress);
CallInvoker invoker = channel.Intercept(new AuthenticatedInterceptor());
_client = new Basket.BasketClient(invoker);
}
[Fact]
public async Task GetBasketReturnsBasket()
{
// Arrange
var request = new Empty();
// Act
var reply = await _client.GetBasketAsync(request);
// Assert
Assert.NotNull(reply);
}
[Fact]
public async Task UpdateBasketReturnsBasketResponse()
{
// Arrange
var request = new UpdateBasketRequest
{
Items =
{
new BasketListItem { ProductId = 1, Quantity = 2 },
new BasketListItem { ProductId = 2, Quantity = 3 }
}
};
// Act
var reply = await _client.UpdateBasketAsync(request);
// Assert
Assert.Equal(2, reply.Items.Count);
}
[Fact]
public async Task DeleteBasketReturnsEmpty()
{
// Arrange
var request = new Empty();
// Act
var reply = await _client.DeleteBasketAsync(request);
// Assert
Assert.NotNull(reply);
}
}
}
@@ -0,0 +1,25 @@
// Copyright (c) HelloShop Corporation. All rights reserved.
// See the license file in the project root for more information.
using Grpc.Net.Client;
using HelloShop.BasketService.Protos;
namespace HelloShop.BasketService.FunctionalTests
{
public class GreeterServiceIntegrationTest
{
[Fact]
public async Task SayHelloReturnsHelloMessage()
{
// Arrange
using var channel = GrpcChannel.ForAddress(GrpcConstants.GrpcAddress);
var client = new Greeter.GreeterClient(channel);
// Act
var reply = await client.SayHelloAsync(new HelloRequest { Name = "Greeter" });
// Assert
Assert.Equal("Hello Greeter", reply.Message);
}
}
}
@@ -0,0 +1,10 @@
// Copyright (c) HelloShop Corporation. All rights reserved.
// See the license file in the project root for more information.
namespace HelloShop.BasketService.FunctionalTests
{
internal class GrpcConstants
{
public const string GrpcAddress = "http://localhost:8004";
}
}
@@ -0,0 +1,39 @@
<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="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Google.Protobuf" Version="3.21.5" />
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.49.0" />
<PackageReference Include="Grpc.Tools" Version="2.49.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Grpc.JsonTranscoding" Version="8.0.7" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>
<ItemGroup>
<Protobuf Include="..\..\src\HelloShop.BasketService\Protos\basket.proto" GrpcServices="Client">
<Link>Protos\basket.proto</Link>
</Protobuf>
<Protobuf Include="..\..\src\HelloShop.BasketService\Protos\greet.proto" GrpcServices="Client">
<Link>Protos\greet.proto</Link>
</Protobuf>
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
</Project>
@@ -0,0 +1,46 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<IncludeHttpRuleProtos>true</IncludeHttpRuleProtos>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Google.Protobuf" Version="3.27.2" />
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.63.0" />
<PackageReference Include="Grpc.Tools" Version="2.64.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Grpc.JsonTranscoding" Version="8.0.7" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Protobuf Include="..\..\src\HelloShop.BasketService\Protos\basket.proto" GrpcServices="Client">
<Link>Protos\basket.proto</Link>
</Protobuf>
<Protobuf Include="..\..\src\HelloShop.BasketService\Protos\greet.proto" GrpcServices="Client">
<Link>Protos\greet.proto</Link>
</Protobuf>
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
</Project>