【问题标题】:Verify API Response验证 API 响应
【发布时间】:2019-07-12 17:56:17
【问题描述】:

我收到这样一个请求的响应: var response = command.PostCommand(testCommand);

我想验证响应是 json 格式,所以我这样做:

Assert.AreEqual("application/json", response.ContentType);

这种方式是否正确,还是我需要从内容类型标头响应中专门验证它?

【问题讨论】:

标签: rest restsharp


【解决方案1】:

您可以在反序列化之前使用IRestRequest.OnBeforeDeserialization 回调检查响应内容类型:

var request = new RestRequest(url)
    .AddQueryParameter(x, y); // whatever you need to configure

request.OnBeforeDeserialization = 
    response => CheckContentType(response.ContentType);

await client.PostAsync<MyResponse>(request);

【讨论】:

  • 我使用 Restsharp 发出请求,使用 Nunit 进行断言
猜你喜欢
  • 1970-01-01
  • 2017-02-23
  • 2018-06-17
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 2023-02-03
  • 2017-10-05
  • 1970-01-01
相关资源
最近更新 更多