gpt4 book ai didi

http - 从 IHttpActionResult 返回值获取 HTTP 状态

转载 作者:可可西里 更新时间:2023-11-01 17:17:43 30 4
gpt4 key购买 nike

在 C# 中,我究竟如何获取 HTTP 状态编号 (200) 并在这种情况下返回文本(“ok”)以对 API Controller 进行单元测试?我想我已经尝试了一切。这当然是一种普遍的需求。谢谢。

public IHttpActionResult Get(string appServer, string action)
{
return Ok("ok");
}

[TestMethod]
public void AppPoolTestStart()
{
AppPoolController controller = new AppPoolController();
IHttpActionResult result = controller.Get("MyServer", "start");
Assert.??? ; // check for 200 or "ok"
}

最佳答案

根据 Action Results in Web API 2 :

A Web API controller action can return any of the following:

  1. void
  2. HttpResponseMessage
  3. IHttpActionResult
  4. Some other type

Depending on which of these is returned, Web API uses a different mechanism to create the HTTP response.

void
Return empty 204 (No Content)

HttpResponseMessage
Convert directly to an HTTP response message.

IHttpActionResult
Call ExecuteAsync to create an HttpResponseMessage, then convert to an HTTP response message.

Other type Write the serialized return value into the response body; return 200 (OK).

...

IHttpActionResult

The IHttpActionResult interface was introduced in Web API 2. Essentially, it defines an HttpResponseMessage factory. Here are some advantages of using the IHttpActionResult interface:

  • Simplifies unit testing your controllers.
  • Moves common logic for creating HTTP responses into separate classes.
  • Makes the intent of the controller action clearer, by hiding the low-level details of constructing the response.

IHttpActionResult contains a single method, ExecuteAsync, which asynchronously creates an HttpResponseMessage instance.

public interface IHttpActionResult
{
Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken);
}

If a controller action returns an IHttpActionResult, Web API calls the ExecuteAsync method to create an HttpResponseMessage. Then it converts the HttpResponseMessage into an HTTP response message.

因此,在您的情况下,您可以调用 result.ExecuteAsync() 并等待生成的 Task 完成,然后获取其 HttpResponseMessage 对象。

但是,有一种不同的方法可以解决这个问题,下面的文档对此有更详细的介绍:

Unit Testing Controllers in ASP.NET Web API 2

特别是,请参阅关于 "Testing Actions that Return IHttpActionResult" 的部分:

In Web API 2, a controller action can return IHttpActionResult, which is analogous to ActionResult in ASP.NET MVC. The IHttpActionResult interface defines a command pattern for creating HTTP responses. Instead of creating the response directly, the controller returns an IHttpActionResult. Later, the pipeline invokes the IHttpActionResult to create the response. This approach makes it easier to write unit tests, because you can skip a lot of the setup that is needed for HttpResponseMessage.

在给出的示例中,他们只是将 IHttpActionResult 类型转换为他们正在测试的所需响应类型。例如:

// Act
IHttpActionResult actionResult = controller.Get(42);
var contentResult = actionResult as OkNegotiatedContentResult<Product>;

// Assert
Assert.IsNotNull(contentResult);
Assert.IsNotNull(contentResult.Content);
Assert.AreEqual(42, contentResult.Content.Id);

// Act
IHttpActionResult actionResult = controller.Get(10);

// Assert
Assert.IsInstanceOfType(actionResult, typeof(NotFoundResult));

// Act
IHttpActionResult actionResult = controller.Delete(10);

// Assert
Assert.IsInstanceOfType(actionResult, typeof(OkResult));

等等。

关于http - 从 IHttpActionResult 返回值获取 HTTP 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54316798/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com