public async Task CreatedAtRouteNegotiatedContentResult_Fails() { // Arrange var urlHelper = new Mock <IUrlHelper>(MockBehavior.Strict); urlHelper .Setup(u => u.RouteUrl(It.IsAny <string>(), It.IsAny <object>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .Returns((string)null); var httpContext = new DefaultHttpContext(); httpContext.RequestServices = CreateServices(urlHelper.Object); var stream = new MemoryStream(); httpContext.Response.Body = stream; var context = new ActionContext(new RouteContext(httpContext), new ActionDescriptor()); var result = new CreatedAtRouteNegotiatedContentResult <Product>( "api_route", new RouteValueDictionary(new { controller = "Products", id = 5 }), new Product()); // Act var ex = await Assert.ThrowsAsync <InvalidOperationException>(async() => await result.ExecuteResultAsync(context)); // Assert Assert.Equal("Failed to generate a URL using route 'api_route'.", ex.Message); }
public async Task CreatedAtRouteNegotiatedContentResult_SetsLocation() { // Arrange var urlHelper = new Mock <IUrlHelper>(MockBehavior.Strict); urlHelper .Setup(u => u.RouteUrl(It.IsAny <string>(), It.IsAny <object>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .Returns("http://contoso.com/api/Products/5"); var httpContext = new DefaultHttpContext(); httpContext.RequestServices = CreateServices(urlHelper.Object); var stream = new MemoryStream(); httpContext.Response.Body = stream; var context = new ActionContext(new RouteContext(httpContext), new ActionDescriptor()); var result = new CreatedAtRouteNegotiatedContentResult <Product>( "api_route", new RouteValueDictionary(new { controller = "Products", id = 5 }), new Product()); // Act await result.ExecuteResultAsync(context); // Assert Assert.Equal("http://contoso.com/api/Products/5", httpContext.Response.Headers["Location"]); }