[Fixes #1283] Add functional tests for WebAPI-style controllers returning HttpContent

This commit is contained in:
Kiran Challa 2014-10-10 13:49:27 -07:00
parent 75c6327b2e
commit acb2a99e9f
2 changed files with 164 additions and 2 deletions

View File

@ -434,6 +434,91 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
// Assert
Assert.Equal(expected, response.StatusCode);
}
[Fact]
public async Task ApiController_Returns_ByteArrayContent()
{
// Arrange
var server = TestServer.Create(_provider, _app);
var client = server.CreateClient();
var expectedBody = "Hello from ByteArrayContent!!";
// Act
var response = await client.GetAsync("http://localhost/api/Blog/HttpRequestMessage/ReturnByteArrayContent");
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.Equal("text/plain", response.Content.Headers.ContentType.MediaType);
var actualBody = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedBody, actualBody);
}
[Fact]
public async Task ApiController_Returns_StreamContent()
{
// Arrange
var server = TestServer.Create(_provider, _app);
var client = server.CreateClient();
var expectedBody = "This content is from a file";
// Act
var response = await client.GetAsync("http://localhost/api/Blog/HttpRequestMessage/ReturnStreamContent");
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.Equal("image/jpeg", response.Content.Headers.ContentType.MediaType);
Assert.NotNull(response.Content.Headers.ContentDisposition);
Assert.Equal("attachment", response.Content.Headers.ContentDisposition.DispositionType);
var actualBody = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedBody, actualBody);
}
[Theory]
[InlineData("ReturnPushStreamContent", "Hello from PushStreamContent!!")]
[InlineData("ReturnPushStreamContentSync", "Hello from PushStreamContent Sync!!")]
public async Task ApiController_Returns_PushStreamContent(string action, string expectedBody)
{
// Arrange
var server = TestServer.Create(_provider, _app);
var client = server.CreateClient();
// Act
var response = await client.GetAsync("http://localhost/api/Blog/HttpRequestMessage/" + action );
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.Equal("application/pdf", response.Content.Headers.ContentType.MediaType);
var actualBody = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedBody, actualBody);
}
[Fact]
public async Task ApiController_Returns_PushStreamContentWithCustomHeaders()
{
// Arrange
var server = TestServer.Create(_provider, _app);
var client = server.CreateClient();
var expectedBody = "Hello from PushStreamContent with custom headers!!";
var multipleValues = new[] { "value1", "value2" };
// Act
var response = await client.GetAsync("http://localhost/api/Blog/HttpRequestMessage/ReturnPushStreamContentWithCustomHeaders");
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.Equal("application/octet-stream", response.Content.Headers.ContentType.ToString());
var actualBody = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedBody, actualBody);
Assert.Equal(multipleValues, response.Headers.GetValues("Multiple"));
}
}
}
#endif

View File

@ -2,13 +2,16 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc;
using System.Net;
using System.Net.Http.Formatting;
namespace WebApiCompatShimWebSite
{
@ -94,6 +97,80 @@ namespace WebApiCompatShimWebSite
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "It failed.");
}
[HttpGet]
public HttpResponseMessage ReturnByteArrayContent()
{
var response = new HttpResponseMessage();
response.Content = new ByteArrayContent(Encoding.UTF8.GetBytes("Hello from ByteArrayContent!!"));
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
return response;
}
[HttpGet]
public HttpResponseMessage ReturnStreamContent()
{
var response = new HttpResponseMessage();
response.Content = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes("This content is from a file")));
response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
return response;
}
// NOTE: PushStreamContent's contract is to close the stream in order to signal
// that the user has done writing to it. However, the stream that is provided here is
// a wrapper delegating stream which actually doesn't close the actual response stream.
[HttpGet]
public HttpResponseMessage ReturnPushStreamContentSync()
{
var response = new HttpResponseMessage();
// Here we are using a non-Task returning action delegate
response.Content = new PushStreamContent((responseStream, httpContent, transportContext) =>
{
using (var streamWriter = new StreamWriter(responseStream))
{
streamWriter.Write("Hello from PushStreamContent Sync!!");
}
});
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return response;
}
[HttpGet]
public HttpResponseMessage ReturnPushStreamContent()
{
var response = new HttpResponseMessage();
response.Content = new PushStreamContent(async (responseStream, httpContent, transportContext) =>
{
using (var streamWriter = new StreamWriter(responseStream))
{
await streamWriter.WriteAsync("Hello from PushStreamContent!!");
}
});
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return response;
}
[HttpGet]
public HttpResponseMessage ReturnPushStreamContentWithCustomHeaders()
{
var response = new HttpResponseMessage();
response.Headers.Add("Multiple", new[] { "value1", "value2" });
response.Content = new PushStreamContent(async (responseStream, httpContent, transportContext) =>
{
using (var streamWriter = new StreamWriter(responseStream))
{
await streamWriter.WriteAsync("Hello from PushStreamContent with custom headers!!");
}
});
return response;
}
private async Task Echo(HttpRequestMessage request)
{
var message = string.Format(