Wiremock.net (C#) 当主体是字节数组时如何构建请求匹配器

问题描述 投票:0回答:1

我需要为主体为字节的请求构建一个 WireMock.Net 服务器。我尝试了匹配器 .WithBody 与转换器 Encoding.ASCII.GetBytes (和 GetString)的结合,但没有成功。

谢谢您的帮助

c# byte matching wiremock.net
1个回答
0
投票

接受字节数组

WithBody
的方法
byte[]
有重载。请参阅下面的示例:

public class UnitTest1 : IDisposable
{
    private readonly WireMockServer _wireMock;
    private readonly HttpClient _client;

    public UnitTest1()
    {
        byte[] bytes = [123, 167, 184]; // some random bytes
        _wireMock = WireMockServer.Start();
        _wireMock
            .Given(
                Request.Create()
                    .WithPath("/test")
                    .UsingGet())
            .RespondWith(
                Response.Create()
                    .WithStatusCode(200)
                    .WithHeader("Content-Type", "application/octet-stream")
                    .WithBody(bytes)
            );
        _client = _wireMock.CreateClient();
    }

    [Fact]
    public async void Test1()
    {
        var response = await _client.GetAsync("/test");
        var bytes = await response.Content.ReadAsByteArrayAsync();

        var content = Encoding.UTF8.GetString(bytes);
    }

    public void Dispose()
    {
        _wireMock?.Dispose();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.