{"id":206130,"date":"2020-12-07T10:48:09","date_gmt":"2020-12-07T02:48:09","guid":{"rendered":"https:\/\/gulass.cn\/?p=206130"},"modified":"2020-11-29T19:49:05","modified_gmt":"2020-11-29T11:49:05","slug":"asp-net-testserver","status":"publish","type":"post","link":"https:\/\/gulass.cn\/asp-net-testserver.html","title":{"rendered":"asp.net core \u4f7f\u7528 TestServer \u6765\u505a\u96c6\u6210\u6d4b\u8bd5"},"content":{"rendered":"
\u5bfc\u8bfb<\/td>\n | \n\u4e4b\u524d\u6211\u7684\u9879\u76ee\u91cc\u7684\u96c6\u6210\u6d4b\u8bd5\u662f\u968f\u673a\u4e00\u4e2a\u7aef\u53e3\uff0c\u6bcf\u6b21\u90fd\u771f\u5b9e\u7684\u542f\u52a8\u4e00\u4e2a WebServer\uff0c\u4e4b\u524d\u4e5f\u6709\u770b\u5230\u8fc7\u5fae\u8f6f\u6587\u6863\u4e0a TestServer \u7684\u4ecb\u7ecd\uff0c\u5f53\u65f6\u6ca1\u4ed4\u7ec6\u770b\u8fc7\u4ee5\u4e3a\u5dee\u4e0d\u591a\u5c31\u6ca1\u7528\uff0c\u4e00\u76f4\u662f\u542f\u52a8\u4e86\u4e00\u4e2a\u771f\u6b63\u7684 WebServer \u53bb\u8dd1\u96c6\u6210\u6d4b\u8bd5\u7684\uff0c\u4e0a\u6b21\u5206\u4eab Xunit.DependencyInjection \u6539\u9020\u6d4b\u8bd5\u9879\u76ee\u7684\u65f6\u5019\uff0c\u5199\u7684\u70c2\u4ee3\u7801\u88ab\u5927\u5e08\u770b\u5230\u4e86\u4e4b\u540e\uff0c \u5927\u5e08\u5efa\u8bae\u7528 TestServer \u6765\u505a\u96c6\u6210\u6d4b\u8bd5\uff0c\u4f7f\u7528 TestServer \u4e0d\u4f1a\u771f\u6b63\u7684\u5360\u7528\u7aef\u53e3\u53f7\uff0c\u4e0d\u4f1a\u51fa\u73b0\u6743\u9650\u95ee\u9898\uff0c\u4e8e\u662f\u6252\u4e86\u6252 TestServer \u7684\u6e90\u7801\uff0c\u5e76\u7528 TestServer \u6539\u8fdb\u4e86\u96c6\u6210\u6d4b\u8bd5\u9879\u76ee\uff0c\u611f\u8c22\u5927\u5e08[\u732e\u82b1\u9c9c\u82b1]~~<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n Sample<\/strong><\/div>\n \u4e4b\u524d\u7684\u96c6\u6210\u6d4b\u8bd5\u76d1\u542c\u4e86\u4e00\u4e2a\u7aef\u53e3\u53f7\uff0c\u4f7f\u7528\u4e86\u4e00\u4e2a\u771f\u5b9e\u7684 WebServer\uff0c\u4e0b\u9762\u6539\u6210\u4f7f\u7528 TestServer<\/p>\n TestServer \u73b0\u5728\u662f\u5728 Microsoft.AspNetCore.TestHost \u8fd9\u4e2a Nuget \u5305\u4e2d\uff0c\u5f15\u7528\u8fd9\u4e2a\u5305\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e86<\/p>\n \u5728\u670d\u52a1\u6ce8\u518c\u7684\u65f6\u5019\u8c03\u7528 UseTestServer \u8fd9\u4e00\u6269\u5c55\u65b9\u6cd5\u5c31\u53ef\u4ee5\u6ce8\u5165 TestServer \u4e86\uff0c\u96c6\u6210\u6d4b\u8bd5\u4e00\u822c\u4f1a\u7528 HttpClient \u6765\u8bf7\u6c42\u670d\u52a1\u5668\u7aef\u7684 API \u5730\u5740\u6216\u9875\u9762\uff0cTestServer \u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b9\u4fbf\u7684 CreateClient \u7684\u65b9\u6cd5\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u521b\u5efa\u4e00\u4e2a\u7528\u6765\u8bf7\u6c42 TestServer \u7684 HttpClient\uff0c\u5fae\u8f6f\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6bd4\u8f83\u65b9\u4fbf\u7684\u6269\u5c55\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528 IHost \u7684 GetTestClient \u6269\u5c55\u65b9\u6cd5\u6765\u83b7\u53d6 HttpClient<\/p>\n \u6539\u6210\u4f7f\u7528 TestServer \u5f88\u7b80\u5355\uff0c\u5f15\u7528 nuget \u5305 Microsoft.AspNetCore.TestHost\uff0c\u53d8\u66f4\u5bf9\u6bd4\u5982\u4e0b\uff1a<\/p>\n <\/p>\n \u6e90\u7801\u6982\u89c8<\/strong><\/div>\n TestServer \u5728\u542f\u52a8\u7684\u65f6\u5019\u5e76\u6ca1\u6709\u76d1\u542c\u7aef\u53e3\uff0c\u53ef\u4ee5\u53c2\u8003\u6e90\u7801 IServer \u7684 Start<\/p>\n <\/p>\n TestServer \u901a\u8fc7 CreateClient \u65b9\u6cd5\u6765\u521b\u5efa\u8c03\u7528 TestServer \u63a5\u53e3\u7684 HttpClient<\/p>\n <\/p>\n \u901a\u8fc7\u4e0a\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u770b\u7684\u51fa\u6765\u6838\u5fc3\u4ee3\u7801\u662f\u5728 ClientHandler \u4e2d\u5b9a\u4e49\u7684\u3002<\/p>\n <\/p>\n ClientHandler \u91cd\u5199\u4e86 HttpClientHandler \u7684 SendAsync \u65b9\u6cd5\uff0c\u4f7f\u5f97\u8bf7\u6c42\u76f4\u63a5\u62e6\u622a\u6389\uff0c\u4e0d\u4f1a\u771f\u6b63\u7684\u53d1\u751f Http \u8bf7\u6c42\uff0c\u5b9e\u9645\u7684\u8bf7\u6c42\u8fc7\u7a0b\u9996\u5148\u5c06 Http \u8bf7\u6c42\u7684\u4fe1\u606f\u8f6c\u6362\u6210 HttpRquestFeature \u7136\u540e\u76f4\u63a5\u4ea4\u7ed9 TestServer \u5904\u7406\uff0c\u5176\u5b9e\u4e5f\u5c31\u662f\u76f4\u63a5\u4ea4\u7ed9 asp.net core \u7684\u8bf7\u6c42\u7ba1\u9053\u53bb\u5904\u7406\uff0c\u8bf7\u6c42\u5904\u7406\u7ed3\u675f\u4e4b\u540e\uff0c\u83b7\u53d6 HttpContext \u54cd\u5e94\uff0c\u83b7\u53d6 HttpResponseFeature \u7136\u540e\u8f6c\u6362\u6210 HttpClient \u9700\u8981\u7684 HttpResponseMessage.<\/p>\n More<\/strong><\/div>\n TestServer \u4e0d\u4ec5\u4ec5\u53ef\u4ee5\u652f\u6301 HTTP \u8bf7\u6c42\u7684\u5904\u7406\uff0c\u8fd8\u652f\u6301 WebSocket \u7684\u5904\u7406\uff0cWebSocket \u7684\u96c6\u6210\u6d4b\u8bd5\u4e5f\u53ef\u4ee5\u4f7f\u7528 TestServer \u6765\u5904\u7406\u3002<\/p>\n \u4f60\u5982\u679c\u8fd8\u662f\u6bd4\u8f83\u6000\u7591\u662f\u5426\u771f\u7684\u6ca1\u6709 HTTP \u8bf7\u6c42\uff0c\u53ef\u4ee5\u7528 Fildder \u4e4b\u7c7b\u7684 HTTP \u6293\u5305\u5de5\u5177\u76d1\u63a7\u5728\u8dd1\u6d4b\u8bd5\u7684\u671f\u95f4\u662f\u5426\u771f\u7684\u6709 HTTP \u8bf7\u6c42\uff0c\u5982\u679c\u662f\u771f\u6b63\u7684 WebServer \u4f1a\u6709 HTTP \u8bf7\u6c42\uff0cTestServer \u4e0d\u4f1a\u6709 HTTP \u8bf7\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":" \u4e4b\u524d\u7684\u96c6\u6210\u6d4b\u8bd5\u76d1\u542c\u4e86\u4e00\u4e2a\u7aef\u53e3\u53f7\uff0c\u4f7f\u7528\u4e86\u4e00\u4e2a\u771f\u5b9e\u7684 WebServer\uff0c\u4e0b\u9762\u6539\u6210\u4f7f\u7528 TestServer Te […]<\/p>\n","protected":false},"author":1903,"featured_media":206153,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-206130","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/206130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=206130"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/206130\/revisions"}],"predecessor-version":[{"id":206156,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/206130\/revisions\/206156"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/206153"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=206130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=206130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=206130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}} |