mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-18 08:07:09 +01:00
BodyAsJsonIndented (#113)
This commit is contained in:
@@ -160,18 +160,17 @@ namespace WireMock.Net.ConsoleApplication
|
||||
.RespondWith(Response.Create().WithStatusCode(200).WithBody("partial = 200"));
|
||||
|
||||
// http://localhost:8080/any/any?start=1000&stop=1&stop=2
|
||||
//server
|
||||
// .Given(Request.Create().WithPath("/*").UsingGet())
|
||||
// .WithGuid("90356dba-b36c-469a-a17e-669cd84f1f05")
|
||||
// .AtPriority(server.Mappings.Count() + 1)
|
||||
// .RespondWith(Response.Create()
|
||||
// .WithStatusCode(200)
|
||||
// .WithHeader("Content-Type", "application/json")
|
||||
// .WithHeader("Transformed-Postman-Token", "token is {{request.headers.Postman-Token}}")
|
||||
// .WithBody(@"{""msg"": ""Hello world CATCH-ALL on /*, {{request.path}}, bykey={{request.query.start}}, bykey={{request.query.stop}}, byidx0={{request.query.stop.[0]}}, byidx1={{request.query.stop.[1]}}"" }")
|
||||
// .WithTransformer()
|
||||
// .WithDelay(TimeSpan.FromMilliseconds(100))
|
||||
// );
|
||||
server
|
||||
.Given(Request.Create().WithPath("/trans").UsingGet())
|
||||
.WithGuid("90356dba-b36c-469a-a17e-669cd84f1f05")
|
||||
.RespondWith(Response.Create()
|
||||
.WithStatusCode(200)
|
||||
.WithHeader("Content-Type", "application/json")
|
||||
.WithHeader("Transformed-Postman-Token", "token is {{request.headers.Postman-Token}}")
|
||||
.WithBody(@"{""msg"": ""Hello world CATCH-ALL on /*, {{request.path}}, bykey={{request.query.start}}, bykey={{request.query.stop}}, byidx0={{request.query.stop.[0]}}, byidx1={{request.query.stop.[1]}}"" }")
|
||||
.WithTransformer()
|
||||
.WithDelay(TimeSpan.FromMilliseconds(100))
|
||||
);
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
|
||||
@@ -32,6 +32,11 @@ namespace WireMock.Admin.Mappings
|
||||
/// </summary>
|
||||
public object BodyAsJson { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether child objects to be indented according to the Newtonsoft.Json.JsonTextWriter.Indentation and Newtonsoft.Json.JsonTextWriter.IndentChar settings.
|
||||
/// </summary>
|
||||
public bool? BodyAsJsonIndented { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the body (as bytearray).
|
||||
/// </summary>
|
||||
|
||||
@@ -87,7 +87,8 @@ namespace WireMock.Owin
|
||||
|
||||
if (responseMessage.BodyAsJson != null)
|
||||
{
|
||||
string jsonBody = JsonConvert.SerializeObject(responseMessage.BodyAsJson, new JsonSerializerSettings { Formatting = Formatting.None, NullValueHandling = NullValueHandling.Ignore });
|
||||
Formatting formatting = responseMessage.BodyAsJsonIndented == true ? Formatting.Indented : Formatting.None;
|
||||
string jsonBody = JsonConvert.SerializeObject(responseMessage.BodyAsJson, new JsonSerializerSettings { Formatting = formatting, NullValueHandling = NullValueHandling.Ignore });
|
||||
using (var writer = new StreamWriter(response.Body, responseMessage.BodyEncoding ?? _utf8NoBom))
|
||||
{
|
||||
await writer.WriteAsync(jsonBody);
|
||||
|
||||
@@ -43,8 +43,17 @@ namespace WireMock.ResponseBuilders
|
||||
/// </summary>
|
||||
/// <param name="body">The body.</param>
|
||||
/// <param name="encoding">The body encoding.</param>
|
||||
/// <param name="indented">Use JSON indented.</param>
|
||||
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||
IResponseBuilder WithBodyAsJson([NotNull] object body, [CanBeNull] Encoding encoding = null);
|
||||
IResponseBuilder WithBodyAsJson([NotNull] object body, [CanBeNull] Encoding encoding = null, bool? indented = null);
|
||||
|
||||
/// <summary>
|
||||
/// WithBody : Create a string response based on a object (which will be converted to a JSON string).
|
||||
/// </summary>
|
||||
/// <param name="body">The body.</param>
|
||||
/// <param name="indented">Define whether child objects to be indented according to the Newtonsoft.Json.JsonTextWriter.Indentation and Newtonsoft.Json.JsonTextWriter.IndentChar settings.</param>
|
||||
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||
IResponseBuilder WithBodyAsJson([NotNull] object body, bool indented);
|
||||
|
||||
/// <summary>
|
||||
/// WithBody : Create a string response based on a Base64 string (which will be decoded to a normal string).
|
||||
|
||||
@@ -258,18 +258,25 @@ namespace WireMock.ResponseBuilders
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IBodyResponseBuilder.WithBodyAsJson"/>
|
||||
public IResponseBuilder WithBodyAsJson(object body, Encoding encoding = null)
|
||||
/// <inheritdoc cref="IBodyResponseBuilder.WithBodyAsJson(object, Encoding, bool)"/>
|
||||
public IResponseBuilder WithBodyAsJson(object body, Encoding encoding = null, bool? indented = null)
|
||||
{
|
||||
Check.NotNull(body, nameof(body));
|
||||
|
||||
ResponseMessage.BodyDestination = null;
|
||||
ResponseMessage.BodyAsJson = body;
|
||||
ResponseMessage.BodyEncoding = encoding;
|
||||
ResponseMessage.BodyAsJsonIndented = indented;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IBodyResponseBuilder.WithBodyAsJson(object, bool)"/>
|
||||
public IResponseBuilder WithBodyAsJson(object body, bool indented)
|
||||
{
|
||||
return WithBodyAsJson(body, null, indented);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IBodyResponseBuilder.WithBodyFromBase64"/>
|
||||
public IResponseBuilder WithBodyFromBase64(string bodyAsbase64, Encoding encoding = null)
|
||||
{
|
||||
|
||||
@@ -42,6 +42,11 @@ namespace WireMock
|
||||
/// </summary>
|
||||
public object BodyAsJson { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether child objects to be indented according to the Newtonsoft.Json.JsonTextWriter.Indentation and Newtonsoft.Json.JsonTextWriter.IndentChar settings.
|
||||
/// </summary>
|
||||
public bool? BodyAsJsonIndented { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the body as bytes.
|
||||
/// </summary>
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.RequestBuilders;
|
||||
@@ -96,6 +94,7 @@ namespace WireMock.Serialization
|
||||
mappingModel.Response.Headers = null;
|
||||
mappingModel.Response.BodyDestination = null;
|
||||
mappingModel.Response.BodyAsJson = null;
|
||||
mappingModel.Response.BodyAsJsonIndented = null;
|
||||
mappingModel.Response.Body = null;
|
||||
mappingModel.Response.BodyAsBytes = null;
|
||||
mappingModel.Response.BodyAsFile = null;
|
||||
@@ -110,6 +109,7 @@ namespace WireMock.Serialization
|
||||
mappingModel.Response.StatusCode = response.ResponseMessage.StatusCode;
|
||||
mappingModel.Response.Headers = Map(response.ResponseMessage.Headers);
|
||||
mappingModel.Response.BodyAsJson = response.ResponseMessage.BodyAsJson;
|
||||
mappingModel.Response.BodyAsJsonIndented = response.ResponseMessage.BodyAsJsonIndented;
|
||||
mappingModel.Response.Body = response.ResponseMessage.Body;
|
||||
mappingModel.Response.BodyAsBytes = response.ResponseMessage.BodyAsBytes;
|
||||
mappingModel.Response.BodyAsFile = response.ResponseMessage.BodyAsFile;
|
||||
@@ -152,9 +152,9 @@ namespace WireMock.Serialization
|
||||
// return funcs?.Select(Map).Where(x => x != null).ToArray();
|
||||
//}
|
||||
|
||||
private static string Map<T>([CanBeNull] Func<T, bool> func)
|
||||
{
|
||||
return func?.ToString();
|
||||
}
|
||||
//private static string Map<T>([CanBeNull] Func<T, bool> func)
|
||||
//{
|
||||
// return func?.ToString();
|
||||
//}
|
||||
}
|
||||
}
|
||||
@@ -266,17 +266,19 @@ namespace WireMock.Server
|
||||
|
||||
private ResponseMessage SettingsUpdate(RequestMessage requestMessage)
|
||||
{
|
||||
var settings = requestMessage.Body != null ? JsonConvert.DeserializeObject<SettingsModel>(requestMessage.Body) : ((JObject)requestMessage.BodyAsJson).ToObject<SettingsModel>();
|
||||
|
||||
if (settings.AllowPartialMapping != null)
|
||||
_options.AllowPartialMapping = settings.AllowPartialMapping.Value;
|
||||
|
||||
var settings = DeserializeObject<SettingsModel>(requestMessage);
|
||||
_options.MaxRequestLogCount = settings.MaxRequestLogCount;
|
||||
|
||||
_options.RequestLogExpirationDuration = settings.RequestLogExpirationDuration;
|
||||
|
||||
if (settings.AllowPartialMapping != null)
|
||||
{
|
||||
_options.AllowPartialMapping = settings.AllowPartialMapping.Value;
|
||||
}
|
||||
|
||||
if (settings.GlobalProcessingDelay != null)
|
||||
{
|
||||
_options.RequestProcessingDelay = TimeSpan.FromMilliseconds(settings.GlobalProcessingDelay.Value);
|
||||
}
|
||||
|
||||
return new ResponseMessage { Body = "Settings updated" };
|
||||
}
|
||||
@@ -303,7 +305,7 @@ namespace WireMock.Server
|
||||
{
|
||||
Guid guid = Guid.Parse(requestMessage.Path.TrimStart(AdminMappings.ToCharArray()));
|
||||
|
||||
MappingModel mappingModel = requestMessage.Body != null ? JsonConvert.DeserializeObject<MappingModel>(requestMessage.Body) : ((JObject)requestMessage.BodyAsJson).ToObject<MappingModel>();
|
||||
var mappingModel = DeserializeObject<MappingModel>(requestMessage);
|
||||
DeserializeAndAddOrUpdateMapping(mappingModel, guid);
|
||||
|
||||
return new ResponseMessage { Body = "Mapping added or updated" };
|
||||
@@ -371,7 +373,7 @@ namespace WireMock.Server
|
||||
{
|
||||
try
|
||||
{
|
||||
MappingModel mappingModel = requestMessage.Body != null ? JsonConvert.DeserializeObject<MappingModel>(requestMessage.Body) : ((JObject)requestMessage.BodyAsJson).ToObject<MappingModel>();
|
||||
var mappingModel = DeserializeObject<MappingModel>(requestMessage);
|
||||
DeserializeAndAddOrUpdateMapping(mappingModel);
|
||||
}
|
||||
catch (ArgumentException a)
|
||||
@@ -552,7 +554,7 @@ namespace WireMock.Server
|
||||
#region Requests/find
|
||||
private ResponseMessage RequestsFind(RequestMessage requestMessage)
|
||||
{
|
||||
var requestModel = requestMessage.Body != null ? JsonConvert.DeserializeObject<RequestModel>(requestMessage.Body) : ((JObject)requestMessage.BodyAsJson).ToObject<RequestModel>();
|
||||
var requestModel = DeserializeObject<RequestModel>(requestMessage);
|
||||
|
||||
var request = (Request)InitRequestBuilder(requestModel);
|
||||
|
||||
@@ -598,8 +600,7 @@ namespace WireMock.Server
|
||||
|
||||
if (requestModel.ClientIP != null)
|
||||
{
|
||||
string clientIP = requestModel.ClientIP as string;
|
||||
if (clientIP != null)
|
||||
if (requestModel.ClientIP is string clientIP)
|
||||
{
|
||||
requestBuilder = requestBuilder.WithClientIP(clientIP);
|
||||
}
|
||||
@@ -615,8 +616,7 @@ namespace WireMock.Server
|
||||
|
||||
if (requestModel.Path != null)
|
||||
{
|
||||
string path = requestModel.Path as string;
|
||||
if (path != null)
|
||||
if (requestModel.Path is string path)
|
||||
{
|
||||
requestBuilder = requestBuilder.WithPath(path);
|
||||
}
|
||||
@@ -632,8 +632,7 @@ namespace WireMock.Server
|
||||
|
||||
if (requestModel.Url != null)
|
||||
{
|
||||
string url = requestModel.Url as string;
|
||||
if (url != null)
|
||||
if (requestModel.Url is string url)
|
||||
{
|
||||
requestBuilder = requestBuilder.WithUrl(url);
|
||||
}
|
||||
@@ -739,7 +738,7 @@ namespace WireMock.Server
|
||||
}
|
||||
else if (responseModel.BodyAsJson != null)
|
||||
{
|
||||
responseBuilder = responseBuilder.WithBodyAsJson(responseModel.BodyAsJson, ToEncoding(responseModel.BodyEncoding));
|
||||
responseBuilder = responseBuilder.WithBodyAsJson(responseModel.BodyAsJson, ToEncoding(responseModel.BodyEncoding), responseModel.BodyAsJsonIndented == true);
|
||||
}
|
||||
else if (responseModel.BodyFromBase64 != null)
|
||||
{
|
||||
@@ -768,5 +767,12 @@ namespace WireMock.Server
|
||||
{
|
||||
return encodingModel != null ? Encoding.GetEncoding(encodingModel.CodePage) : null;
|
||||
}
|
||||
|
||||
private T DeserializeObject<T>(RequestMessage requestMessage)
|
||||
{
|
||||
return requestMessage.Body != null ?
|
||||
JsonConvert.DeserializeObject<T>(requestMessage.Body) :
|
||||
((JObject)requestMessage.BodyAsJson).ToObject<T>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,8 @@ namespace WireMock.Transformers
|
||||
var template = new { request = requestMessage };
|
||||
|
||||
// Body
|
||||
string body = bodyIsJson ? JsonConvert.SerializeObject(original.BodyAsJson) : original.Body;
|
||||
Formatting formatting = original.BodyAsJsonIndented == true ? Formatting.Indented : Formatting.None;
|
||||
string body = bodyIsJson ? JsonConvert.SerializeObject(original.BodyAsJson, formatting) : original.Body;
|
||||
if (body != null)
|
||||
{
|
||||
var templateBody = Handlebars.Compile(body);
|
||||
|
||||
@@ -232,6 +232,40 @@ namespace WireMock.Net.Tests
|
||||
Check.That(response).IsEqualTo("Hello world!");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FluentMockServer_Should_respond_to_request_BodyAsJson()
|
||||
{
|
||||
// Assign
|
||||
_server = FluentMockServer.Start();
|
||||
|
||||
_server
|
||||
.Given(Request.Create().UsingAnyVerb())
|
||||
.RespondWith(Response.Create().WithBodyAsJson(new { message = "Hello" }));
|
||||
|
||||
// Act
|
||||
var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0]);
|
||||
|
||||
// Assert
|
||||
Check.That(response).IsEqualTo("{\"message\":\"Hello\"}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FluentMockServer_Should_respond_to_request_BodyAsJson_Indented()
|
||||
{
|
||||
// Assign
|
||||
_server = FluentMockServer.Start();
|
||||
|
||||
_server
|
||||
.Given(Request.Create().UsingAnyVerb())
|
||||
.RespondWith(Response.Create().WithBodyAsJson(new { message = "Hello" }, true));
|
||||
|
||||
// Act
|
||||
var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0]);
|
||||
|
||||
// Assert
|
||||
Check.That(response).IsEqualTo("{\r\n \"message\": \"Hello\"\r\n}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FluentMockServer_Should_respond_to_request_bodyAsCallback()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user