PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/test/Microsoft.AspNet.Server.KestrelTests/EngineTests.cs

https://gitlab.com/latindignity/KestrelHttpServer
C# | 344 lines | 320 code | 21 blank | 3 comment | 13 complexity | 1c3d1b08f60dda74a0322fd49dd6c3e7 MD5 | raw file
  1. using Microsoft.AspNet.Server.Kestrel;
  2. using Microsoft.AspNet.Server.Kestrel.Http;
  3. using Microsoft.Framework.Runtime;
  4. using Microsoft.Framework.Runtime.Infrastructure;
  5. using System;
  6. using System.IO;
  7. using System.Net;
  8. using System.Net.Sockets;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using Xunit;
  12. namespace Microsoft.AspNet.Server.KestrelTests
  13. {
  14. /// <summary>
  15. /// Summary description for EngineTests
  16. /// </summary>
  17. internal class EngineTests
  18. {
  19. private async Task App(Frame frame)
  20. {
  21. for (; ;)
  22. {
  23. var buffer = new byte[8192];
  24. var count = await frame.RequestBody.ReadAsync(buffer, 0, buffer.Length);
  25. if (count == 0)
  26. {
  27. break;
  28. }
  29. await frame.ResponseBody.WriteAsync(buffer, 0, count);
  30. }
  31. }
  32. ILibraryManager LibraryManager
  33. {
  34. get
  35. {
  36. try
  37. {
  38. var locator = CallContextServiceLocator.Locator;
  39. if (locator == null)
  40. {
  41. return null;
  42. }
  43. var services = locator.ServiceProvider;
  44. if (services == null)
  45. {
  46. return null;
  47. }
  48. return (ILibraryManager)services.GetService(typeof(ILibraryManager));
  49. }
  50. catch (NullReferenceException)
  51. { return null; }
  52. }
  53. }
  54. private async Task AppChunked(Frame frame)
  55. {
  56. var data = new MemoryStream();
  57. for (; ;)
  58. {
  59. var buffer = new byte[8192];
  60. var count = await frame.RequestBody.ReadAsync(buffer, 0, buffer.Length);
  61. if (count == 0)
  62. {
  63. break;
  64. }
  65. data.Write(buffer, 0, count);
  66. }
  67. var bytes = data.ToArray();
  68. frame.ResponseHeaders["Content-Length"] = new[] { bytes.Length.ToString() };
  69. await frame.ResponseBody.WriteAsync(bytes, 0, bytes.Length);
  70. }
  71. [Fact]
  72. public async Task EngineCanStartAndStop()
  73. {
  74. var engine = new KestrelEngine(LibraryManager);
  75. engine.Start(1);
  76. engine.Dispose();
  77. }
  78. [Fact]
  79. public async Task ListenerCanCreateAndDispose()
  80. {
  81. var engine = new KestrelEngine(LibraryManager);
  82. engine.Start(1);
  83. var started = engine.CreateServer("http", "localhost", 54321, App);
  84. started.Dispose();
  85. engine.Dispose();
  86. }
  87. [Fact]
  88. public async Task ConnectionCanReadAndWrite()
  89. {
  90. var engine = new KestrelEngine(LibraryManager);
  91. engine.Start(1);
  92. var started = engine.CreateServer("http", "localhost", 54321, App);
  93. Console.WriteLine("Started");
  94. var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  95. socket.Connect(new IPEndPoint(IPAddress.Loopback, 54321));
  96. socket.Send(Encoding.ASCII.GetBytes("POST / HTTP/1.0\r\n\r\nHello World"));
  97. socket.Shutdown(SocketShutdown.Send);
  98. var buffer = new byte[8192];
  99. for (; ;)
  100. {
  101. var length = socket.Receive(buffer);
  102. if (length == 0) { break; }
  103. var text = Encoding.ASCII.GetString(buffer, 0, length);
  104. }
  105. started.Dispose();
  106. engine.Dispose();
  107. }
  108. [Fact]
  109. public async Task Http10()
  110. {
  111. using (var server = new TestServer(App))
  112. {
  113. using (var connection = new TestConnection())
  114. {
  115. await connection.SendEnd(
  116. "POST / HTTP/1.0",
  117. "",
  118. "Hello World");
  119. await connection.ReceiveEnd(
  120. "HTTP/1.0 200 OK",
  121. "",
  122. "Hello World");
  123. }
  124. }
  125. }
  126. [Fact]
  127. public async Task Http11()
  128. {
  129. using (var server = new TestServer(AppChunked))
  130. {
  131. using (var connection = new TestConnection())
  132. {
  133. await connection.SendEnd(
  134. "GET / HTTP/1.1",
  135. "",
  136. "GET / HTTP/1.1",
  137. "Connection: close",
  138. "",
  139. "Goodbye");
  140. await connection.ReceiveEnd(
  141. "HTTP/1.1 200 OK",
  142. "Content-Length: 0",
  143. "",
  144. "HTTP/1.1 200 OK",
  145. "Content-Length: 7",
  146. "Connection: close",
  147. "",
  148. "Goodbye");
  149. }
  150. }
  151. }
  152. [Fact]
  153. public async Task Http10ContentLength()
  154. {
  155. using (var server = new TestServer(App))
  156. {
  157. using (var connection = new TestConnection())
  158. {
  159. await connection.Send(
  160. "POST / HTTP/1.0",
  161. "Content-Length: 11",
  162. "",
  163. "Hello World");
  164. await connection.ReceiveEnd(
  165. "HTTP/1.0 200 OK",
  166. "",
  167. "Hello World");
  168. }
  169. }
  170. }
  171. [Fact]
  172. public async Task Http10TransferEncoding()
  173. {
  174. using (var server = new TestServer(App))
  175. {
  176. using (var connection = new TestConnection())
  177. {
  178. await connection.Send(
  179. "POST / HTTP/1.0",
  180. "Transfer-Encoding: chunked",
  181. "",
  182. "5", "Hello", "6", " World", "0\r\n");
  183. await connection.ReceiveEnd(
  184. "HTTP/1.0 200 OK",
  185. "",
  186. "Hello World");
  187. }
  188. }
  189. }
  190. [Fact]
  191. public async Task Http10KeepAlive()
  192. {
  193. using (var server = new TestServer(AppChunked))
  194. {
  195. using (var connection = new TestConnection())
  196. {
  197. await connection.SendEnd(
  198. "GET / HTTP/1.0",
  199. "Connection: keep-alive",
  200. "",
  201. "POST / HTTP/1.0",
  202. "",
  203. "Goodbye");
  204. await connection.Receive(
  205. "HTTP/1.0 200 OK",
  206. "Content-Length: 0",
  207. "Connection: keep-alive",
  208. "\r\n");
  209. await connection.ReceiveEnd(
  210. "HTTP/1.0 200 OK",
  211. "Content-Length: 7",
  212. "",
  213. "Goodbye");
  214. }
  215. }
  216. }
  217. [Fact]
  218. public async Task Http10KeepAliveContentLength()
  219. {
  220. using (var server = new TestServer(AppChunked))
  221. {
  222. using (var connection = new TestConnection())
  223. {
  224. await connection.SendEnd(
  225. "POST / HTTP/1.0",
  226. "Connection: keep-alive",
  227. "Content-Length: 11",
  228. "",
  229. "Hello WorldPOST / HTTP/1.0",
  230. "",
  231. "Goodbye");
  232. await connection.Receive(
  233. "HTTP/1.0 200 OK",
  234. "Content-Length: 11",
  235. "Connection: keep-alive",
  236. "",
  237. "Hello World");
  238. await connection.ReceiveEnd(
  239. "HTTP/1.0 200 OK",
  240. "Content-Length: 7",
  241. "",
  242. "Goodbye");
  243. }
  244. }
  245. }
  246. [Fact]
  247. public async Task Http10KeepAliveTransferEncoding()
  248. {
  249. using (var server = new TestServer(AppChunked))
  250. {
  251. using (var connection = new TestConnection())
  252. {
  253. await connection.SendEnd(
  254. "POST / HTTP/1.0",
  255. "Transfer-Encoding: chunked",
  256. "Connection: keep-alive",
  257. "",
  258. "5", "Hello", "6", " World", "0",
  259. "POST / HTTP/1.0",
  260. "",
  261. "Goodbye");
  262. await connection.Receive(
  263. "HTTP/1.0 200 OK",
  264. "Content-Length: 11",
  265. "Connection: keep-alive",
  266. "",
  267. "Hello World");
  268. await connection.ReceiveEnd(
  269. "HTTP/1.0 200 OK",
  270. "Content-Length: 7",
  271. "",
  272. "Goodbye");
  273. }
  274. }
  275. }
  276. [Fact]
  277. public async Task Expect100ContinueForBody()
  278. {
  279. using (var server = new TestServer(AppChunked))
  280. {
  281. using (var connection = new TestConnection())
  282. {
  283. await connection.Send(
  284. "POST / HTTP/1.1",
  285. "Expect: 100-continue",
  286. "Content-Length: 11",
  287. "Connection: close",
  288. "\r\n");
  289. await connection.Receive("HTTP/1.1 100 Continue", "\r\n");
  290. await connection.SendEnd("Hello World");
  291. await connection.Receive(
  292. "HTTP/1.1 200 OK",
  293. "Content-Length: 11",
  294. "Connection: close",
  295. "",
  296. "Hello World");
  297. }
  298. }
  299. }
  300. [Fact]
  301. public async Task DisconnectingClient()
  302. {
  303. using (var server = new TestServer(App))
  304. {
  305. var socket = new Socket(SocketType.Stream, ProtocolType.IP);
  306. socket.Connect(IPAddress.Loopback, 54321);
  307. await Task.Delay(200);
  308. socket.Disconnect(false);
  309. socket.Dispose();
  310. await Task.Delay(200);
  311. using (var connection = new TestConnection())
  312. {
  313. await connection.SendEnd(
  314. "GET / HTTP/1.0",
  315. "\r\n");
  316. await connection.ReceiveEnd(
  317. "HTTP/1.0 200 OK",
  318. "\r\n");
  319. }
  320. }
  321. }
  322. }
  323. }