144 lines
4.9 KiB
C#
144 lines
4.9 KiB
C#
// Copyright (c) .NET Foundation. All rights reserved.
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNet.Http;
|
|
using Microsoft.AspNet.Http.Internal;
|
|
using Microsoft.AspNet.Mvc.Actions;
|
|
using Microsoft.AspNet.Routing;
|
|
using Microsoft.Framework.DependencyInjection;
|
|
using Microsoft.Net.Http.Headers;
|
|
using Newtonsoft.Json;
|
|
using Xunit;
|
|
|
|
namespace Microsoft.AspNet.Mvc
|
|
{
|
|
public class JsonResultTest
|
|
{
|
|
private static readonly byte[] _abcdUTF8Bytes
|
|
= new byte[] { 123, 34, 102, 111, 111, 34, 58, 34, 97, 98, 99, 100, 34, 125 };
|
|
|
|
[Fact]
|
|
public async Task ExecuteResultAsync_UsesDefaultContentType_IfNoContentTypeSpecified()
|
|
{
|
|
// Arrange
|
|
var expected = _abcdUTF8Bytes;
|
|
|
|
var context = GetHttpContext();
|
|
var actionContext = new ActionContext(context, new RouteData(), new ActionDescriptor());
|
|
|
|
var result = new JsonResult(new { foo = "abcd" });
|
|
|
|
// Act
|
|
await result.ExecuteResultAsync(actionContext);
|
|
var written = GetWrittenBytes(context);
|
|
|
|
// Assert
|
|
Assert.Equal(expected, written);
|
|
Assert.Equal("application/json; charset=utf-8", context.Response.ContentType);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExecuteResultAsync_NullEncoding_SetsContentTypeAndDefaultEncoding()
|
|
{
|
|
// Arrange
|
|
var expected = _abcdUTF8Bytes;
|
|
|
|
var context = GetHttpContext();
|
|
var actionContext = new ActionContext(context, new RouteData(), new ActionDescriptor());
|
|
|
|
var result = new JsonResult(new { foo = "abcd" });
|
|
result.ContentType = new MediaTypeHeaderValue("text/json");
|
|
|
|
// Act
|
|
await result.ExecuteResultAsync(actionContext);
|
|
var written = GetWrittenBytes(context);
|
|
|
|
// Assert
|
|
Assert.Equal(expected, written);
|
|
Assert.Equal("text/json; charset=utf-8", context.Response.ContentType);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExecuteResultAsync_SetsContentTypeAndEncoding()
|
|
{
|
|
// Arrange
|
|
var expected = _abcdUTF8Bytes;
|
|
|
|
var context = GetHttpContext();
|
|
var actionContext = new ActionContext(context, new RouteData(), new ActionDescriptor());
|
|
|
|
var result = new JsonResult(new { foo = "abcd" });
|
|
result.ContentType = new MediaTypeHeaderValue("text/json")
|
|
{
|
|
Encoding = Encoding.ASCII
|
|
};
|
|
|
|
// Act
|
|
await result.ExecuteResultAsync(actionContext);
|
|
var written = GetWrittenBytes(context);
|
|
|
|
// Assert
|
|
Assert.Equal(expected, written);
|
|
Assert.Equal("text/json; charset=us-ascii", context.Response.ContentType);
|
|
}
|
|
|
|
private static List<byte> AbcdIndentedUTF8Bytes
|
|
{
|
|
get
|
|
{
|
|
var bytes = new List<byte>();
|
|
bytes.Add(123);
|
|
bytes.AddRange(Encoding.UTF8.GetBytes(Environment.NewLine));
|
|
bytes.AddRange(new byte[] { 32, 32, 34, 102, 111, 111, 34, 58, 32, 34, 97, 98, 99, 100, 34 });
|
|
bytes.AddRange(Encoding.UTF8.GetBytes(Environment.NewLine));
|
|
bytes.Add(125);
|
|
return bytes;
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExecuteResultAsync_UsesPassedInSerializerSettings()
|
|
{
|
|
// Arrange
|
|
var expected = AbcdIndentedUTF8Bytes;
|
|
|
|
var context = GetHttpContext();
|
|
var actionContext = new ActionContext(context, new RouteData(), new ActionDescriptor());
|
|
|
|
var serializerSettings = new JsonSerializerSettings();
|
|
serializerSettings.Formatting = Formatting.Indented;
|
|
|
|
var result = new JsonResult(new { foo = "abcd" }, serializerSettings);
|
|
|
|
// Act
|
|
await result.ExecuteResultAsync(actionContext);
|
|
var written = GetWrittenBytes(context);
|
|
|
|
// Assert
|
|
Assert.Equal(expected, written);
|
|
Assert.Equal("application/json; charset=utf-8", context.Response.ContentType);
|
|
}
|
|
|
|
private static HttpContext GetHttpContext()
|
|
{
|
|
var httpContext = new DefaultHttpContext();
|
|
httpContext.Response.Body = new MemoryStream();
|
|
var services = new ServiceCollection();
|
|
services.AddOptions();
|
|
httpContext.RequestServices = services.BuildServiceProvider();
|
|
|
|
return httpContext;
|
|
}
|
|
|
|
private static byte[] GetWrittenBytes(HttpContext context)
|
|
{
|
|
context.Response.Body.Seek(0, SeekOrigin.Begin);
|
|
return Assert.IsType<MemoryStream>(context.Response.Body).ToArray();
|
|
}
|
|
}
|
|
} |