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_RespectsInputBufferingSetting(bool bufferInput) { var handler = CreateOKHandlerStub(); var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: bufferInput, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var expectedBody = "This is the request body."; var requestBodyMock = new Mock <MemoryStream>(Encoding.UTF8.GetBytes(expectedBody)); requestBodyMock.CallBase = true; requestBodyMock.Setup(s => s.CanSeek).Returns(false); MemoryStream requestBody = requestBodyMock.Object; environment["owin.RequestBody"] = requestBody; var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); adapter.Invoke(environment).Wait(); if (bufferInput) { Assert.False(requestBody.CanRead); // Assert that the OWIN environment still has a request body that can be read var owinRequestBody = environment["owin.RequestBody"] as Stream; byte[] bodyBytes = new byte[25]; int charsRead = owinRequestBody.Read(bodyBytes, 0, 25); Assert.Equal(expectedBody, Encoding.UTF8.GetString(bodyBytes)); } else { Assert.True(requestBody.CanRead); } // Assert that Web API gets the right body var request = handler.Request; Assert.Equal(expectedBody, request.Content.ReadAsStringAsync().Result); }
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_Throws_IfHostHeaderMissing() { var handler = CreateOKHandlerStub(); var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var requestHeaders = environment["owin.RequestHeaders"] as IDictionary<string, string[]>; requestHeaders.Remove("Host"); var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); Assert.Throws<InvalidOperationException>( () => adapter.Invoke(environment).Wait(), "The OWIN environment does not contain a value for the required 'Host' header."); }
public void Invoke_BuildsUriWithHostAndPort() { var handler = CreateOKHandlerStub(); var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost:12345", "/vroot", "/api/customers"); var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); adapter.Invoke(environment).Wait(); var request = handler.Request; Assert.Equal(HttpMethod.Get, request.Method); Assert.Equal("http://localhost:12345/vroot/api/customers", request.RequestUri.AbsoluteUri); }
public void Invoke_SetsOwinEnvironment() { var handler = CreateOKHandlerStub(); 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 request = handler.Request; Assert.Same(environment, request.GetOwinEnvironment()); }
public void Invoke_CallsMessageHandler_WithEnvironmentCancellationToken() { var handler = CreateOKHandlerStub(); var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var cancellationToken = new CancellationToken(); environment["owin.CallCancelled"] = cancellationToken; var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); adapter.Invoke(environment).Wait(); Assert.Equal(cancellationToken, handler.CancellationToken); }
public void Invoke_CallsMessageHandler_WithEnvironmentUser() { var handler = CreateOKHandlerStub(); var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var user = new Mock <IPrincipal>().Object; environment["server.User"] = user; var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); adapter.Invoke(environment).Wait(); Assert.Equal(user, handler.User); }
public void Invoke_Throws_IfHostHeaderHasNoValues() { var handler = CreateOKHandlerStub(); var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var requestHeaders = environment["owin.RequestHeaders"] as IDictionary <string, string[]>; requestHeaders["Host"] = new string[0]; var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); Assert.Throws <InvalidOperationException>( () => adapter.Invoke(environment).Wait(), "The OWIN environment does not contain a value for the required 'Host' header."); }
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_SetsClientCertificate() { var handler = CreateOKHandlerStub(); var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var clientCert = new Mock <X509Certificate2>().Object; environment["ssl.ClientCertificate"] = clientCert; var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); adapter.Invoke(environment).Wait(); var request = handler.Request; Assert.Equal(clientCert, request.GetClientCertificate()); }
public void Invoke_SetsRequestBodyOnRequestMessage() { var handler = CreateOKHandlerStub(); var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var expectedBody = "This is the request body."; environment["owin.RequestBody"] = new MemoryStream(Encoding.UTF8.GetBytes(expectedBody)); var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); adapter.Invoke(environment).Wait(); var request = handler.Request; Assert.Equal(expectedBody, request.Content.ReadAsStringAsync().Result); }
public void Invoke_SetsRequestIsLocalProperty(bool?isLocal, bool expectedRequestLocal) { var handler = CreateOKHandlerStub(); var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); if (isLocal.HasValue) { environment["server.IsLocal"] = isLocal.Value; } var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); adapter.Invoke(environment).Wait(); var request = handler.Request; Assert.Equal(expectedRequestLocal, request.IsLocal()); }
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_AddsRequestHeadersToRequestMessage() { var handler = CreateOKHandlerStub(); var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var requestHeaders = environment["owin.RequestHeaders"] as IDictionary <string, string[]>; requestHeaders["Accept"] = new string[] { "application/json", "application/xml" }; requestHeaders["Content-Length"] = new string[] { "45" }; var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); adapter.Invoke(environment).Wait(); var request = handler.Request; Assert.Equal(2, request.Headers.Count()); Assert.Equal(new string[] { "application/json", "application/xml" }, request.Headers.Accept.Select(mediaType => mediaType.ToString()).ToArray()); Assert.Equal("localhost", request.Headers.Host); Assert.Single(request.Content.Headers); Assert.Equal(45, request.Content.Headers.ContentLength); }
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_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_SetsRequestIsLocalProperty(bool? isLocal, bool expectedRequestLocal) { var handler = CreateOKHandlerStub(); var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); if (isLocal.HasValue) { environment["server.IsLocal"] = isLocal.Value; } var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); adapter.Invoke(environment).Wait(); var request = handler.Request; Assert.Equal(expectedRequestLocal, request.IsLocal()); }
public void Invoke_SetsClientCertificate() { var handler = CreateOKHandlerStub(); var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var clientCert = new Mock<X509Certificate2>().Object; environment["ssl.ClientCertificate"] = clientCert; var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); adapter.Invoke(environment).Wait(); var request = handler.Request; Assert.Equal(clientCert, request.GetClientCertificate()); }
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_CallsMessageHandler_WithEnvironmentUser() { var handler = CreateOKHandlerStub(); var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var user = new Mock<IPrincipal>().Object; environment["server.User"] = user; var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); adapter.Invoke(environment).Wait(); Assert.Equal(user, handler.User); }
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_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_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_AddsRequestHeadersToRequestMessage() { var handler = CreateOKHandlerStub(); var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var requestHeaders = environment["owin.RequestHeaders"] as IDictionary<string, string[]>; requestHeaders["Accept"] = new string[] { "application/json", "application/xml" }; requestHeaders["Content-Length"] = new string[] { "45" }; var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); adapter.Invoke(environment).Wait(); var request = handler.Request; Assert.Equal(2, request.Headers.Count()); Assert.Equal(new string[] { "application/json", "application/xml" }, request.Headers.Accept.Select(mediaType => mediaType.ToString()).ToArray()); Assert.Equal("localhost", request.Headers.Host); Assert.Single(request.Content.Headers); Assert.Equal(45, request.Content.Headers.ContentLength); }
public void Invoke_RespectsInputBufferingSetting(bool bufferInput) { var handler = CreateOKHandlerStub(); var bufferPolicySelector = CreateBufferPolicySelector(bufferInput: bufferInput, bufferOutput: false); var environment = CreateEnvironment("GET", "http", "localhost", "/vroot", "/api/customers"); var expectedBody = "This is the request body."; var requestBodyMock = new Mock<MemoryStream>(Encoding.UTF8.GetBytes(expectedBody)); requestBodyMock.CallBase = true; requestBodyMock.Setup(s => s.CanSeek).Returns(false); MemoryStream requestBody = requestBodyMock.Object; environment["owin.RequestBody"] = requestBody; var adapter = new HttpMessageHandlerAdapter(env => TaskHelpers.Completed(), handler, bufferPolicySelector); adapter.Invoke(environment).Wait(); if (bufferInput) { Assert.False(requestBody.CanRead); // Assert that the OWIN environment still has a request body that can be read var owinRequestBody = environment["owin.RequestBody"] as Stream; byte[] bodyBytes = new byte[25]; int charsRead = owinRequestBody.Read(bodyBytes, 0, 25); Assert.Equal(expectedBody, Encoding.UTF8.GetString(bodyBytes)); } else { Assert.True(requestBody.CanRead); } // Assert that Web API gets the right body var request = handler.Request; Assert.Equal(expectedBody, request.Content.ReadAsStringAsync().Result); }