public static TestHttpMessageHandler CreateDefault() { var testHttpMessageHandler = new TestHttpMessageHandler(); var deleteCts = new CancellationTokenSource(); testHttpMessageHandler.OnSocketSend((_, __) => ResponseUtils.CreateResponse(HttpStatusCode.Accepted)); testHttpMessageHandler.OnLongPoll(async cancellationToken => { var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, deleteCts.Token); // Just block until canceled var tcs = new TaskCompletionSource(); using (cts.Token.Register(() => tcs.TrySetResult())) { await tcs.Task; } return(ResponseUtils.CreateResponse(HttpStatusCode.NoContent)); }); testHttpMessageHandler.OnRequest((request, next, cancellationToken) => { if (request.Method.Equals(HttpMethod.Delete) && request.RequestUri.PathAndQuery.Contains("id=")) { deleteCts.Cancel(); return(Task.FromResult(ResponseUtils.CreateResponse(HttpStatusCode.Accepted))); } return(next()); }); return(testHttpMessageHandler); }
public async Task CanReceiveDataEvenIfExceptionThrownSynchronouslyFromPreviousReceivedEvent() { var testHttpHandler = new TestHttpMessageHandler(); testHttpHandler.OnLongPoll(cancellationToken => ResponseUtils.CreateResponse(HttpStatusCode.OK, "42")); testHttpHandler.OnSocketSend((_, __) => ResponseUtils.CreateResponse(HttpStatusCode.Accepted)); await WithConnectionAsync( CreateConnection(testHttpHandler), async (connection, closed) => { var receiveTcs = new TaskCompletionSource <string>(); var receivedRaised = false; connection.OnReceived((data, state) => { if (!receivedRaised) { receivedRaised = true; throw new InvalidOperationException(); } receiveTcs.TrySetResult(Encoding.UTF8.GetString(data)); return(Task.CompletedTask); }, receiveTcs); await connection.StartAsync(TransferFormat.Text).OrTimeout(); Assert.Contains("42", await receiveTcs.Task.OrTimeout()); Assert.True(receivedRaised); }); }
public async Task CanSendData() { var data = new byte[] { 1, 1, 2, 3, 5, 8 }; var testHttpHandler = new TestHttpMessageHandler(); var sendTcs = new TaskCompletionSource <byte[]>(); var longPollTcs = new TaskCompletionSource <HttpResponseMessage>(); testHttpHandler.OnLongPoll(cancellationToken => longPollTcs.Task); testHttpHandler.OnSocketSend((buf, cancellationToken) => { sendTcs.TrySetResult(buf); return(Task.FromResult(ResponseUtils.CreateResponse(HttpStatusCode.Accepted))); }); await WithConnectionAsync( CreateConnection(testHttpHandler), async (connection) => { await connection.StartAsync().OrTimeout(); await connection.Transport.Output.WriteAsync(data).OrTimeout(); Assert.Equal(data, await sendTcs.Task.OrTimeout()); longPollTcs.TrySetResult(ResponseUtils.CreateResponse(HttpStatusCode.NoContent)); }); }
public async Task CanReceiveData() { var testHttpHandler = new TestHttpMessageHandler(); // Set the long poll up to return a single message over a few polls. var requestCount = 0; var messageFragments = new[] { "This ", "is ", "a ", "test" }; testHttpHandler.OnLongPoll(cancellationToken => { if (requestCount >= messageFragments.Length) { return(ResponseUtils.CreateResponse(HttpStatusCode.NoContent)); } var resp = ResponseUtils.CreateResponse(HttpStatusCode.OK, messageFragments[requestCount]); requestCount += 1; return(resp); }); testHttpHandler.OnSocketSend((_, __) => ResponseUtils.CreateResponse(HttpStatusCode.Accepted)); await WithConnectionAsync( CreateConnection(testHttpHandler), async (connection) => { await connection.StartAsync().OrTimeout(); Assert.Contains("This is a test", Encoding.UTF8.GetString(await connection.Transport.Input.ReadAllAsync())); }); }
public async Task ExceptionOnSendAsyncClosesWithError() { var testHttpHandler = new TestHttpMessageHandler(); var longPollTcs = new TaskCompletionSource <HttpResponseMessage>(TaskCreationOptions.RunContinuationsAsynchronously); testHttpHandler.OnLongPoll(cancellationToken => { cancellationToken.Register(() => longPollTcs.TrySetResult(null)); return(longPollTcs.Task); }); testHttpHandler.OnSocketSend((buf, cancellationToken) => { return(Task.FromResult(ResponseUtils.CreateResponse(HttpStatusCode.InternalServerError))); }); await WithConnectionAsync( CreateConnection(testHttpHandler), async (connection, closed) => { await connection.StartAsync().OrTimeout(); await connection.SendAsync(new byte[] { 0 }).OrTimeout(); var exception = await Assert.ThrowsAsync <HttpRequestException>(() => closed.OrTimeout()); }); }
public async Task TransportPipeIsCompletedWhenErrorOccursInTransport() { using (StartLog(out var loggerFactory)) { var httpHandler = new TestHttpMessageHandler(); var longPollResult = new TaskCompletionSource <HttpResponseMessage>(); httpHandler.OnLongPoll(cancellationToken => { cancellationToken.Register(() => { longPollResult.TrySetResult(ResponseUtils.CreateResponse(HttpStatusCode.NoContent)); }); return(longPollResult.Task); }); httpHandler.OnSocketSend((data, _) => { Assert.Collection(data, i => Assert.Equal(0x42, i)); return(Task.FromResult(ResponseUtils.CreateResponse(HttpStatusCode.InternalServerError))); }); await WithConnectionAsync( CreateConnection(httpHandler, loggerFactory), async (connection) => { await connection.StartAsync(TransferFormat.Text).OrTimeout(); await connection.Transport.Output.WriteAsync(new byte[] { 0x42 }).OrTimeout(); // We should get the exception in the transport input completion. await Assert.ThrowsAsync <HttpRequestException>(() => connection.Transport.Input.WaitForWriterToComplete()); }); } }
public static HttpMessageHandler CreateDefault() { var testHttpMessageHandler = new TestHttpMessageHandler(); testHttpMessageHandler.OnSocketSend((_, __) => ResponseUtils.CreateResponse(HttpStatusCode.Accepted)); testHttpMessageHandler.OnLongPoll(async cancellationToken => { // Just block until canceled var tcs = new TaskCompletionSource <object>(); using (cancellationToken.Register(() => tcs.TrySetResult(null))) { await tcs.Task; } return(ResponseUtils.CreateResponse(HttpStatusCode.NoContent)); }); return(testHttpMessageHandler); }
public async Task TransportPipeIsCompletedWhenErrorOccursInTransport() { bool ExpectedErrors(WriteContext writeContext) { return(writeContext.LoggerName == typeof(LongPollingTransport).FullName && writeContext.EventId.Name == "ErrorSending"); } using (StartVerifiableLog(expectedErrorsFilter: ExpectedErrors)) { var httpHandler = new TestHttpMessageHandler(); var longPollResult = new TaskCompletionSource <HttpResponseMessage>(); httpHandler.OnLongPoll(cancellationToken => { cancellationToken.Register(() => { longPollResult.TrySetResult(ResponseUtils.CreateResponse(HttpStatusCode.NoContent)); }); return(longPollResult.Task); }); httpHandler.OnLongPollDelete(cancellationToken => ResponseUtils.CreateResponse(HttpStatusCode.NoContent)); httpHandler.OnSocketSend((data, _) => { Assert.Collection(data, i => Assert.Equal(0x42, i)); return(Task.FromResult(ResponseUtils.CreateResponse(HttpStatusCode.InternalServerError))); }); await WithConnectionAsync( CreateConnection(httpHandler, LoggerFactory), async (connection) => { await connection.StartAsync().OrTimeout(); await connection.Transport.Output.WriteAsync(new byte[] { 0x42 }).OrTimeout(); // We should get the exception in the transport input completion. await Assert.ThrowsAsync <HttpRequestException>(() => connection.Transport.Input.WaitForWriterToComplete()).OrTimeout(); }); } }
public async Task CanReceiveData() { var testHttpHandler = new TestHttpMessageHandler(); testHttpHandler.OnLongPoll(cancellationToken => ResponseUtils.CreateResponse(HttpStatusCode.OK, "42")); testHttpHandler.OnSocketSend((_, __) => ResponseUtils.CreateResponse(HttpStatusCode.Accepted)); await WithConnectionAsync( CreateConnection(testHttpHandler), async (connection, closed) => { var receiveTcs = new TaskCompletionSource <string>(); connection.OnReceived((data, state) => { var tcs = ((TaskCompletionSource <string>)state); tcs.TrySetResult(Encoding.UTF8.GetString(data)); return(Task.CompletedTask); }, receiveTcs); await connection.StartAsync(TransferFormat.Text).OrTimeout(); Assert.Contains("42", await receiveTcs.Task.OrTimeout()); }); }
public async Task CanStartConnectionAfterConnectionStoppedWithError() { using (StartLog(out var loggerFactory)) { var httpHandler = new TestHttpMessageHandler(); var longPollResult = new TaskCompletionSource <HttpResponseMessage>(); httpHandler.OnLongPoll(cancellationToken => { cancellationToken.Register(() => { longPollResult.TrySetResult(ResponseUtils.CreateResponse(HttpStatusCode.NoContent)); }); return(longPollResult.Task); }); httpHandler.OnSocketSend((data, _) => { Assert.Collection(data, i => Assert.Equal(0x42, i)); return(Task.FromResult(ResponseUtils.CreateResponse(HttpStatusCode.InternalServerError))); }); await WithConnectionAsync( CreateConnection(httpHandler, loggerFactory), async (connection, closed) => { await connection.StartAsync(TransferFormat.Text).OrTimeout(); await connection.SendAsync(new byte[] { 0x42 }).OrTimeout(); // Wait for the connection to close, because the send failed. await Assert.ThrowsAsync <HttpRequestException>(() => closed.OrTimeout()); // Start it up again await connection.StartAsync(TransferFormat.Text).OrTimeout(); }); } }