/// <summary> /// AcceptAsync extension with timeout. This extension should be used in all tests to prevent /// unexpected hangs when a request does not arrive. /// </summary> internal static async Task <RequestContext> AcceptAsync(this HttpSysListener server, TimeSpan timeout) { var factory = new TestRequestContextFactory(server); using var acceptContext = new AsyncAcceptContext(server, factory); async Task <RequestContext> AcceptAsync() { while (true) { var requestContext = await server.AcceptAsync(acceptContext); if (server.ValidateRequest(requestContext)) { requestContext.InitializeFeatures(); return(requestContext); } } } var acceptTask = AcceptAsync(); var completedTask = await Task.WhenAny(acceptTask, Task.Delay(timeout)); if (completedTask == acceptTask) { return(await acceptTask); } else { server.Dispose(); throw new TimeoutException("AcceptAsync has timed out."); } }
public async Task Request_MultiplePrefixes(string requestUri, string expectedPathBase, string expectedPath) { // TODO: We're just doing this to get a dynamic port. This can be removed later when we add support for hot-adding prefixes. string root; var server = Utilities.CreateHttpServerReturnRoot("/", out root); server.Dispose(); server = new HttpSysListener(new HttpSysOptions(), new LoggerFactory()); using (server) { var uriBuilder = new UriBuilder(root); foreach (string path in new[] { "/", "/11", "/2/3", "/2", "/11/2" }) { server.Options.UrlPrefixes.Add(UrlPrefix.Create(uriBuilder.Scheme, uriBuilder.Host, uriBuilder.Port, path)); } server.Start(); Task <string> responseTask = SendRequestAsync(root + requestUri); var context = await server.AcceptAsync(Utilities.DefaultTimeout); var request = context.Request; Assert.Equal(expectedPath, request.Path); Assert.Equal(expectedPathBase, request.PathBase); context.Dispose(); string response = await responseTask; Assert.Equal(string.Empty, response); } }
/// <summary> /// AcceptAsync extension with timeout. This extension should be used in all tests to prevent /// unexpected hangs when a request does not arrive. /// </summary> internal static async Task <RequestContext> AcceptAsync(this HttpSysListener server, TimeSpan timeout) { var acceptTask = server.AcceptAsync(); var completedTask = await Task.WhenAny(acceptTask, Task.Delay(timeout)); if (completedTask == acceptTask) { return(await acceptTask); } else { server.Dispose(); throw new TimeoutException("AcceptAsync has timed out."); } }