本文整理汇总了C#中ObjectResult.SelectFormatter方法的典型用法代码示例。如果您正苦于以下问题:C# ObjectResult.SelectFormatter方法的具体用法?C# ObjectResult.SelectFormatter怎么用?C# ObjectResult.SelectFormatter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObjectResult
的用法示例。
在下文中一共展示了ObjectResult.SelectFormatter方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MemoryStream
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));
}