public void ShouldStoreStatusCode() { HttpStatusCode statusCode = HttpStatusCode.InternalServerError; JsonWithHttpStatusCodeResult result = new JsonWithHttpStatusCodeResult(statusCode); Assert.AreEqual((int)statusCode, result.StatusCode, "The status code was not set."); }
public static TModel AssertJsonWithHttpStatusCode <TModel>(ActionResult result, HttpStatusCode statusCode) { Assert.IsInstanceOfType(result, typeof(JsonWithHttpStatusCodeResult), "JSON was not returned."); JsonWithHttpStatusCodeResult jsonResult = (JsonWithHttpStatusCodeResult)result; Assert.AreEqual((int)statusCode, jsonResult.StatusCode, "The wrong HTTP status code was returned."); Assert.IsInstanceOfType(jsonResult.Data, typeof(TModel), "The wrong model was passed to the JSON."); TModel model = (TModel)jsonResult.Data; return(model); }
public void ShouldSetTheContentType() { HttpContextBase httpContext = Substitute.For <HttpContextBase>(); RouteData routeData = new RouteData(); ControllerBase controller = Substitute.For <ControllerBase>(); ControllerContext context = new ControllerContext(httpContext, routeData, controller); JsonWithHttpStatusCodeResult result = new JsonWithHttpStatusCodeResult(HttpStatusCode.InternalServerError); result.ExecuteResult(context); Assert.AreEqual("application/json", httpContext.Response.ContentType, "The content type was not set."); }
public void ShouldSetTheResponseStatusCode() { HttpContextBase httpContext = Substitute.For <HttpContextBase>(); RouteData routeData = new RouteData(); ControllerBase controller = Substitute.For <ControllerBase>(); ControllerContext context = new ControllerContext(httpContext, routeData, controller); JsonWithHttpStatusCodeResult result = new JsonWithHttpStatusCodeResult((int)HttpStatusCode.InternalServerError); result.ExecuteResult(context); Assert.AreEqual(result.StatusCode, httpContext.Response.StatusCode, "The status code was not set."); }
public void ShouldSerializeDataToJson() { using (StringWriter writer = new StringWriter(CultureInfo.CurrentCulture)) { HttpContextBase httpContext = Substitute.For <HttpContextBase>(); httpContext.Response.When(c => c.Write(Arg.Any <string>())).Do(c => writer.Write(c.Arg <string>())); RouteData routeData = new RouteData(); ControllerBase controller = Substitute.For <ControllerBase>(); ControllerContext context = new ControllerContext(httpContext, routeData, controller); JsonWithHttpStatusCodeResult result = new JsonWithHttpStatusCodeResult(HttpStatusCode.InternalServerError); result.Data = new { Value = 123 }; result.ExecuteResult(context); writer.Flush(); string content = writer.ToString(); Assert.AreEqual(@"{""Value"":123}", content, "The content was not written."); } }