예제 #1
0
 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 });
 }
예제 #2
0
        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)));
        }
예제 #3
0
        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);
        }
예제 #4
0
        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);
        }