public async Task ObjectResult_Execute_CallsJsonResult_SetsContent() { // Arrange var expectedContentType = "application/json;charset=utf-8"; var nonStringValue = new { x1 = 10, y1 = "Hello" }; var httpResponse = Mock.Of <HttpResponse>(); httpResponse.Body = new MemoryStream(); var actionContext = CreateMockActionContext(httpResponse); var tempStream = new MemoryStream(); var tempHttpContext = new Mock <HttpContext>(); var tempHttpResponse = new Mock <HttpResponse>(); tempHttpResponse.SetupGet(o => o.Body).Returns(tempStream); tempHttpResponse.SetupProperty <string>(o => o.ContentType); tempHttpContext.SetupGet(o => o.Response).Returns(tempHttpResponse.Object); tempHttpContext.SetupGet(o => o.Request.AcceptCharset).Returns(string.Empty); var tempActionContext = new ActionContext(tempHttpContext.Object, new RouteData(), new ActionDescriptor()); var formatterContext = new OutputFormatterContext() { ActionContext = tempActionContext, Object = nonStringValue, DeclaredType = nonStringValue.GetType() }; var formatter = new JsonOutputFormatter(); formatter.WriteResponseHeaders(formatterContext); await formatter.WriteAsync(formatterContext); // Act var result = new ObjectResult(nonStringValue); await result.ExecuteResultAsync(actionContext); // Assert Assert.Equal(expectedContentType, httpResponse.ContentType); Assert.Equal(tempStream.ToArray(), ((MemoryStream)actionContext.HttpContext.Response.Body).ToArray()); }
public async Task ObjectResult_Execute_CallsJsonResult_SetsContent() { // Arrange var expectedContentType = "application/json; charset=utf-8"; var nonStringValue = new { x1 = 10, y1 = "Hello" }; var httpResponse = Mock.Of<HttpResponse>(); httpResponse.Body = new MemoryStream(); var actionContext = CreateMockActionContext(httpResponse); var tempStream = new MemoryStream(); var tempHttpContext = new Mock<HttpContext>(); var tempHttpResponse = new Mock<HttpResponse>(); tempHttpResponse.SetupGet(o => o.Body).Returns(tempStream); tempHttpResponse.SetupProperty<string>(o => o.ContentType); tempHttpContext.SetupGet(o => o.Request).Returns(new DefaultHttpContext().Request); tempHttpContext.SetupGet(o => o.Response).Returns(tempHttpResponse.Object); var tempActionContext = new ActionContext(tempHttpContext.Object, new RouteData(), new ActionDescriptor()); var formatterContext = new OutputFormatterContext() { HttpContext = tempActionContext.HttpContext, Object = nonStringValue, DeclaredType = nonStringValue.GetType() }; var formatter = new JsonOutputFormatter(); formatter.WriteResponseHeaders(formatterContext); await formatter.WriteAsync(formatterContext); // Act var result = new ObjectResult(nonStringValue); await result.ExecuteResultAsync(actionContext); // Assert Assert.Equal(expectedContentType, httpResponse.ContentType); Assert.Equal(tempStream.ToArray(), ((MemoryStream)actionContext.HttpContext.Response.Body).ToArray()); }