コード例 #1
0
    public async Task ContentResult_ExecuteAsync_SetContentTypeAndEncoding_OnResponse(
        MediaTypeHeaderValue contentType,
        string content,
        string responseContentType,
        string expectedContentType,
        byte[] expectedContentData)
    {
        // Arrange
        var contentResult = new ContentResult
        {
            Content     = content,
            ContentType = contentType?.ToString()
        };
        var httpContext  = GetHttpContext();
        var memoryStream = new MemoryStream();

        httpContext.Response.Body        = memoryStream;
        httpContext.Response.ContentType = responseContentType;

        // Act
        await contentResult.ExecuteAsync(httpContext);

        // Assert
        var finalResponseContentType = httpContext.Response.ContentType;

        Assert.Equal(expectedContentType, finalResponseContentType);
        Assert.Equal(expectedContentData, memoryStream.ToArray());
        Assert.Equal(expectedContentData.Length, httpContext.Response.ContentLength);
    }
コード例 #2
0
    public async Task ContentResult_ExecuteAsync_Response_NullContent_SetsContentTypeAndEncoding()
    {
        // Arrange
        var contentResult = new ContentResult
        {
            Content     = null,
            ContentType = new MediaTypeHeaderValue("text/plain")
            {
                Encoding = Encoding.Unicode
            }.ToString()
        };
        var httpContext = GetHttpContext();

        // Act
        await contentResult.ExecuteAsync(httpContext);

        // Assert
        Assert.Equal("text/plain; charset=utf-16", httpContext.Response.ContentType);
    }