public void ValidateInvalidValue() { MockWebContext ctx = new MockWebContext(); ctx.ActionArguments.Add("id", "{CA761232-ED42-11CE-BACD-00AA0057B223"); // missing ending curly brace var actExeContext = ctx.CreateActionExecutingContext(); ValidationBaseAttribute attr = new ValidationBaseAttribute("id", new ValidGuidAttribute()); attr.OnActionExecuting(actExeContext); // bad request response, http status code 400 actExeContext.HttpContext.Response.StatusCode.Should().Be((int)HttpStatusCode.BadRequest); }
public void ValidateValidValue() { MockWebContext ctx = new MockWebContext(); ctx.ActionArguments.Add("id", "{CA761232-ED42-11CE-BACD-00AA0057B223}"); var actExeContext = ctx.CreateActionExecutingContext(); ValidationBaseAttribute attr = new ValidationBaseAttribute("id", new ValidGuidAttribute()); attr.OnActionExecuting(actExeContext); // if the argument needs to be present when using this validator then we should expect here validation error with http status code 400 or 422 // if everything is ok the status is not changed so default 0 value is the expected value and not http 200 actExeContext.HttpContext.Response.StatusCode.Should().Be(0); }
public void ValidatorShouldntThrowWhenKeyNameIsMissingFromRequest() { // it shouldn't throw if it is ok that the value is missing from request // for example with guid attribute null and empty values are ok by the validator // or the implementation of the validator should be changed so that it doesn't // crash with KeyNotFoundException, the code should check if the required key exists and if not return // the similiar response (json) as it currently returns if the supplied value is not valid MockWebContext ctx = new MockWebContext(); ctx.ActionArguments.Add("id", "{CA761232-ED42-11CE-BACD-00AA0057B223}"); var actExeContext = ctx.CreateActionExecutingContext(); ValidationBaseAttribute attr = new ValidationBaseAttribute("notfoundkeyname", new ValidGuidAttribute()); attr.OnActionExecuting(actExeContext); // if the argument needs to be present when using this validator then we should expect here validation error with http status code 400 or 422 actExeContext.HttpContext.Response.StatusCode.Should().Be((int)HttpStatusCode.BadRequest); }