public static Response AsCreated(this IResponseFormatter formatter, string route, Guid id) { var jsonResult = formatter.AsJson(new { id }); jsonResult.StatusCode = HttpStatusCode.Created; jsonResult.Headers["Location"] = string.Format(route, id); return jsonResult; }
public static Response AsJsonErrorOnValidation(this IResponseFormatter responseFormatter, IEnumerable<ModelValidationError> errors) { var actionResult = new ActionResult(false); foreach (var error in errors) { foreach (var member in error.MemberNames) { actionResult.Errors.Add(error.GetMessage(member)); } } return responseFormatter.AsJson(actionResult); }
public static Response AsValidationError(this IResponseFormatter response, ModelValidationResult result) { var model = new { Errors = new List<string>() }; foreach (var error in result.Errors) { foreach (var member in error.MemberNames) { model.Errors.Add(error.GetMessage(member)); } } return response.AsJson(model) .WithStatusCode(400); }
public static Stream CreateBody(this FormDataSet formDataSet, String enctype, Encoding encoding) { if (enctype.Isi(MimeTypeNames.UrlencodedForm)) { return formDataSet.AsUrlEncoded(encoding); } else if (enctype.Isi(MimeTypeNames.MultipartForm)) { return formDataSet.AsMultipart(encoding); } else if (enctype.Isi(MimeTypeNames.Plain)) { return formDataSet.AsPlaintext(encoding); } else if (enctype.Isi(MimeTypeNames.ApplicationJson)) { return formDataSet.AsJson(); } return MemoryStream.Null; }
/// <summary> /// Asserts that the response content type is of the MIME-type 'application/json' or the specified override. /// </summary> /// <param name="response">The <see cref="ClientResponse"/> that the assert should be made on.</param> /// <param name="assertions">Additional assertions on the JSON object.</param> /// <param name="contentType">The expected content type.</param> /// <returns>The JSON object.</returns> public static dynamic ShouldBeJson(this ClientResponse response, Action<dynamic> assertions = null, string contentType = "application/json") { response.ShouldHaveContentType(contentType); dynamic json; try { json = response.AsJson(); } catch (Exception exception) { throw AssertException.Create(response, "Failed to convert response body into JSON.", exception); } if (assertions != null) { assertions(json); } return json; }
/// <summary> /// Asserts that the response content type is of the MIME-type 'application/json' or the specified override. /// </summary> /// <param name="response">The <see cref="BrowserResponse"/> that the assert should be made on.</param> /// <param name="assertions">Additional assertions on the JSON object.</param> /// <param name="contentType">The expected content type.</param> /// <returns>The JSON object.</returns> public static dynamic ShouldBeJson(this BrowserResponse response, Action<dynamic> assertions = null, string contentType = "application/json") { response.AssertStatusCode(HttpStatusCode.OK); response.AssertContentType(contentType); dynamic json; try { json = response.AsJson(); } catch (Exception exception) { throw new AssertException("Failed to convert response body into JSON.", exception); } if (assertions != null) { assertions(json); } return json; }
public static Response Error(this IResponseFormatter response, string error) { return response.AsJson(new { error = error }, HttpStatusCode.BadRequest); }