Skip to content

Instantly share code, notes, and snippets.

@joncloud
Last active November 2, 2016 15:25
Show Gist options
  • Select an option

  • Save joncloud/2f7ecec14b21d4a41ff100922ce54ef0 to your computer and use it in GitHub Desktop.

Select an option

Save joncloud/2f7ecec14b21d4a41ff100922ce54ef0 to your computer and use it in GitHub Desktop.
Model State Validation
C:\Users\jberube>http get localhost:53043/api/MyModels/10
HTTP/1.1 200 OK
Cache-Control: no-cache
Content-Length: 12
Content-Type: application/json; charset=utf-8
Date: Tue, 01 Nov 2016 20:50:32 GMT
Expires: -1
Pragma: no-cache
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-SourceFiles: =?UTF-8?B?YzpcdXNlcnNcamJlcnViZVxkb2N1bWVudHNcdmlzdWFsIHN0dWRpbyAyMDE1XFByb2plY3RzXFdlYkFwcGxpY2F0aW9uMTZcV2ViQXBwbGljYXRpb24xNlxhcGlcTXlNb2RlbHNcMTA=?=
{
"Value": 10
}
C:\Users\jberube>http get localhost:53043/api/MyModels/101
HTTP/1.1 400 Bad Request
Cache-Control: no-cache
Content-Length: 0
Date: Tue, 01 Nov 2016 20:50:46 GMT
Expires: -1
Pragma: no-cache
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-SourceFiles: =?UTF-8?B?YzpcdXNlcnNcamJlcnViZVxkb2N1bWVudHNcdmlzdWFsIHN0dWRpbyAyMDE1XFByb2plY3RzXFdlYkFwcGxpY2F0aW9uMTZcV2ViQXBwbGljYXRpb24xNlxhcGlcTXlNb2RlbHNcMTAx?=
public class MyModel
{
public const int MaximumValue = 100;
public const int MinimumValue = 1;
[Range(MinimumValue, MaximumValue)]
public int Value { get; set; }
}
public class MyModelsController : ApiController
{
[Route("api/MyModels/{Value}")]
public IHttpActionResult Get([FromUri]MyModel model)
{
if (!ModelState.IsValid)
return BadRequest();
return Ok(model);
}
}
[Fact]
public void GetReturnsBadRequestForInvalid()
{
var target = new MyModelsController();
target.ModelState.AddModelError("*", "Anything goes wrong");
IHttpActionResult actual = target.Get(null);
Assert.IsType<BadRequestResult>(actual);
}
[Fact]
public void GetReturnsOKAndModelForValid()
{
var target = new MyModelsController();
var expected = new MyModel();
IHttpActionResult result = target.Get(expected);
var okResult = Assert.IsType<OkNegotiatedContentResult<MyModel>>(result);
Assert.Same(expected, okResult.Content);
}
static MyModel CreateValid() => new MyModel
{
Value = MyModel.MinimumValue
};
[InlineData(MyModel.MaximumValue)]
[InlineData(MyModel.MaximumValue - 1)]
[InlineData(MyModel.MaximumValue - 10)]
[InlineData(MyModel.MinimumValue + 10)]
[InlineData(MyModel.MinimumValue + 1)]
[InlineData(MyModel.MinimumValue)]
[Theory]
public void InRangeIsValid(int value)
{
var target = CreateValid();
target.Value = value;
bool actual = ValidationTest.IsValid(target);
Assert.True(actual, "MyModel is invalid when in range.");
}
[InlineData(MyModel.MaximumValue + 1)]
[InlineData(MyModel.MinimumValue - 1)]
[InlineData(int.MaxValue)]
[InlineData(int.MinValue)]
[Theory]
public void OutOfRangeIsInvalid(int value)
{
var target = CreateValid();
target.Value = value;
bool actual = ValidationTest.IsValid(target);
Assert.False(actual, "MyModel is valid when out of range.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment