我需要为主体为字节的请求构建一个 WireMock.Net 服务器。我尝试了匹配器 .WithBody 与转换器 Encoding.ASCII.GetBytes (和 GetString)的结合,但没有成功。
谢谢您的帮助
接受字节数组
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();
}
}