Skip to content

Commit fbecab1

Browse files
Testserver limitations (#18283)
* Testserver limitations * Testserver limitations * Testserver limitations * Update aspnetcore/test/middleware.md Co-authored-by: Chris Ross <Tratcher@Outlook.com> * final fix Co-authored-by: Chris Ross <Tratcher@Outlook.com>
1 parent 6d607a0 commit fbecab1

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

aspnetcore/test/middleware.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ author: tratcher
44
description: Learn how to test ASP.NET Core middleware with TestServer.
55
ms.author: riande
66
ms.custom: mvc
7-
ms.date: 5/6/2019
7+
ms.date: 5/12/2020
88
no-loc: [Blazor, "Identity", "Let's Encrypt", Razor, SignalR]
99
uid: test/middleware
1010
---
@@ -103,3 +103,20 @@ public async Task TestMiddleware_ExpectedResponse()
103103
<xref:Microsoft.AspNetCore.TestHost.TestServer.SendAsync%2A> permits direct configuration of an <xref:Microsoft.AspNetCore.Http.HttpContext> object rather than using the <xref:System.Net.Http.HttpClient> abstractions. Use <xref:Microsoft.AspNetCore.TestHost.TestServer.SendAsync%2A> to manipulate structures only available on the server, such as [HttpContext.Items](xref:Microsoft.AspNetCore.Http.HttpContext.Items) or [HttpContext.Features](xref:Microsoft.AspNetCore.Http.HttpContext.Features).
104104

105105
As with the earlier example that tested for a *404 - Not Found* response, check the opposite for each `Assert` statement in the preceding test. The check confirms that the test fails correctly when the middleware is operating normally. After you've confirmed that the false positive test works, set the final `Assert` statements for the expected conditions and values of the test. Run it again to confirm that the test passes.
106+
107+
## TestServer limitations
108+
109+
TestServer:
110+
111+
* Was created to replicate server behaviors to test middleware.
112+
* Does ***not*** try to replicate all <xref:System.Net.Http.HttpClient> behaviors.
113+
* Attempts to give the client access to as much control over the server as possible, and with as much visibility into what's happening on the server as possible. For example it may throw exceptions not normally thrown by `HttpClient` in order to directly communicate server state.
114+
* Doesn't set some transport specific headers by default as those are not usually relevant to middleware. For more information, see the next section.
115+
116+
### Content-Length and Transfer-Encoding headers
117+
118+
TestServer does ***not*** set transport related request or response headers such as [Content-Length](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Length) or [Transfer-Encoding](https://developer.mozilla.org/docs/Web/HTTP/Headers/Transfer-Encoding). Applications should avoid depending on these headers because their usage varies by client, scenario, and protocol. If `Content-Length` and `Transfer-Encoding` are necessary to test a specific scenario, they can be specified in the test when composing the <xref:System.Net.Http.HttpRequestMessage> or <xref:Microsoft.AspNetCore.Http.HttpContext>. For more information, see the following GitHub issues:
119+
120+
* [dotnet/aspnetcore#21677](https://github.com/dotnet/aspnetcore/issues/21677)
121+
* [dotnet/aspnetcore#18463](https://github.com/dotnet/aspnetcore/issues/18463)
122+
* [dotnet/aspnetcore#13273](https://github.com/dotnet/aspnetcore/issues/13273)

0 commit comments

Comments
 (0)