/// <summary> /// executes the action to the client /// Now, it just uses a simple json execution /// </summary> /// <param name="context">the context</param> public override void ExecuteResult(ControllerContext context) { JsonResult jResult = new JsonResult(); jResult.Data = this; jResult.ExecuteResult(context); }
protected void Application_Error() { //log error Exception ex = Server.GetLastError(); if (ex != null) { BOL.Repository.LogRepository.LogException(ex); } HttpContext httpContext = HttpContext.Current; if (httpContext != null) { RequestContext requestContext = ((MvcHandler)httpContext.CurrentHandler).RequestContext; if (requestContext.HttpContext.Request.IsAjaxRequest()) { httpContext.Response.Clear(); string controllerName = requestContext.RouteData.GetRequiredString("controller"); IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory(); IController controller = factory.CreateController(requestContext, controllerName); ControllerContext controllerContext = new ControllerContext(requestContext, (ControllerBase)controller); JsonResult jsonResult = new JsonResult(); jsonResult.Data = new { status = "error" }; jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet; jsonResult.ExecuteResult(controllerContext); httpContext.Response.End(); } else { //httpContext.Response.Redirect("~/Error"); } } }
protected override void OnException(ExceptionContext filterContext) { if (filterContext.HttpContext.IsCustomErrorEnabled || true) //IsCustomErrorEnabled always false if client is localhost or client and server IPs identical. True set to override. { filterContext.ExceptionHandled = true; // If this is an ajax request, return the exception in the response if (filterContext.HttpContext.Request.IsAjaxRequest()) { //if (HttpContext.Response.ContentType == "text/html") //{ // filterContext.HttpContext.Response.StatusCode = 500; // var json = new JsonResult(); // json.JsonRequestBehavior = JsonRequestBehavior.AllowGet; //SICHERHEITSPROBLEM: s. http://haacked.com/archive/2009/06/25/json-hijacking.aspx // json.Data = HttpUtility.UrlEncode(filterContext.Exception.Message); // +filterContext.Exception.StackTrace; // json.ExecuteResult(this.ControllerContext); //} //else //{ filterContext.HttpContext.Response.StatusCode = 500; var json = new JsonResult(); json.JsonRequestBehavior = JsonRequestBehavior.AllowGet; //SICHERHEITSPROBLEM: s. http://haacked.com/archive/2009/06/25/json-hijacking.aspx json.Data = HttpUtility.UrlEncode(filterContext.Exception.Message); // +filterContext.Exception.StackTrace; json.ExecuteResult(this.ControllerContext); //} } else { ViewData.Model = new System.Web.Mvc.HandleErrorInfo(filterContext.Exception, "ControllerName", "ActionName"); // Pass a flag to the view to tell it whether or not to show a the stack trace ViewBag.IsCustomErrorEnabled = true; //filterContext.HttpContext.IsCustomErrorEnabled; this.View("Error").ExecuteResult(this.ControllerContext); } } }
public override void ExecuteResult(ControllerContext context) { var contentType = context.RequestContext.HttpContext.Request.Headers["Accept"]; if (contentType != null && contentType.Contains("application/json")) { if (Model is IEnumerable && !(Model is string)) { Model = new CollectionWrapper { Collection = (IEnumerable)Model }; } var result = new JsonResult { Data = Model, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; result.ExecuteResult(context); } else { context.Controller.ViewData.Model = Model; var result = new ViewResult { ViewData = context.Controller.ViewData }; result.ExecuteResult(context); } }
protected void Application_Error(Object sender, EventArgs e) { HttpContext httpContext = HttpContext.Current; if (httpContext != null) { RequestContext requestContext = ((MvcHandler)httpContext.CurrentHandler).RequestContext; /* when the request is ajax the system can automatically handle a mistake with a JSON response. then overwrites the default response */ if (requestContext.HttpContext.Request.IsAjaxRequest()) { httpContext.Response.Clear(); string controllerName = requestContext.RouteData.GetRequiredString("GIMS"); IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory(); IController controller = factory.CreateController(requestContext, controllerName); ControllerContext controllerContext = new ControllerContext(requestContext, (ControllerBase)controller); JsonResult jsonResult = new JsonResult(); jsonResult.Data = new { success = false, serverError = "500" }; jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet; jsonResult.ExecuteResult(controllerContext); httpContext.Response.End(); } else { httpContext.Response.Redirect("~/GIMS/ErrorView"); } } }
public override void OnException(ExceptionContext filterContext) { _logger.Error(filterContext.Exception); string viewName = "~/Views/Home/Error.cshtml"; var httpContext = HttpContext.Current; if (httpContext == null) { return; } if (filterContext.ExceptionHandled || filterContext.HttpContext.IsCustomErrorEnabled) { return; } if (filterContext.Exception is HttpException) { //A http exception has occourd } else if (filterContext.Exception is UnauthorizedAccessException) { httpContext.Response.Redirect(@"/Home/Page504"); } else if (filterContext.Exception is WebException) { httpContext.Response.Redirect(@"/Home/Page404"); } filterContext.ExceptionHandled = true; var actionName = (string)filterContext.RouteData.Values["action"]; var controllerName = (string)filterContext.RouteData.Values["controller"]; var requestContext = ((MvcHandler)httpContext.CurrentHandler).RequestContext; if (requestContext.HttpContext.Request.IsAjaxRequest()) { httpContext.Response.Clear(); var factory = ControllerBuilder.Current.GetControllerFactory(); var controller = factory.CreateController(requestContext, controllerName); var controllerContext = new ControllerContext(requestContext, (ControllerBase)controller); var jsonResult = new JsonResult { Data = new { success = false, serverError = "500" }, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; jsonResult.ExecuteResult(controllerContext); httpContext.Response.End(); } else { var viewResult = new ViewResult { MasterName = Master, TempData = filterContext.Controller.TempData, ViewName = viewName }; if (filterContext.Exception.InnerException != null && filterContext.Exception.InnerException.Source.Equals(GeneralMessages.ExceptionSource)) { viewResult.ViewData = new ViewDataDictionary(filterContext.Exception.Message); } filterContext.Result = viewResult; } }
public void NullRecursionLimitDefaultIsUsed() { // Arrange Tuple <string, Tuple <string, Tuple <string, string> > > data = new Tuple <string, Tuple <string, Tuple <string, string> > >("key1", new Tuple <string, Tuple <string, string> >("key2", new Tuple <string, string>("key3", "value") ) ); string jsonData = "{\"Item1\":\"key1\",\"Item2\":{\"Item1\":\"key2\",\"Item2\":{\"Item1\":\"key3\",\"Item2\":\"value\"}}}"; Mock <ControllerContext> mockControllerContext = new Mock <ControllerContext>(); mockControllerContext.SetupGet(c => c.HttpContext.Request.HttpMethod).Returns("POST").Verifiable(); mockControllerContext.SetupSet(c => c.HttpContext.Response.ContentType = "application/json").Verifiable(); mockControllerContext.Setup(c => c.HttpContext.Response.Write(jsonData)).Verifiable(); JsonResult result = new JsonResult { Data = data }; // Act result.ExecuteResult(mockControllerContext.Object); // Assert mockControllerContext.Verify(); }
public void NullMaxJsonLengthDefaultIsUsed() { // Arrange string data = new String('1', 2100000); Mock <ControllerContext> mockControllerContext = new Mock <ControllerContext>( MockBehavior.Strict ); mockControllerContext.SetupGet(c => c.HttpContext.Request.HttpMethod).Returns("POST"); // Though most other tests run fine with SetupSet(c => c.HttpContext.Response.ContentType = ...), this // one does not. Explicitly ensure Response property is not null. var response = new Mock <HttpResponseBase>(MockBehavior.Strict); response.SetupSet(r => r.ContentType = "application/json"); mockControllerContext.SetupGet(c => c.HttpContext.Response).Returns(response.Object); JsonResult result = new JsonResult { Data = data }; // Act & Assert Assert.Throws <InvalidOperationException>( () => result.ExecuteResult(mockControllerContext.Object), "Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property." ); }
public void MaxJsonLengthIsPassedToSerializer() { // Arrange string data = new String('1', 2100000); string jsonData = "\"" + data + "\""; Mock <ControllerContext> mockControllerContext = new Mock <ControllerContext>(); mockControllerContext .SetupGet(c => c.HttpContext.Request.HttpMethod) .Returns("POST") .Verifiable(); mockControllerContext .SetupSet(c => c.HttpContext.Response.ContentType = "application/json") .Verifiable(); mockControllerContext.Setup(c => c.HttpContext.Response.Write(jsonData)).Verifiable(); JsonResult result = new JsonResult { Data = data, MaxJsonLength = 2200000 }; // Act result.ExecuteResult(mockControllerContext.Object); // Assert mockControllerContext.Verify(); }
public void NullContentIsNotOutput() { // Arrange string contentType = "Some content type."; Encoding contentEncoding = Encoding.UTF8; // Arrange expectations Mock <ControllerContext> mockControllerContext = new Mock <ControllerContext>(); mockControllerContext.SetupGet(c => c.HttpContext.Request.HttpMethod).Returns("POST").Verifiable(); mockControllerContext.SetupSet(c => c.HttpContext.Response.ContentType = contentType).Verifiable(); mockControllerContext.SetupSet(c => c.HttpContext.Response.ContentEncoding = contentEncoding).Verifiable(); JsonResult result = new JsonResult { ContentType = contentType, ContentEncoding = contentEncoding }; // Act result.ExecuteResult(mockControllerContext.Object); // Assert mockControllerContext.Verify(); }
public void ExecuteResult() { // Arrange object data = _jsonData; string contentType = "Some content type."; Encoding contentEncoding = Encoding.UTF8; // Arrange expectations Mock <ControllerContext> mockControllerContext = new Mock <ControllerContext>(MockBehavior.Strict); mockControllerContext.SetupGet(c => c.HttpContext.Request.HttpMethod).Returns("POST").Verifiable(); mockControllerContext.SetupSet(c => c.HttpContext.Response.ContentType = contentType).Verifiable(); mockControllerContext.SetupSet(c => c.HttpContext.Response.ContentEncoding = contentEncoding).Verifiable(); mockControllerContext.Setup(c => c.HttpContext.Response.Write(_jsonSerializedData)).Verifiable(); JsonResult result = new JsonResult { Data = data, ContentType = contentType, ContentEncoding = contentEncoding }; // Act result.ExecuteResult(mockControllerContext.Object); // Assert mockControllerContext.Verify(); }
public void NullContentIsNotOutput() { // Arrange string contentType = "Some content type."; Encoding contentEncoding = Encoding.UTF8; // Arrange expectations Mock <ControllerContext> mockControllerContext = new Mock <ControllerContext>(MockBehavior.Strict); mockControllerContext.SetupGet(c => c.HttpContext.Request.HttpMethod).Returns("POST").Verifiable(); // Though most other tests run fine with SetupSet(c => c.HttpContext.Response.ContentType = ...), this // one does not. Explicitly ensure Response property is not null. var response = new Mock <HttpResponseBase>(MockBehavior.Strict); response.SetupSet(r => r.ContentType = contentType).Verifiable(); response.SetupSet(r => r.ContentEncoding = contentEncoding).Verifiable(); mockControllerContext.SetupGet(c => c.HttpContext.Response).Returns(response.Object); JsonResult result = new JsonResult { ContentType = contentType, ContentEncoding = contentEncoding }; // Act result.ExecuteResult(mockControllerContext.Object); // Assert mockControllerContext.Verify(); response.Verify(); }
private static void RenderAjaxView(HttpApplication httpApplication, HttpStatusCode httpStatusCode, Exception currentError) { // Ok. lets check if this content type contains a request for json. string errorMessage = httpApplication.Request.ContentType.Contains("json") ? currentError.Message : string.Format( "An error occured but we are unable to handle the request.ContentType [{0}]. Anyways, the error is: {1}", httpApplication.Request.ContentType, currentError.Message); var errorController = new FakeErrorController(); var controllerContext = new ControllerContext(httpApplication.Context.Request.RequestContext, errorController); var jsonResult = new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet, Data = new { error_message = errorMessage } }; jsonResult.ExecuteResult(controllerContext); // Lets make sure we set the correct Error Status code :) httpApplication.Response.StatusCode = (int)httpStatusCode; }
protected void Application_Error() { var ex = Server.GetLastError(); _logger.Error(ex); HttpContext httpContext = HttpContext.Current; if (httpContext != null) { RequestContext requestContext = ((MvcHandler)httpContext.CurrentHandler).RequestContext; if (requestContext.HttpContext.Request.IsAjaxRequest()) { httpContext.Response.Clear(); string controllerName = requestContext.RouteData.GetRequiredString("controller"); IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory(); IController controller = factory.CreateController(requestContext, controllerName); ControllerContext controllerContext = new ControllerContext(requestContext, (ControllerBase)controller); JsonResult jsonResult = new JsonResult(); jsonResult.Data = new { success = false, serverError = "500" }; jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet; jsonResult.ExecuteResult(controllerContext); httpContext.Response.End(); } else { httpContext.Response.Redirect("~/Error"); } } }
public void EmptyContentTypeRendersDefault() { // Arrange object data = _jsonData; Encoding contentEncoding = Encoding.UTF8; // Arrange expectations Mock <ControllerContext> mockControllerContext = new Mock <ControllerContext>(MockBehavior.Strict); mockControllerContext.ExpectGet(c => c.HttpContext.Request.HttpMethod).Returns("POST").Verifiable(); mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentType, "application/json").Verifiable(); mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentEncoding, contentEncoding).Verifiable(); mockControllerContext.Expect(c => c.HttpContext.Response.Write(_jsonSerializedData)).Verifiable(); JsonResult result = new JsonResult { Data = data, ContentType = String.Empty, ContentEncoding = contentEncoding }; // Act result.ExecuteResult(mockControllerContext.Object); // Assert mockControllerContext.Verify(); }
public void RecursionLimitIsPassedToSerilizer() { // Arrange Tuple <string, Tuple <string, Tuple <string, string> > > data = new Tuple <string, Tuple <string, Tuple <string, string> > >("key1", new Tuple <string, Tuple <string, string> >("key2", new Tuple <string, string>("key3", "value") ) ); string jsonData = "{\"Item1\":\"key1\",\"Item2\":{\"Item1\":\"key2\",\"Item2\":{\"Item1\":\"key3\",\"Item2\":\"value\"}}}"; Mock <ControllerContext> mockControllerContext = new Mock <ControllerContext>(); mockControllerContext.SetupGet(c => c.HttpContext.Request.HttpMethod).Returns("POST").Verifiable(); mockControllerContext.SetupSet(c => c.HttpContext.Response.ContentType = "application/json").Verifiable(); mockControllerContext.Setup(c => c.HttpContext.Response.Write(jsonData)).Verifiable(); JsonResult result = new JsonResult { Data = data, RecursionLimit = 2 }; // Act & Assert Assert.Throws <ArgumentException>( () => result.ExecuteResult(mockControllerContext.Object), "RecursionLimit exceeded."); }
protected void Application_Error() { var exception = Server.GetLastError(); Context.Response.Clear(); Context.ClearError(); var httpException = exception as HttpException ?? new HttpException((Int32)HttpStatusCode.InternalServerError, "Internal Server Error", exception); var httpStatusCode = httpException.GetHttpCode(); HttpContext httpContext = HttpContext.Current; if (httpContext != null) { RequestContext requestContext = ((MvcHandler)httpContext.CurrentHandler).RequestContext; /* When the request is ajax the system can automatically handle a mistake with a JSON response. * Then overwrites the default response */ if (requestContext.HttpContext.Request.IsAjaxRequest()) { httpContext.Response.Clear(); string controllerName = requestContext.RouteData.GetRequiredString("controller"); IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory(); IController controller = factory.CreateController(requestContext, controllerName); ControllerContext controllerContext = new ControllerContext(requestContext, (ControllerBase)controller); JsonResult jsonResult = new JsonResult { Data = new { success = false, serverError = "500" }, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; jsonResult.ExecuteResult(controllerContext); httpContext.Response.End(); } else { var routeData = new RouteData(); routeData.Values["controller"] = "Error"; //routeData.Values["action"] = "generalerror"; //routeData.Values.Add("url", Context.Request.Url.OriginalString); if (httpException != null) { switch ((HttpStatusCode)httpStatusCode) { case HttpStatusCode.Forbidden: case HttpStatusCode.NotFound: case HttpStatusCode.InternalServerError: routeData.Values.Add("action", $"Error{httpStatusCode}"); break; default: routeData.Values.Add("action", "Index"); break; } } Server.ClearError(); IController controller = new ErrorController(); controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); } } }
public override void ExecuteResult(ControllerContext context) { JsonResult innerAction = new JsonResult { Data = this.Data }; innerAction.ExecuteResult(context); }
/// <inheritdoc /> public override void ExecuteResult(ActionContext context) { var jsonResult = new JsonResult(this) { StatusCode = (int)_statusCode }; jsonResult.ExecuteResult(context); }
public override void Execute(object model, ControllerContext context) { context.HttpContext = new HttpContextWrapper(HttpContext.Current); var result = new JsonResult { Data = model }; result.ExecuteResult(context); }
private void OutputJson(object data, ControllerContext context) { var jr = new JsonResult { Data = data, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; jr.ExecuteResult(context); }
public override void ExecuteResult(ControllerContext context) { var result = new JsonResult { Data = Data, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; result.ExecuteResult(context); }
public override void ExecuteResult(ControllerContext context) { var jsonResponse = new JsonResult { Data = this, ContentType = "application/json", JsonRequestBehavior = JsonRequestBehavior.AllowGet }; jsonResponse.ExecuteResult(context); }
public override void ExecuteResult(ControllerContext context) { JsonResult jsonResult = new JsonResult() { Data = this, JsonRequestBehavior = JsonRequestBehavior.AllowGet, }; context.HttpContext.Response.StatusCode = StatusCode; jsonResult.ExecuteResult(context); }
private void HandleExceptionInAjaxContext(Exception exception) { Response.ContentType = "application/json"; Response.StatusCode = 400; JsonResult json = new JsonResult { Data = exception.Message }; json.ExecuteResult(new ControllerContext(Request.RequestContext, new BaseController())); }
public override void ExecuteResult(ControllerContext context) { if (context.HttpContext.Request.IsAjaxRequest()) { _jsonResult.ExecuteResult(context); } else { _partialViewResult.ExecuteResult(context); } }
protected virtual void ExecuteAjaxResult(ControllerContext context) { var httpContext = context.HttpContext; httpContext.Response.Clear(); httpContext.Response.TrySkipIisCustomErrors = true; httpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError; var jsonData = AjaxResponse.Fail(m_Message); var result = new JsonResult { Data = jsonData, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; result.ExecuteResult(context); }
protected void Application_Error() { var ex = Server.GetLastError(); if (ex != null) { App.Logger.Error(ex); } HttpContext httpContext = HttpContext.Current; if (httpContext != null) { RequestContext requestContext = ((MvcHandler)httpContext.CurrentHandler).RequestContext; /* when the request is ajax the system can automatically handle a mistake with a JSON response. then overwrites the default response */ if (requestContext.HttpContext.Request.IsAjaxRequest()) { httpContext.Response.Clear(); string controllerName = requestContext.RouteData.GetRequiredString("controller"); IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory(); IController controller = factory.CreateController(requestContext, controllerName); ControllerContext controllerContext = new ControllerContext(requestContext, (ControllerBase)controller); JsonResult jsonResult = new JsonResult(); jsonResult.Data = new { success = false, serverError = "500" }; jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet; jsonResult.ExecuteResult(controllerContext); httpContext.Response.End(); } else { Exception exception = Server.GetLastError(); Server.ClearError(); Response.TrySkipIisCustomErrors = true; // RouteValues erstellen um danach auszufuehren RouteData routeData = new RouteData(); routeData.Values.Add("controller", "Error"); routeData.Values.Add("action", "Index"); routeData.Values.Add("exception", exception); // Error-Seite aufrufen IController errorController = new ErrorController(); errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); } } }
public override void ExecuteResult(ControllerContext context) { var result = new JsonResult { Data = new { Completed = this.Completed, Message = this.Message, Data = this.Data } }; result.JsonRequestBehavior = this.AllowGet ? JsonRequestBehavior.AllowGet : JsonRequestBehavior.DenyGet; result.ExecuteResult(context); }
public override void ExecuteResult(ControllerContext context) { var response = context.HttpContext.Response; response.StatusCode = 400; var jsonResult = new JsonResult { Data = new { Errors = _validationErrors }, ContentType = null, ContentEncoding = null, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; jsonResult.ExecuteResult(context); }
public void EmptyContentTypeRendersDefault() { // Arrange object data = _jsonData; Encoding contentEncoding = Encoding.UTF8; // Arrange expectations Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(MockBehavior.Strict); mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentType, "application/json").Verifiable(); mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentEncoding, contentEncoding).Verifiable(); mockControllerContext.Expect(c => c.HttpContext.Response.Write(_jsonSerializedData)).Verifiable(); JsonResult result = new JsonResult { Data = data, ContentType = String.Empty, ContentEncoding = contentEncoding }; // Act result.ExecuteResult(mockControllerContext.Object); // Assert mockControllerContext.Verify(); }
public void NullContentIsNotOutput() { // Arrange string contentType = "Some content type."; Encoding contentEncoding = Encoding.UTF8; // Arrange expectations Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(); mockControllerContext.SetupGet(c => c.HttpContext.Request.HttpMethod).Returns("POST").Verifiable(); mockControllerContext.SetupSet(c => c.HttpContext.Response.ContentType = contentType).Verifiable(); mockControllerContext.SetupSet(c => c.HttpContext.Response.ContentEncoding = contentEncoding).Verifiable(); JsonResult result = new JsonResult { ContentType = contentType, ContentEncoding = contentEncoding }; // Act result.ExecuteResult(mockControllerContext.Object); // Assert mockControllerContext.Verify(); }
public void GetRequestBlocked() { // Arrange expectations Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(MockBehavior.Strict); mockControllerContext.SetupGet(c => c.HttpContext.Request.HttpMethod).Returns("GET").Verifiable(); JsonResult result = new JsonResult(); // Act & Assert Assert.Throws<InvalidOperationException>( () => result.ExecuteResult(mockControllerContext.Object), "This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet."); mockControllerContext.Verify(); }
public void NullRecursionLimitDefaultIsUsed() { // Arrange Tuple<string, Tuple<string, Tuple<string, string>>> data = new Tuple<string, Tuple<string, Tuple<string, string>>>("key1", new Tuple<string, Tuple<string, string>>("key2", new Tuple<string, string>("key3", "value") ) ); string jsonData = "{\"Item1\":\"key1\",\"Item2\":{\"Item1\":\"key2\",\"Item2\":{\"Item1\":\"key3\",\"Item2\":\"value\"}}}"; Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(); mockControllerContext.SetupGet(c => c.HttpContext.Request.HttpMethod).Returns("POST").Verifiable(); mockControllerContext.SetupSet(c => c.HttpContext.Response.ContentType = "application/json").Verifiable(); mockControllerContext.Setup(c => c.HttpContext.Response.Write(jsonData)).Verifiable(); JsonResult result = new JsonResult { Data = data }; // Act result.ExecuteResult(mockControllerContext.Object); // Assert mockControllerContext.Verify(); }
public void RecursionLimitIsPassedToSerilizer() { // Arrange Tuple<string, Tuple<string, Tuple<string, string>>> data = new Tuple<string, Tuple<string, Tuple<string, string>>>("key1", new Tuple<string, Tuple<string, string>>("key2", new Tuple<string, string>("key3", "value") ) ); string jsonData = "{\"Item1\":\"key1\",\"Item2\":{\"Item1\":\"key2\",\"Item2\":{\"Item1\":\"key3\",\"Item2\":\"value\"}}}"; Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(); mockControllerContext.SetupGet(c => c.HttpContext.Request.HttpMethod).Returns("POST").Verifiable(); mockControllerContext.SetupSet(c => c.HttpContext.Response.ContentType = "application/json").Verifiable(); mockControllerContext.Setup(c => c.HttpContext.Response.Write(jsonData)).Verifiable(); JsonResult result = new JsonResult { Data = data, RecursionLimit = 2 }; // Act & Assert Assert.Throws<ArgumentException>( () => result.ExecuteResult(mockControllerContext.Object), "RecursionLimit exceeded."); }
public void MaxJsonLengthIsPassedToSerializer() { // Arrange string data = new String('1', 2100000); string jsonData = "\"" + data + "\""; Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(); mockControllerContext.SetupGet(c => c.HttpContext.Request.HttpMethod).Returns("POST").Verifiable(); mockControllerContext.SetupSet(c => c.HttpContext.Response.ContentType = "application/json").Verifiable(); mockControllerContext.Setup(c => c.HttpContext.Response.Write(jsonData)).Verifiable(); JsonResult result = new JsonResult { Data = data, MaxJsonLength = 2200000 }; // Act result.ExecuteResult(mockControllerContext.Object); // Assert mockControllerContext.Verify(); }
public void NullMaxJsonLengthDefaultIsUsed() { // Arrange string data = new String('1', 2100000); Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(); mockControllerContext.SetupGet(c => c.HttpContext.Request.HttpMethod).Returns("POST").Verifiable(); mockControllerContext.SetupSet(c => c.HttpContext.Response.ContentType = "application/json").Verifiable(); JsonResult result = new JsonResult { Data = data }; // Act & Assert Assert.Throws<InvalidOperationException>( () => result.ExecuteResult(mockControllerContext.Object), "Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property."); }
public void NullContentIsNotOutput() { // Arrange string contentType = "Some content type."; Encoding contentEncoding = Encoding.UTF8; // Arrange expectations Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(MockBehavior.Strict); mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentType, contentType).Verifiable(); mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentEncoding, contentEncoding).Verifiable(); JsonResult result = new JsonResult { ContentType = contentType, ContentEncoding = contentEncoding }; // Act result.ExecuteResult(mockControllerContext.Object); // Assert mockControllerContext.Verify(); }
public void NullContentEncodingIsNotOutput() { // Arrange object data = _jsonData; string contentType = "Some content type."; // Arrange expectations Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(MockBehavior.Strict); mockControllerContext.ExpectSet(c => c.HttpContext.Response.ContentType, contentType).Verifiable(); mockControllerContext.Expect(c => c.HttpContext.Response.Write(_jsonSerializedData)).Verifiable(); JsonResult result = new JsonResult { Data = data, ContentType = contentType, }; // Act result.ExecuteResult(mockControllerContext.Object); // Assert mockControllerContext.Verify(); }
public void ExecuteResult() { // Arrange object data = _jsonData; string contentType = "Some content type."; Encoding contentEncoding = Encoding.UTF8; // Arrange expectations Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>(MockBehavior.Strict); mockControllerContext.SetupGet(c => c.HttpContext.Request.HttpMethod).Returns("POST").Verifiable(); mockControllerContext.SetupSet(c => c.HttpContext.Response.ContentType = contentType).Verifiable(); mockControllerContext.SetupSet(c => c.HttpContext.Response.ContentEncoding = contentEncoding).Verifiable(); mockControllerContext.Setup(c => c.HttpContext.Response.Write(_jsonSerializedData)).Verifiable(); JsonResult result = new JsonResult { Data = data, ContentType = contentType, ContentEncoding = contentEncoding }; // Act result.ExecuteResult(mockControllerContext.Object); // Assert mockControllerContext.Verify(); }