public void Invoke_SetsResponseBody() { var response = new HttpResponseMessage(HttpStatusCode.OK); var expectedBody = @"{""x"":""y""}"; response.Content = new StringContent(expectedBody, Encoding.UTF8, "application/json"); var handler = new HandlerStub() { Response = response }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateOwinEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var responseStream = new MemoryStream(); environment["owin.ResponseBody"] = responseStream; var adapter = new HttpMessageHandlerAdapter(next: null, messageHandler: handler, bufferPolicySelector: bufferPolicySelector); adapter.Invoke(new OwinContext(environment)).Wait(); responseStream.Seek(0, SeekOrigin.Begin); byte[] bodyBytes = new byte[9]; int charsRead = responseStream.Read(bodyBytes, 0, 9); // Assert that we can read 9 characters and no more characters after that Assert.Equal(9, charsRead); Assert.Equal(-1, responseStream.ReadByte()); Assert.Equal(expectedBody, Encoding.UTF8.GetString(bodyBytes)); }
public void Invoke_RespectsOutputBufferingSetting(bool bufferOutput) { var response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new ObjectContent <string>("blue", new JsonMediaTypeFormatter()); var handler = new HandlerStub() { Response = response }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: bufferOutput); var environment = CreateOwinEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var adapter = new HttpMessageHandlerAdapter(next: null, messageHandler: handler, bufferPolicySelector: bufferPolicySelector); adapter.Invoke(new OwinContext(environment)).Wait(); var responseHeaders = environment["owin.ResponseHeaders"] as IDictionary <string, string[]>; if (bufferOutput) { Assert.True(responseHeaders.ContainsKey("Content-Length")); } else { Assert.False(responseHeaders.ContainsKey("Content-Length")); } }
public void Invoke_DoesNotCallNext_IfMessageHandlerDoesNotAddNoRouteMatchedProperty() { var mockNext = new Mock <OwinMiddleware>(MockBehavior.Strict, null); HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.NotFound); var handler = new HandlerStub() { Response = response, AddNoRouteMatchedKey = false }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateOwinEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var adapter = new HttpMessageHandlerAdapter(next: mockNext.Object, messageHandler: handler, bufferPolicySelector: bufferPolicySelector); Assert.DoesNotThrow( () => adapter.Invoke(new OwinContext(environment)).Wait()); }
public void Invoke_Throws_IfMessageHandlerReturnsNull() { HttpResponseMessage response = null; var handler = new HandlerStub() { Response = response }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateOwinEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var adapter = new HttpMessageHandlerAdapter(next: null, messageHandler: handler, bufferPolicySelector: bufferPolicySelector); Assert.Throws <InvalidOperationException>( () => adapter.Invoke(new OwinContext(environment)).Wait(), "The message handler did not return a response message."); }
public void Invoke_AddsZeroContentLengthHeader_WhenThereIsNoContent() { var response = new HttpResponseMessage(HttpStatusCode.OK); var handler = new HandlerStub() { Response = response }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateOwinEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var adapter = new HttpMessageHandlerAdapter(next: null, messageHandler: handler, bufferPolicySelector: bufferPolicySelector); adapter.Invoke(new OwinContext(environment)).Wait(); var responseHeaders = environment["owin.ResponseHeaders"] as IDictionary <string, string[]>; Assert.Equal("0", responseHeaders["Content-Length"][0]); }
public void Invoke_CallsNext_IfMessageHandlerReturns404WithNoRouteMatched() { var nextMock = new Mock <OwinMiddleware>(null); nextMock.Setup(middleware => middleware.Invoke(It.IsAny <OwinContext>())).Returns(TaskHelpers.Completed()).Verifiable(); HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.NotFound); var handler = new HandlerStub() { Response = response, AddNoRouteMatchedKey = true }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateOwinEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var adapter = new HttpMessageHandlerAdapter(next: nextMock.Object, messageHandler: handler, bufferPolicySelector: bufferPolicySelector); adapter.Invoke(new OwinContext(environment)).Wait(); nextMock.Verify(); }
public void Invoke_SetsResponseStatusCodeAndReasonPhrase() { var expectedReasonPhrase = "OH NO!"; var response = new HttpResponseMessage(HttpStatusCode.ServiceUnavailable) { ReasonPhrase = expectedReasonPhrase }; var handler = new HandlerStub() { Response = response }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateOwinEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var adapter = new HttpMessageHandlerAdapter(next: null, messageHandler: handler, bufferPolicySelector: bufferPolicySelector); adapter.Invoke(new OwinContext(environment)).Wait(); Assert.Equal(503, environment["owin.ResponseStatusCode"]); Assert.Equal(expectedReasonPhrase, environment["owin.ResponseReasonPhrase"]); }
public void Invoke_AddsTransferEncodingChunkedHeaderIfThereIsNoContentLength() { var response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new ObjectContent <string>("blue", new JsonMediaTypeFormatter()); var handler = new HandlerStub() { Response = response }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateOwinEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var adapter = new HttpMessageHandlerAdapter(next: null, messageHandler: handler, bufferPolicySelector: bufferPolicySelector); adapter.Invoke(new OwinContext(environment)).Wait(); var responseHeaders = environment["owin.ResponseHeaders"] as IDictionary <string, string[]>; Assert.Equal("chunked", responseHeaders["Transfer-Encoding"][0]); Assert.False(responseHeaders.ContainsKey("Content-Length")); }
public void Invoke_CallsNext_IfMessageHandlerReturns404WithNoRouteMatched() { bool nextCalled = false; var next = new Func <IDictionary <string, object>, Task>(env => { nextCalled = true; return(TaskHelpers.Completed()); }); HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.NotFound); var handler = new HandlerStub() { Response = response, AddNoRouteMatchedKey = true }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var adapter = new HttpMessageHandlerAdapter(next, handler, bufferPolicySelector); adapter.Invoke(environment).Wait(); Assert.True(nextCalled); }
public void Invoke_AddsTransferEncodingChunkedHeaderOverContentLength() { var response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes("Hello world"))); response.Headers.TransferEncodingChunked = true; var handler = new HandlerStub() { Response = response }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateOwinEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var adapter = new HttpMessageHandlerAdapter(next: null, messageHandler: handler, bufferPolicySelector: bufferPolicySelector); adapter.Invoke(new OwinContext(environment)).Wait(); var responseHeaders = environment["owin.ResponseHeaders"] as IDictionary <string, string[]>; Assert.Equal("chunked", responseHeaders["Transfer-Encoding"][0]); Assert.False(responseHeaders.ContainsKey("Content-Length")); }
public void Invoke_SetsResponseHeaders() { var response = new HttpResponseMessage(HttpStatusCode.OK); response.Headers.Location = new Uri("http://www.location.com/"); response.Content = new StringContent(@"{""x"":""y""}", Encoding.UTF8, "application/json"); var handler = new HandlerStub() { Response = response }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateOwinEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var adapter = new HttpMessageHandlerAdapter(next: null, messageHandler: handler, bufferPolicySelector: bufferPolicySelector); adapter.Invoke(new OwinContext(environment)).Wait(); var responseHeaders = environment["owin.ResponseHeaders"] as IDictionary <string, string[]>; Assert.Equal(3, responseHeaders.Count); Assert.Equal("http://www.location.com/", Assert.Single(responseHeaders["Location"])); Assert.Equal("9", Assert.Single(responseHeaders["Content-Length"])); Assert.Equal("application/json; charset=utf-8", Assert.Single(responseHeaders["Content-Type"])); }
public void Invoke_AddsZeroContentLengthHeader_WhenThereIsNoContent() { var response = new HttpResponseMessage(HttpStatusCode.OK); var handler = new HandlerStub() { Response = response }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); adapter.Invoke(environment).Wait(); var responseHeaders = environment["owin.ResponseHeaders"] as IDictionary<string, string[]>; Assert.Equal("0", responseHeaders["Content-Length"][0]); }
public void Invoke_RespectsOutputBufferingSetting(bool bufferOutput) { var response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new ObjectContent<string>("blue", new JsonMediaTypeFormatter()); var handler = new HandlerStub() { Response = response }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: bufferOutput); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); adapter.Invoke(environment).Wait(); var responseHeaders = environment["owin.ResponseHeaders"] as IDictionary<string, string[]>; if (bufferOutput) { Assert.True(responseHeaders.ContainsKey("Content-Length")); } else { Assert.False(responseHeaders.ContainsKey("Content-Length")); } }
public void Invoke_SetsResponseBody() { var response = new HttpResponseMessage(HttpStatusCode.OK); var expectedBody = @"{""x"":""y""}"; response.Content = new StringContent(expectedBody, Encoding.UTF8, "application/json"); var handler = new HandlerStub() { Response = response }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var responseStream = new MemoryStream(); environment["owin.ResponseBody"] = responseStream; var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); adapter.Invoke(environment).Wait(); responseStream.Seek(0, SeekOrigin.Begin); byte[] bodyBytes = new byte[9]; int charsRead = responseStream.Read(bodyBytes, 0, 9); // Assert that we can read 9 characters and no more characters after that Assert.Equal(9, charsRead); Assert.Equal(-1, responseStream.ReadByte()); Assert.Equal(expectedBody, Encoding.UTF8.GetString(bodyBytes)); }
public void Invoke_SetsResponseHeaders() { var response = new HttpResponseMessage(HttpStatusCode.OK); response.Headers.Location = new Uri("http://www.location.com/"); response.Content = new StringContent(@"{""x"":""y""}", Encoding.UTF8, "application/json"); var handler = new HandlerStub() { Response = response }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); adapter.Invoke(environment).Wait(); var responseHeaders = environment["owin.ResponseHeaders"] as IDictionary<string, string[]>; Assert.Equal(3, responseHeaders.Count); Assert.Equal("http://www.location.com/", Assert.Single(responseHeaders["Location"])); Assert.Equal("9", Assert.Single(responseHeaders["Content-Length"])); Assert.Equal("application/json; charset=utf-8", Assert.Single(responseHeaders["Content-Type"])); }
public void Invoke_SetsResponseStatusCodeAndReasonPhrase() { var expectedReasonPhrase = "OH NO!"; var response = new HttpResponseMessage(HttpStatusCode.ServiceUnavailable) { ReasonPhrase = expectedReasonPhrase }; var handler = new HandlerStub() { Response = response }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); adapter.Invoke(environment).Wait(); Assert.Equal(HttpStatusCode.ServiceUnavailable, environment["owin.ResponseStatusCode"]); Assert.Equal(expectedReasonPhrase, environment["owin.ResponseReasonPhrase"]); }
public void Invoke_CallsNext_IfMessageHandlerReturns404WithNoRouteMatched() { bool nextCalled = false; var next = new Func<IDictionary<string, object>, Task>(env => { nextCalled = true; return TaskHelpers.Completed(); }); HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.NotFound); var handler = new HandlerStub() { Response = response, AddNoRouteMatchedKey = true }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var adapter = new HttpMessageHandlerAdapter(next, handler, bufferPolicySelector); adapter.Invoke(environment).Wait(); Assert.True(nextCalled); }
public void Invoke_Throws_IfMessageHandlerReturnsNull() { HttpResponseMessage response = null; var handler = new HandlerStub() { Response = response }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); Assert.Throws<InvalidOperationException>( () => adapter.Invoke(environment).Wait(), "The message handler did not return a response message."); }
public void Invoke_CallsNext_IfMessageHandlerReturns404WithNoRouteMatched() { var nextMock = new Mock<OwinMiddleware>(null); nextMock.Setup(middleware => middleware.Invoke(It.IsAny<OwinContext>())).Returns(TaskHelpers.Completed()).Verifiable(); HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.NotFound); var handler = new HandlerStub() { Response = response, AddNoRouteMatchedKey = true }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateOwinEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var adapter = new HttpMessageHandlerAdapter(next: nextMock.Object, messageHandler: handler, bufferPolicySelector: bufferPolicySelector); adapter.Invoke(new OwinContext(environment)).Wait(); nextMock.Verify(); }
public void Invoke_DoesNotCallNext_IfMessageHandlerDoesNotAddNoRouteMatchedProperty() { var mockNext = new Mock<OwinMiddleware>(MockBehavior.Strict, null); HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.NotFound); var handler = new HandlerStub() { Response = response, AddNoRouteMatchedKey = false }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateOwinEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var adapter = new HttpMessageHandlerAdapter(next: mockNext.Object, messageHandler: handler, bufferPolicySelector: bufferPolicySelector); Assert.DoesNotThrow( () => adapter.Invoke(new OwinContext(environment)).Wait()); }
public void Invoke_IfTransferEncodingChunkedAndContentLengthAreBothSet_IgnoresContentLength() { var response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes("Hello world"))); response.Headers.TransferEncodingChunked = true; var handler = new HandlerStub() { Response = response }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateOwinEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var options = CreateValidOptions(handler, bufferPolicySelector); var adapter = CreateProductUnderTest(options); adapter.Invoke(new OwinContext(environment)).Wait(); var responseHeaders = environment["owin.ResponseHeaders"] as IDictionary<string, string[]>; Assert.False(responseHeaders.ContainsKey("Content-Length")); }
public void Invoke_DoesNotCallNext_IfMessageHandlerDoesNotReturn404() { var mockNext = new Mock<OwinMiddleware>(MockBehavior.Strict, null); HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); var handler = new HandlerStub() { Response = response, AddNoRouteMatchedKey = true }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateOwinEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var options = CreateValidOptions(handler, bufferPolicySelector); var adapter = CreateProductUnderTest(mockNext.Object, options); Assert.DoesNotThrow( () => adapter.Invoke(new OwinContext(environment)).Wait()); }
public void Invoke_AddsTransferEncodingChunkedHeaderOverContentLength() { var response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes("Hello world"))); response.Headers.TransferEncodingChunked = true; var handler = new HandlerStub() { Response = response }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); adapter.Invoke(environment).Wait(); var responseHeaders = environment["owin.ResponseHeaders"] as IDictionary<string, string[]>; Assert.Equal("chunked", responseHeaders["Transfer-Encoding"][0]); Assert.False(responseHeaders.ContainsKey("Content-Length")); }
public void Invoke_SetsResponseStatusCodeAndReasonPhrase() { var expectedReasonPhrase = "OH NO!"; var response = new HttpResponseMessage(HttpStatusCode.ServiceUnavailable) { ReasonPhrase = expectedReasonPhrase }; var handler = new HandlerStub() { Response = response }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateOwinEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var options = CreateValidOptions(handler, bufferPolicySelector); var adapter = CreateProductUnderTest(options); adapter.Invoke(new OwinContext(environment)).Wait(); Assert.Equal(503, environment["owin.ResponseStatusCode"]); Assert.Equal(expectedReasonPhrase, environment["owin.ResponseReasonPhrase"]); }
public void Invoke_AddsZeroContentLengthHeader_WhenThereIsNoContent() { var response = new HttpResponseMessage(HttpStatusCode.OK); var handler = new HandlerStub() { Response = response }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateOwinEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var options = CreateValidOptions(handler, bufferPolicySelector); var adapter = CreateProductUnderTest(options); adapter.Invoke(new OwinContext(environment)).Wait(); var responseHeaders = environment["owin.ResponseHeaders"] as IDictionary<string, string[]>; Assert.Equal("0", responseHeaders["Content-Length"][0]); }
public void Invoke_AddsTransferEncodingChunkedHeaderIfThereIsNoContentLength() { var response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new ObjectContent<string>("blue", new JsonMediaTypeFormatter()); var handler = new HandlerStub() { Response = response }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); adapter.Invoke(environment).Wait(); var responseHeaders = environment["owin.ResponseHeaders"] as IDictionary<string, string[]>; Assert.Equal("chunked", responseHeaders["Transfer-Encoding"][0]); Assert.False(responseHeaders.ContainsKey("Content-Length")); }
public void Invoke_Throws_IfMessageHandlerReturnsNull() { HttpResponseMessage response = null; var handler = new HandlerStub() { Response = response }; var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateOwinEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var options = CreateValidOptions(handler, bufferPolicySelector); var adapter = CreateProductUnderTest(options); Assert.Throws<InvalidOperationException>( () => adapter.Invoke(new OwinContext(environment)).Wait(), "The message handler did not return a response message."); }