public void CreateResponse_DoingConneg_OnlyContent_RetrievesContentNegotiatorFromServices() { // Arrange var context = new DefaultHttpContext(); var services = new Mock<IServiceProvider>(); services .Setup(s => s.GetService(typeof(IContentNegotiator))) .Returns(Mock.Of<IContentNegotiator>()) .Verifiable(); var options = new WebApiCompatShimOptions(); options.Formatters.AddRange(new MediaTypeFormatterCollection()); var optionsAccessor = new Mock<IOptions<WebApiCompatShimOptions>>(); optionsAccessor.SetupGet(o => o.Options).Returns(options); services .Setup(s => s.GetService(typeof(IOptions<WebApiCompatShimOptions>))) .Returns(optionsAccessor.Object); context.RequestServices = services.Object; var request = CreateRequest(context); // Act request.CreateResponse(CreateValue()); // Assert services.Verify(); }
private static IServiceProvider CreateServices( IContentNegotiator contentNegotiator = null, MediaTypeFormatter formatter = null) { var options = new WebApiCompatShimOptions(); if (formatter == null) { options.Formatters.AddRange(new MediaTypeFormatterCollection()); } else { options.Formatters.Add(formatter); } var optionsAccessor = new Mock<IOptions<WebApiCompatShimOptions>>(); optionsAccessor.SetupGet(o => o.Options).Returns(options); var services = new Mock<IServiceProvider>(MockBehavior.Strict); services .Setup(s => s.GetService(typeof(IOptions<WebApiCompatShimOptions>))) .Returns(optionsAccessor.Object); if (contentNegotiator != null) { services .Setup(s => s.GetService(typeof(IContentNegotiator))) .Returns(contentNegotiator); } return services.Object; }
public void Configure(WebApiCompatShimOptions options, string name = "") { // Add the default formatters options.Formatters.AddRange(new MediaTypeFormatterCollection()); }