public async Task ThrowingResultsIn500Response(ServiceContext testContext) { bool onStartingCalled = false; var testLogger = new TestApplicationErrorLogger(); testContext.Log = new KestrelTrace(testLogger); using (var server = new TestServer(frame => { var response = frame.Get <IHttpResponseFeature>(); response.OnStarting(_ => { onStartingCalled = true; return(Task.FromResult <object>(null)); }, null); // Anything added to the ResponseHeaders dictionary is ignored response.Headers.Clear(); response.Headers["Content-Length"] = "11"; throw new Exception(); }, testContext)) { using (var connection = new TestConnection()) { await connection.SendEnd( "GET / HTTP/1.1", "", "GET / HTTP/1.1", "Connection: close", "", ""); await connection.Receive( "HTTP/1.1 500 Internal Server Error", ""); await connection.ReceiveStartsWith("Date:"); await connection.Receive( "Content-Length: 0", "Server: Kestrel", "", "HTTP/1.1 500 Internal Server Error", ""); await connection.ReceiveStartsWith("Date:"); await connection.ReceiveEnd( "Content-Length: 0", "Server: Kestrel", "Connection: close", "", ""); Assert.False(onStartingCalled); Assert.Equal(2, testLogger.ApplicationErrorsLogged); } } }
public async Task ThrowingResultsIn500Response() { bool onStartingCalled = false; using (var server = new TestServer(frame => { frame.OnStarting(_ => { onStartingCalled = true; return(Task.FromResult <object>(null)); }, null); // Anything added to the ResponseHeaders dictionary is ignored frame.ResponseHeaders.Clear(); frame.ResponseHeaders["Content-Length"] = new[] { "11" }; throw new Exception(); })) { using (var connection = new TestConnection()) { await connection.SendEnd( "GET / HTTP/1.1", "", "GET / HTTP/1.1", "Connection: close", "", ""); await connection.Receive( "HTTP/1.1 500 Internal Server Error", ""); await connection.ReceiveStartsWith("Date:"); await connection.Receive( "Content-Length: 0", "Server: Kestrel", "", "HTTP/1.1 500 Internal Server Error", ""); await connection.ReceiveStartsWith("Date:"); await connection.ReceiveEnd( "Content-Length: 0", "Server: Kestrel", "Connection: close", "", ""); Assert.False(onStartingCalled); } } }
public async Task ThrowingInOnStartingResultsIn500Response() { using (var server = new TestServer(frame => { frame.OnStarting(_ => { throw new Exception(); }, null); frame.ResponseHeaders.Clear(); frame.ResponseHeaders["Content-Length"] = new[] { "11" }; // If we write to the response stream, we will not get a 500. return(Task.FromResult <object>(null)); })) { using (var connection = new TestConnection()) { await connection.SendEnd( "GET / HTTP/1.1", "", "GET / HTTP/1.1", "Connection: close", "", ""); await connection.Receive( "HTTP/1.1 500 Internal Server Error", ""); await connection.ReceiveStartsWith("Date:"); await connection.Receive( "Content-Length: 0", "Server: Kestrel", "", "HTTP/1.1 500 Internal Server Error", ""); await connection.ReceiveStartsWith("Date:"); await connection.ReceiveEnd( "Content-Length: 0", "Server: Kestrel", "Connection: close", "", ""); } } }
public async Task ThrowingInOnStartingResultsIn500Response() { using (var server = new TestServer(frame => { frame.OnStarting(_ => { throw new Exception(); }, null); frame.ResponseHeaders.Clear(); frame.ResponseHeaders["Content-Length"] = new[] { "11" }; // If we write to the response stream, we will not get a 500. return Task.FromResult<object>(null); })) { using (var connection = new TestConnection()) { await connection.SendEnd( "GET / HTTP/1.1", "", "GET / HTTP/1.1", "Connection: close", "", ""); await connection.Receive( "HTTP/1.1 500 Internal Server Error", ""); await connection.ReceiveStartsWith("Date:"); await connection.Receive( "Content-Length: 0", "Server: Kestrel", "", "HTTP/1.1 500 Internal Server Error", ""); await connection.ReceiveStartsWith("Date:"); await connection.ReceiveEnd( "Content-Length: 0", "Server: Kestrel", "Connection: close", "", ""); } } }
public async Task ThrowingResultsIn500Response() { bool onStartingCalled = false; using (var server = new TestServer(frame => { frame.OnStarting(_ => { onStartingCalled = true; return Task.FromResult<object>(null); }, null); // Anything added to the ResponseHeaders dictionary is ignored frame.ResponseHeaders.Clear(); frame.ResponseHeaders["Content-Length"] = new[] { "11" }; throw new Exception(); })) { using (var connection = new TestConnection()) { await connection.SendEnd( "GET / HTTP/1.1", "", "GET / HTTP/1.1", "Connection: close", "", ""); await connection.Receive( "HTTP/1.1 500 Internal Server Error", ""); await connection.ReceiveStartsWith("Date:"); await connection.Receive( "Content-Length: 0", "Server: Kestrel", "", "HTTP/1.1 500 Internal Server Error", ""); await connection.ReceiveStartsWith("Date:"); await connection.ReceiveEnd( "Content-Length: 0", "Server: Kestrel", "Connection: close", "", ""); Assert.False(onStartingCalled); } } }
public async Task ThrowingInOnStartingResultsInFailedWritesAnd500Response(ServiceContext testContext) { var onStartingCallCount1 = 0; var onStartingCallCount2 = 0; var failedWriteCount = 0; var testLogger = new TestApplicationErrorLogger(); testContext.Log = new KestrelTrace(testLogger); using (var server = new TestServer(async frame => { var onStartingException = new Exception(); var response = frame.Get<IHttpResponseFeature>(); response.OnStarting(_ => { onStartingCallCount1++; throw onStartingException; }, null); response.OnStarting(_ => { onStartingCallCount2++; throw onStartingException; }, null); response.Headers.Clear(); response.Headers["Content-Length"] = new[] { "11" }; var writeException = await Assert.ThrowsAsync<ObjectDisposedException>(async () => await response.Body.WriteAsync(Encoding.ASCII.GetBytes("Hello World"), 0, 11)); Assert.Same(onStartingException, writeException.InnerException); failedWriteCount++; }, testContext)) { using (var connection = new TestConnection()) { await connection.SendEnd( "GET / HTTP/1.1", "", "GET / HTTP/1.1", "Connection: close", "", ""); await connection.Receive( "HTTP/1.1 500 Internal Server Error", ""); await connection.ReceiveStartsWith("Date:"); await connection.Receive( "Content-Length: 0", "Server: Kestrel", "", "HTTP/1.1 500 Internal Server Error", ""); await connection.ReceiveStartsWith("Date:"); await connection.ReceiveEnd( "Content-Length: 0", "Server: Kestrel", "Connection: close", "", ""); Assert.Equal(2, onStartingCallCount1); // The second OnStarting callback should not be called since the first failed. Assert.Equal(0, onStartingCallCount2); Assert.Equal(2, testLogger.ApplicationErrorsLogged); } } }
public async Task ThrowingResultsIn500Response(ServiceContext testContext) { bool onStartingCalled = false; var testLogger = new TestApplicationErrorLogger(); testContext.Log = new KestrelTrace(testLogger); using (var server = new TestServer(frame => { var response = frame.Get<IHttpResponseFeature>(); response.OnStarting(_ => { onStartingCalled = true; return Task.FromResult<object>(null); }, null); // Anything added to the ResponseHeaders dictionary is ignored response.Headers.Clear(); response.Headers["Content-Length"] = "11"; throw new Exception(); }, testContext)) { using (var connection = new TestConnection()) { await connection.SendEnd( "GET / HTTP/1.1", "", "GET / HTTP/1.1", "Connection: close", "", ""); await connection.Receive( "HTTP/1.1 500 Internal Server Error", ""); await connection.ReceiveStartsWith("Date:"); await connection.Receive( "Content-Length: 0", "Server: Kestrel", "", "HTTP/1.1 500 Internal Server Error", ""); await connection.ReceiveStartsWith("Date:"); await connection.ReceiveEnd( "Content-Length: 0", "Server: Kestrel", "Connection: close", "", ""); Assert.False(onStartingCalled); Assert.Equal(2, testLogger.ApplicationErrorsLogged); } } }
public async Task ThrowingInOnStartingResultsInFailedWritesAnd500Response(ServiceContext testContext) { var onStartingCallCount1 = 0; var onStartingCallCount2 = 0; var failedWriteCount = 0; var testLogger = new TestApplicationErrorLogger(); testContext.Log = new KestrelTrace(testLogger); using (var server = new TestServer(async httpContext => { var onStartingException = new Exception(); var response = httpContext.Response; response.OnStarting(_ => { onStartingCallCount1++; throw onStartingException; }, null); response.OnStarting(_ => { onStartingCallCount2++; throw onStartingException; }, null); response.Headers.Clear(); response.Headers["Content-Length"] = new[] { "11" }; var writeException = await Assert.ThrowsAsync <ObjectDisposedException>(async() => await response.Body.WriteAsync(Encoding.ASCII.GetBytes("Hello World"), 0, 11)); Assert.Same(onStartingException, writeException.InnerException); failedWriteCount++; }, testContext)) { using (var connection = new TestConnection()) { await connection.SendEnd( "GET / HTTP/1.1", "", "GET / HTTP/1.1", "Connection: close", "", ""); await connection.Receive( "HTTP/1.1 500 Internal Server Error", ""); await connection.ReceiveStartsWith("Date:"); await connection.Receive( "Content-Length: 0", "Server: Kestrel", "", "HTTP/1.1 500 Internal Server Error", "Connection: close", ""); await connection.ReceiveStartsWith("Date:"); await connection.ReceiveEnd( "Content-Length: 0", "Server: Kestrel", "", ""); Assert.Equal(2, onStartingCallCount1); // The second OnStarting callback should not be called since the first failed. Assert.Equal(0, onStartingCallCount2); Assert.Equal(2, testLogger.ApplicationErrorsLogged); } } }