public static void AddDefaultRoute(this HttpRequestMessage request) { request.RequestUri = new Uri(request.RequestUri, "api/qux"); request.AddRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional }); }
public void GetUriForNonMatchingRouteThrows( [Frozen] HttpRequestMessage request, RouteLinker sut, int id) { request.RequestUri = new Uri(request.RequestUri, "api/foo/" + id); request.AddRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{bar}", defaults: new { controller = "Home", id = RouteParameter.Optional }); Assert.Throws <InvalidOperationException>(() => sut.GetUri <FooController>(c => c.GetById(id))); }
public void GetUriWithCustomRouteAndDispatcherReturnsCorrectResult( [Frozen] HttpRequestMessage request, [Frozen(As = typeof(IRouteValuesQuery))] ScalarRouteValuesQuery dummyQuery, [Frozen] Mock <IRouteDispatcher> dispatcherStub, string routeName, [Greedy] RouteLinker sut, int ploeh, string fnaah) { // Arrange request.AddDefaultRoute(); request.AddRoute( name: routeName, routeTemplate: "foo/{ploeh}/{fnaah}", defaults: new { }); var method = Reflect <FooController> .GetMethod(c => c.GetWithPloehAndFnaah(ploeh, fnaah)); dispatcherStub .Setup(d => d.Dispatch( It.Is <MethodCallExpression>(exp => method.Equals(exp.Method)), It.IsAny <IDictionary <string, object> >())) .Returns((MethodCallExpression _, IDictionary <string, object> routeValues) => new Rouple(routeName, routeValues)); // Act var actual = sut.GetUri <FooController>(r => r.GetWithPloehAndFnaah(ploeh, fnaah)); // Assert var baseUri = request.RequestUri.GetLeftPart(UriPartial.Authority); var expected = new Uri( new Uri(baseUri), "foo/" + ploeh + "/" + fnaah); Assert.Equal(expected, actual); }
public void GetFooRouteForDefaultGetMethodFromIndexedUriReturnsCorrectResult( [Frozen] HttpRequestMessage request, RouteLinker sut, string currentId) { request.RequestUri = new Uri(request.RequestUri, "api/foo/" + currentId); request.AddRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { controller = "Home", id = RouteParameter.Optional }); Uri actual = sut.GetUri <FooController>(r => r.GetDefault()); var baseUri = request.RequestUri.GetLeftPart(UriPartial.Authority); var expected = new Uri(new Uri(baseUri), "api/foo"); Assert.Equal(expected, actual); }