public void SendAsync_IgnoreRoute_UsesRouteDataWithStopRoutingHandlerFromRequestContext() { // Arrange Mock <HttpMessageHandler> doNotUseDefaultHandlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict); doNotUseDefaultHandlerMock.Protected().Setup("Dispose", true); using (HttpConfiguration configuration = new HttpConfiguration()) using (HttpMessageHandler doNoUseDefaultHandler = doNotUseDefaultHandlerMock.Object) using (HttpRoutingDispatcher dispatcher = new HttpRoutingDispatcher(configuration, doNoUseDefaultHandler)) using (HttpMessageInvoker invoker = new HttpMessageInvoker(dispatcher)) using (HttpRequestMessage expectedRequest = new HttpRequestMessage()) using (HttpResponseMessage expectedResponse = new HttpResponseMessage()) { HttpMessageHandler routeHandler = new StopRoutingHandler(); Mock <IHttpRoute> routeMock = new Mock <IHttpRoute>(MockBehavior.Strict); routeMock.Setup(r => r.Handler).Returns(routeHandler); Mock <IHttpRouteData> routeDataMock = new Mock <IHttpRouteData>(MockBehavior.Strict); routeDataMock.Setup(d => d.Route).Returns(routeMock.Object); routeDataMock.Setup(d => d.Values).Returns(new Dictionary <string, object>()); HttpRequestContext context = new HttpRequestContext(); context.RouteData = routeDataMock.Object; expectedRequest.SetRequestContext(context); // Act HttpResponseMessage response = invoker.SendAsync(expectedRequest, CancellationToken.None).Result; // Assert Assert.Equal(response.StatusCode, HttpStatusCode.NotFound); Assert.True(response.RequestMessage.Properties.ContainsKey(HttpPropertyKeys.NoRouteMatched)); } }
public void SendAsync_UsesRouteDataFromRequestContext() { // Arrange Mock <HttpMessageHandler> doNotUseDefaultHandlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict); doNotUseDefaultHandlerMock.Protected().Setup("Dispose", true); using (HttpConfiguration configuration = new HttpConfiguration()) using (HttpMessageHandler doNoUseDefaultHandler = doNotUseDefaultHandlerMock.Object) using (HttpRoutingDispatcher dispatcher = new HttpRoutingDispatcher(configuration, doNoUseDefaultHandler)) using (HttpMessageInvoker invoker = new HttpMessageInvoker(dispatcher)) using (HttpRequestMessage expectedRequest = new HttpRequestMessage()) using (HttpResponseMessage expectedResponse = new HttpResponseMessage()) { SpyHttpMessageHandler routeHandler = new SpyHttpMessageHandler(expectedResponse); Mock <IHttpRoute> routeMock = new Mock <IHttpRoute>(MockBehavior.Strict); routeMock.Setup(r => r.Handler).Returns(routeHandler); Mock <IHttpRouteData> routeDataMock = new Mock <IHttpRouteData>(MockBehavior.Strict); routeDataMock.Setup(d => d.Route).Returns(routeMock.Object); routeDataMock.Setup(d => d.Values).Returns(new Dictionary <string, object>()); HttpRequestContext context = new HttpRequestContext(); context.RouteData = routeDataMock.Object; expectedRequest.SetRequestContext(context); // Act HttpResponseMessage response = invoker.SendAsync(expectedRequest, CancellationToken.None).Result; // Assert Assert.Same(expectedResponse, response); Assert.Same(expectedRequest, routeHandler.Request); } }
public void SendAsync_RemovesRouteParameterOptionalValuesThatAreNotPresent() { var config = new HttpConfiguration(); var request = CreateRequest(config, "http://localhost/api/controllerName"); var dispatcher = new HttpRoutingDispatcher(config); var invoker = new HttpMessageInvoker(dispatcher); invoker.SendAsync(request, CancellationToken.None).WaitUntilCompleted(); Assert.False(request.GetRouteData().Values.ContainsKey("id")); }
public async Task SendAsync_Returns404WhenNoMatchingRoute() { var config = new HttpConfiguration(); var request = CreateRequest(config, "http://localhost/noMatch"); var dispatcher = new HttpRoutingDispatcher(config); var invoker = new HttpMessageInvoker(dispatcher); var response = await invoker.SendAsync(request, CancellationToken.None); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); Assert.True((bool)request.Properties[HttpPropertyKeys.NoRouteMatched]); }
public void SendAsync_CallsDefaultHandlerWhenRouteHandlerIsNull() { var mockHandler = new Mock<HttpMessageHandler>(); var config = new HttpConfiguration(); var request = CreateRequest(config, "http://localhost/api/controllerName", routeHandler: null); var dispatcher = new HttpRoutingDispatcher(config, defaultHandler: mockHandler.Object); var invoker = new HttpMessageInvoker(dispatcher); invoker.SendAsync(request, CancellationToken.None); mockHandler.Protected().Verify("SendAsync", Times.Once(), request, CancellationToken.None); }
public void SendAsync_Returns404WhenNoMatchingRoute() { var config = new HttpConfiguration(); var request = CreateRequest(config, "http://localhost/noMatch"); var dispatcher = new HttpRoutingDispatcher(config); var invoker = new HttpMessageInvoker(dispatcher); var responseTask = invoker.SendAsync(request, CancellationToken.None); responseTask.WaitUntilCompleted(); Assert.Equal(HttpStatusCode.NotFound, responseTask.Result.StatusCode); }
public void SendAsync_CallsRouterHandlerWhenRouteHandlerIsNotNull() { var mockHandler = new Mock <HttpMessageHandler>(); var config = new HttpConfiguration(); var request = CreateRequest(config, "http://localhost/api/controllerName", routeHandler: mockHandler.Object); var dispatcher = new HttpRoutingDispatcher(config); var invoker = new HttpMessageInvoker(dispatcher); invoker.SendAsync(request, CancellationToken.None); mockHandler.Protected().Verify("SendAsync", Times.Once(), request, CancellationToken.None); }
public void SendAsync_PopulatesRouteDataWhenNotPresentInRequest() { var config = new HttpConfiguration(); var request = CreateRequest(config, "http://localhost/api/controllerName/42"); var dispatcher = new HttpRoutingDispatcher(config); var invoker = new HttpMessageInvoker(dispatcher); invoker.SendAsync(request, CancellationToken.None).WaitUntilCompleted(); var routeData = request.GetRouteData(); Assert.NotNull(routeData); Assert.Same(config.Routes.Single(), routeData.Route); Assert.Equal("controllerName", routeData.Values["controller"]); Assert.Equal("42", routeData.Values["id"]); }
public void SendAsync_Returns_RoutingErrors_IfPresentOnTheRequest() { // Arrange HttpRoutingDispatcher dispatcher = new HttpRoutingDispatcher(new HttpConfiguration()); HttpMessageInvoker invoker = new HttpMessageInvoker(dispatcher); HttpRequestMessage request = new HttpRequestMessage(); HttpResponseMessage routingErrorResponse = new HttpResponseMessage(); request.SetRoutingErrorResponse(routingErrorResponse); // Act var result = invoker.SendAsync(request, CancellationToken.None).Result; // Assert Assert.Same(routingErrorResponse, result); }
public void SendAsync_IgnoreRoute_UsesRouteDataWithStopRoutingHandlerFromRequestContext() { // Arrange Mock<HttpMessageHandler> doNotUseDefaultHandlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict); doNotUseDefaultHandlerMock.Protected().Setup("Dispose", true); using (HttpConfiguration configuration = new HttpConfiguration()) using (HttpMessageHandler doNoUseDefaultHandler = doNotUseDefaultHandlerMock.Object) using (HttpRoutingDispatcher dispatcher = new HttpRoutingDispatcher(configuration, doNoUseDefaultHandler)) using (HttpMessageInvoker invoker = new HttpMessageInvoker(dispatcher)) using (HttpRequestMessage expectedRequest = new HttpRequestMessage()) using (HttpResponseMessage expectedResponse = new HttpResponseMessage()) { HttpMessageHandler routeHandler = new StopRoutingHandler(); Mock<IHttpRoute> routeMock = new Mock<IHttpRoute>(MockBehavior.Strict); routeMock.Setup(r => r.Handler).Returns(routeHandler); Mock<IHttpRouteData> routeDataMock = new Mock<IHttpRouteData>(MockBehavior.Strict); routeDataMock.Setup(d => d.Route).Returns(routeMock.Object); routeDataMock.Setup(d => d.Values).Returns(new Dictionary<string, object>()); HttpRequestContext context = new HttpRequestContext(); context.RouteData = routeDataMock.Object; expectedRequest.SetRequestContext(context); // Act HttpResponseMessage response = invoker.SendAsync(expectedRequest, CancellationToken.None).Result; // Assert Assert.Equal(response.StatusCode, HttpStatusCode.NotFound); Assert.True(response.RequestMessage.Properties.ContainsKey(HttpPropertyKeys.NoRouteMatched)); } }
public void SendAsync_UsesRouteDataFromRequestContext() { // Arrange Mock<HttpMessageHandler> doNotUseDefaultHandlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict); doNotUseDefaultHandlerMock.Protected().Setup("Dispose", true); using (HttpConfiguration configuration = new HttpConfiguration()) using (HttpMessageHandler doNoUseDefaultHandler = doNotUseDefaultHandlerMock.Object) using (HttpRoutingDispatcher dispatcher = new HttpRoutingDispatcher(configuration, doNoUseDefaultHandler)) using (HttpMessageInvoker invoker = new HttpMessageInvoker(dispatcher)) using (HttpRequestMessage expectedRequest = new HttpRequestMessage()) using (HttpResponseMessage expectedResponse = new HttpResponseMessage()) { SpyHttpMessageHandler routeHandler = new SpyHttpMessageHandler(expectedResponse); Mock<IHttpRoute> routeMock = new Mock<IHttpRoute>(MockBehavior.Strict); routeMock.Setup(r => r.Handler).Returns(routeHandler); Mock<IHttpRouteData> routeDataMock = new Mock<IHttpRouteData>(MockBehavior.Strict); routeDataMock.Setup(d => d.Route).Returns(routeMock.Object); routeDataMock.Setup(d => d.Values).Returns(new Dictionary<string, object>()); HttpRequestContext context = new HttpRequestContext(); context.RouteData = routeDataMock.Object; expectedRequest.SetRequestContext(context); // Act HttpResponseMessage response = invoker.SendAsync(expectedRequest, CancellationToken.None).Result; // Assert Assert.Same(expectedResponse, response); Assert.Same(expectedRequest, routeHandler.Request); } }
public void SendAsync_CallsDefaultHandlerWhenCustomASPNETRoute() { // Arrange var mockHandler = new Mock<HttpMessageHandler>(); var config = new HttpConfiguration(); var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/api/controllerName"); request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; HttpDomainRoute domainRoute = new HttpDomainRoute("test", new { controller = "Values", action = "GetTenant" }); request.Properties[HttpPropertyKeys.HttpRouteDataKey] = new HostedHttpRouteData(domainRoute.GetRouteData(null)); var dispatcher = new HttpRoutingDispatcher(config, defaultHandler: mockHandler.Object); var invoker = new HttpMessageInvoker(dispatcher); // Act invoker.SendAsync(request, CancellationToken.None); // Assert mockHandler.Protected().Verify("SendAsync", Times.Once(), request, CancellationToken.None); }
public void SendAsync_HandlesHttpResponseExceptions_FromCustomRoutes() { // Arrange HttpResponseException routingError = new HttpResponseException(new HttpResponseMessage()); HttpRequestMessage request = new HttpRequestMessage(); Mock<IHttpRoute> customRoute = new Mock<IHttpRoute>(); customRoute.Setup(r => r.GetRouteData("/", request)).Throws(routingError); HttpConfiguration config = new HttpConfiguration(); config.Routes.Add("default", customRoute.Object); HttpRoutingDispatcher dispatcher = new HttpRoutingDispatcher(config); HttpMessageInvoker invoker = new HttpMessageInvoker(dispatcher); // Act var result = invoker.SendAsync(request, CancellationToken.None).Result; // Assert Assert.Same(routingError.Response, result); }