コード例 #1
0
        public void PostSetsLocationHeader()
        {
            // Arrange
            ProductsController controller = new ProductsController(repository);

            controller.Request = new HttpRequestMessage
            {
                RequestUri = new Uri("http://localhost/api/products")
            };
            controller.Configuration = new HttpConfiguration();
            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional });

            controller.RequestContext.RouteData = new HttpRouteData(
                route: new HttpRoute(),
                values: new HttpRouteValueDictionary { { "controller", "products" } });

            // Act
            Product product = new Product() { Id = 42, Name = "Product1" };
            var response = controller.Post(product);

            // Assert
            Assert.AreEqual("http://localhost/api/products/42", response.Headers.Location.AbsoluteUri);
        }
コード例 #2
0
        public void PostSetsLocationHeader_MockVersion()
        {
            // This version uses a mock UrlHelper.

            // Arrange
            ProductsController controller = new ProductsController(repository);
            controller.Request = new HttpRequestMessage();
            controller.Configuration = new HttpConfiguration();

            string locationUrl = "http://location/";

             //Create the mock and set up the Link method, which is used to create the Location header.
             //The mock version returns a fixed string.
            var mockUrlHelper = new Mock<UrlHelper>();
            mockUrlHelper.Setup(x => x.Link(It.IsAny<string>(), It.IsAny<object>())).Returns(locationUrl);
            controller.Url = mockUrlHelper.Object;

            // Act
            var response = controller.Post(new Product() { Id = 42 });

            var foop = controller.Url.Link("DefaultApi", new { id = 42 });
            // Assert
            Assert.AreEqual(locationUrl, response.Headers.Location.AbsoluteUri);
        }