added tests for json input formatter

This commit is contained in:
Ajay Bhargav Baaskaran 2014-11-10 11:55:31 -08:00
parent 17e4dd2bf6
commit 232deb47d0
2 changed files with 69 additions and 0 deletions

View File

@ -104,5 +104,64 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
Assert.Equal("dummy", result.ParameterName);
Assert.Equal(expectedSource, result.Source);
}
[Theory]
[InlineData("application/json", "{\"SampleInt\":10}", 10)]
[InlineData("application/json", "{}", 0)]
public async Task JsonInputFormatter_IsModelStateValid_ForValidContentType(string requestContentType, string jsonInput, int expectedSampleIntValue)
{
// Arrange
var server = TestServer.Create(_services, _app);
var client = server.CreateClient();
var content = new StringContent(jsonInput, Encoding.UTF8, requestContentType);
// Act
var response = await client.PostAsync("http://localhost/JsonFormatter/ReturnInput/", content);
var responseBody = await response.Content.ReadAsStringAsync();
//Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(expectedSampleIntValue.ToString(), responseBody);
}
[Theory]
[InlineData("{\"SampleInt\":10}")]
[InlineData("{}")]
[InlineData("")]
public async Task JsonInputFormatter_IsModelStateInvalid_ForEmptyContentType(string jsonInput)
{
// Arrange
var server = TestServer.Create(_services, _app);
var client = server.CreateClient();
var content = new StringContent(jsonInput, Encoding.UTF8, "application/json");
content.Headers.Clear();
// Act
var response = await client.PostAsync("http://localhost/JsonFormatter/ReturnInput/", content);
var responseBody = await response.Content.ReadAsStringAsync();
//Assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}
[Theory]
[InlineData("application/json", "{\"SampleInt\":10}", 10)]
[InlineData("application/json", "{}", 0)]
public async Task JsonInputFormatter_IsModelStateValid_ForTransferEncodingChunk(string requestContentType, string jsonInput, int expectedSampleIntValue)
{
// Arrange
var server = TestServer.Create(_services, _app);
var client = server.CreateClient();
var content = new StringContent(jsonInput, Encoding.UTF8, requestContentType);
client.DefaultRequestHeaders.TransferEncodingChunked = true;
// Act
var response = await client.PostAsync("http://localhost/JsonFormatter/ReturnInput/", content);
var responseBody = await response.Content.ReadAsStringAsync();
//Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(expectedSampleIntValue.ToString(), responseBody);
}
}
}

View File

@ -25,5 +25,15 @@ namespace FormatterWebSite.Controllers
return objectResult;
}
[HttpPost]
public IActionResult ReturnInput([FromBody]DummyClass dummyObject)
{
if (!ModelState.IsValid)
{
return new HttpStatusCodeResult(400);
}
return Content(dummyObject.SampleInt.ToString());
}
}
}