Esempio n. 1
0
        public async Task WriteAsync(OutputFormatterWriteContext context)
        {
            var halResponse = context.Object as HALResponse;

            if (halResponse == null)
            {
                await jsonFormatter.WriteAsync(context);

                return;
            }

            string mediaType = context.ContentType.HasValue ? context.ContentType.Value : null;

            object value = null;

            // If it is a HAL response but set to application/json - convert to a plain response
            var serializer = JsonSerializer.Create(this.serializerSettings);

            if (!halResponse.Config.ForceHAL && !halJsonMediaTypes.Contains(mediaType))
            {
                value = halResponse.ToPlainResponse(serializer);
            }
            else
            {
                value = halResponse.ToJObject(serializer);
            }

            var jsonContext = new OutputFormatterWriteContext(context.HttpContext, context.WriterFactory, value.GetType(), value);

            jsonContext.ContentType = new StringSegment(mediaType);

            await jsonFormatter.WriteAsync(jsonContext);
        }
Esempio n. 2
0
        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 WriteAsync(OutputFormatterWriteContext context)
        {
            string mediaType = context.ContentType.MediaType;

            object value       = null;
            var    halResponse = ((HALResponse)context.Object);

            // If it is a HAL response but set to application/json - convert to a plain response
            var serializer = Newtonsoft.Json.JsonSerializer.Create(jsonFormatter.SerializerSettings);

            if (!halResponse.Config.ForceHAL && halJsonMediaTypes.Contains(mediaType))
            {
                value = halResponse.ToPlainResponse(serializer);
            }
            else
            {
                value = halResponse.ToJObject(serializer);
            }

            var jsonContext = new OutputFormatterWriteContext(context.HttpContext, context.WriterFactory, value.GetType(), value);

            await jsonFormatter.WriteAsync(jsonContext);
        }
Esempio n. 4
0
        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());
        }