public void SelectFormatter_WithNoMatchingAcceptHeadersAndRequestContentType_PicksFormatterBasedOnObjectType (string acceptHeader) { // For no accept headers, // can write is called twice once for the request Content-Type and once for the type match pass. // For each additional accept header, it is called once. // Arrange var acceptHeaderCollection = string.IsNullOrEmpty(acceptHeader) ? null : MediaTypeHeaderValue.ParseList(new[] { acceptHeader }).ToArray(); var stream = new MemoryStream(); var httpResponse = new Mock <HttpResponse>(); httpResponse.SetupProperty <string>(o => o.ContentType); httpResponse.SetupGet(r => r.Body).Returns(stream); var actionContext = CreateMockActionContext(httpResponse.Object, requestAcceptHeader: acceptHeader, requestContentType: "application/xml"); var input = "testInput"; var result = new ObjectResult(input); var mockCountingFormatter = new Mock <IOutputFormatter>(); var context = new OutputFormatterContext() { HttpContext = actionContext.HttpContext, Object = input, DeclaredType = typeof(string) }; var mockCountingSupportedContentType = MediaTypeHeaderValue.Parse("application/text"); mockCountingFormatter.Setup(o => o.CanWriteResult(context, It.Is <MediaTypeHeaderValue>(mth => mth == null))) .Returns(true); mockCountingFormatter.Setup(o => o.CanWriteResult(context, mockCountingSupportedContentType)) .Returns(true); // Set more than one formatters. The test output formatter throws on write. result.Formatters = new List <IOutputFormatter> { new CannotWriteFormatter(), mockCountingFormatter.Object, }; // Act var formatter = result.SelectFormatter(context, result.Formatters); // Assert Assert.Equal(mockCountingFormatter.Object, formatter); mockCountingFormatter.Verify(v => v.CanWriteResult(context, null), Times.Once()); // CanWriteResult is invoked for the following cases: // 1. For each accept header present // 2. Request Content-Type // 3. Type based match var callCount = (acceptHeaderCollection == null ? 0 : acceptHeaderCollection.Count()) + 2; mockCountingFormatter.Verify(v => v.CanWriteResult(context, It.IsNotIn <MediaTypeHeaderValue>(mockCountingSupportedContentType)), Times.Exactly(callCount)); }
private IOutputFormatter SelectFormatter(ObjectResult objectResult, OutputFormatterContext formatterContext) { if (Formatter == null) { // If no formatter was provided, then run Conneg with the formatters configured in options. var formatters = formatterContext .ActionContext .HttpContext .RequestServices .GetRequiredService <IOutputFormattersProvider>() .OutputFormatters .OfType <IJsonOutputFormatter>() .ToArray(); var formatter = objectResult.SelectFormatter(formatterContext, formatters); if (formatter == null) { // If the available user-configured formatters can't write this type, then fall back to the // 'global' one. formatter = formatterContext .ActionContext .HttpContext .RequestServices .GetRequiredService <JsonOutputFormatter>(); // Run SelectFormatter again to try to choose a content type that this formatter can do. objectResult.SelectFormatter(formatterContext, new[] { formatter }); } return(formatter); } else { // Run SelectFormatter to try to choose a content type that this formatter can do. objectResult.SelectFormatter(formatterContext, new[] { Formatter }); return(Formatter); } }
private IOutputFormatter SelectFormatter(OutputFormatterContext formatterContext) { var defaultFormatters = formatterContext.ActionContext .HttpContext .RequestServices .GetService <IOutputFormattersProvider>() .OutputFormatters; var formatter = _objectResult.SelectFormatter(formatterContext, defaultFormatters); if (formatter == null) { formatter = _defaultFormatter ?? formatterContext.ActionContext .HttpContext .RequestServices .GetService <JsonOutputFormatter>(); } return(formatter); }
public void SelectFormatter_WithNoMatchingAcceptHeadersAndRequestContentType_PicksFormatterBasedOnObjectType (string acceptHeader) { // For no accept headers, // can write is called twice once for the request Content-Type and once for the type match pass. // For each additional accept header, it is called once. // Arrange var acceptHeaderCollection = string.IsNullOrEmpty(acceptHeader) ? null : MediaTypeHeaderValue.ParseList(new[] { acceptHeader }).ToArray(); var stream = new MemoryStream(); var httpResponse = new Mock<HttpResponse>(); httpResponse.SetupProperty<string>(o => o.ContentType); httpResponse.SetupGet(r => r.Body).Returns(stream); var actionContext = CreateMockActionContext(httpResponse.Object, requestAcceptHeader: acceptHeader, requestContentType: "application/xml"); var input = "testInput"; var result = new ObjectResult(input); var mockCountingFormatter = new Mock<IOutputFormatter>(); var context = new OutputFormatterContext() { HttpContext = actionContext.HttpContext, Object = input, DeclaredType = typeof(string) }; var mockCountingSupportedContentType = MediaTypeHeaderValue.Parse("application/text"); mockCountingFormatter.Setup(o => o.CanWriteResult(context, It.Is<MediaTypeHeaderValue>(mth => mth == null))) .Returns(true); mockCountingFormatter.Setup(o => o.CanWriteResult(context, mockCountingSupportedContentType)) .Returns(true); // Set more than one formatters. The test output formatter throws on write. result.Formatters = new List<IOutputFormatter> { new CannotWriteFormatter(), mockCountingFormatter.Object, }; // Act var formatter = result.SelectFormatter(context, result.Formatters); // Assert Assert.Equal(mockCountingFormatter.Object, formatter); mockCountingFormatter.Verify(v => v.CanWriteResult(context, null), Times.Once()); // CanWriteResult is invoked for the following cases: // 1. For each accept header present // 2. Request Content-Type // 3. Type based match var callCount = (acceptHeaderCollection == null ? 0 : acceptHeaderCollection.Count()) + 2; mockCountingFormatter.Verify(v => v.CanWriteResult(context, It.IsNotIn<MediaTypeHeaderValue>(mockCountingSupportedContentType)), Times.Exactly(callCount)); }
private IOutputFormatter SelectFormatter(ObjectResult objectResult, OutputFormatterContext formatterContext) { if (Formatter == null) { // If no formatter was provided, then run Conneg with the formatters configured in options. var formatters = formatterContext .HttpContext .RequestServices .GetRequiredService<IOptions<MvcOptions>>() .Options .OutputFormatters .OfType<IJsonOutputFormatter>() .ToArray(); var formatter = objectResult.SelectFormatter(formatterContext, formatters); if (formatter == null) { // If the available user-configured formatters can't write this type, then fall back to the // 'global' one. formatter = formatterContext .HttpContext .RequestServices .GetRequiredService<JsonOutputFormatter>(); // Run SelectFormatter again to try to choose a content type that this formatter can do. objectResult.SelectFormatter(formatterContext, new[] { formatter }); } return formatter; } else { // Run SelectFormatter to try to choose a content type that this formatter can do. objectResult.SelectFormatter(formatterContext, new[] { Formatter }); return Formatter; } }