Skip to content

Commit

Permalink
groen
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Dec 27, 2023
1 parent 3c63a8d commit 0679f58
Show file tree
Hide file tree
Showing 17 changed files with 264 additions and 104 deletions.
19 changes: 18 additions & 1 deletion src/WireMock.Net.Abstractions/Admin/Mappings/ResponseModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ResponseModel
public bool? BodyAsJsonIndented { get; set; }

/// <summary>
/// Gets or sets the body (as bytearray).
/// Gets or sets the body (as byte array).
/// </summary>
public byte[]? BodyAsBytes { get; set; }

Expand Down Expand Up @@ -123,4 +123,21 @@ public class ResponseModel
/// Gets or sets the WebProxy settings.
/// </summary>
public WebProxyModel? WebProxy { get; set; }

#region ProtoBuf
/// <summary>
/// Gets or sets the proto definition.
/// </summary>
public string? ProtoDefinition { get; set; }

/// <summary>
/// Gets or sets the full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".
/// </summary>
public string? ProtoBufMessageType { get; set; }

/// <summary>
/// Gets or sets the body as JSON object (which defines a ProtoBuf message byte array).
/// </summary>
public object? BodyAsProtoBufJson { get; set; }
#endregion
}
19 changes: 18 additions & 1 deletion src/WireMock.Net.Abstractions/Models/IBodyData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace WireMock.Util;
public interface IBodyData
{
/// <summary>
/// The body (as bytearray).
/// The body (as byte array).
/// </summary>
byte[]? BodyAsBytes { get; set; }

Expand Down Expand Up @@ -68,4 +68,21 @@ public interface IBodyData
/// Defines if this BodyData is the result of a dynamically created response-string. (
/// </summary>
public string? IsFuncUsed { get; set; }

#region ProtoBuf
/// <summary>
/// Gets or sets the proto definition.
/// </summary>
public string? ProtoDefinition { get; set; }

/// <summary>
/// Gets or sets the full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".
/// </summary>
public string? ProtoBufMessageType { get; set; }

/// <summary>
/// Gets or sets the body as JSON object (which defines a ProtoBuf message byte array).
/// </summary>
public object? BodyAsProtoBufJson { get; set; }
#endregion
}
7 changes: 6 additions & 1 deletion src/WireMock.Net.Abstractions/Types/BodyType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,10 @@ public enum BodyType
/// <summary>
/// Body is a String which is x-www-form-urlencoded.
/// </summary>
FormUrlEncoded
FormUrlEncoded,

/// <summary>
/// Body is a ProtoBuf Byte array
/// </summary>
ProtoBuf
}
8 changes: 4 additions & 4 deletions src/WireMock.Net/Matchers/ProtoBufMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace WireMock.Matchers;
/// <summary>
/// Grpc ProtoBuf Matcher
/// </summary>
/// <inheritdoc cref="IObjectMatcher"/>
/// <inheritdoc cref="IBytesMatcher"/>
public class ProtoBufMatcher : IBytesMatcher
{
/// <inheritdoc />
Expand All @@ -20,7 +20,7 @@ public class ProtoBufMatcher : IBytesMatcher
public MatchBehaviour MatchBehaviour { get; }

/// <summary>
/// The proto definition as a string.
/// The proto definition.
/// </summary>
public string ProtoDefinition { get; }

Expand All @@ -37,9 +37,9 @@ public class ProtoBufMatcher : IBytesMatcher
private static readonly IConverter ProtoBufToJsonConverter = SingletonFactory<Converter>.GetInstance();

/// <summary>
/// Initializes a new instance of the <see cref="GraphQLMatcher"/> class.
/// Initializes a new instance of the <see cref="ProtoBufMatcher"/> class.
/// </summary>
/// <param name="protoDefinition">The proto definition as a string.</param>
/// <param name="protoDefinition">The proto definition.</param>
/// <param name="messageType">The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".</param>
/// <param name="matchBehaviour">The match behaviour. (default = "AcceptOnMatch")</param>
/// <param name="jsonMatcher">The optional jsonMatcher to use to match the ProtoBuf as (json) object.</param>
Expand Down
32 changes: 22 additions & 10 deletions src/WireMock.Net/Models/BodyData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace WireMock.Util;
/// </summary>
public class BodyData : IBodyData
{
/// <inheritdoc cref="IBodyData.Encoding" />
/// <inheritdoc />
public Encoding? Encoding { get; set; }

/// <inheritdoc />
Expand All @@ -18,30 +18,42 @@ public class BodyData : IBodyData
/// <inheritdoc />
public IDictionary<string, string>? BodyAsFormUrlEncoded { get; set; }

/// <inheritdoc cref="IBodyData.BodyAsJson" />
/// <inheritdoc />
public object? BodyAsJson { get; set; }

/// <inheritdoc cref="IBodyData.BodyAsBytes" />
/// <inheritdoc />
public byte[]? BodyAsBytes { get; set; }

/// <inheritdoc cref="IBodyData.BodyAsJsonIndented" />
/// <inheritdoc />
public bool? BodyAsJsonIndented { get; set; }

/// <inheritdoc cref="IBodyData.BodyAsFile" />
/// <inheritdoc />
public string? BodyAsFile { get; set; }

/// <inheritdoc cref="IBodyData.BodyAsFileIsCached" />
/// <inheritdoc />
public bool? BodyAsFileIsCached { get; set; }

/// <inheritdoc cref="IBodyData.DetectedBodyType" />
/// <inheritdoc />
public BodyType? DetectedBodyType { get; set; }

/// <inheritdoc cref="IBodyData.DetectedBodyTypeFromContentType" />
/// <inheritdoc />
public BodyType? DetectedBodyTypeFromContentType { get; set; }

/// <inheritdoc cref="IRequestMessage.DetectedCompression" />
/// <inheritdoc />
public string? DetectedCompression { get; set; }

/// <inheritdoc />
public string? IsFuncUsed { get; set; }

#region ProtoBuf
/// <inheritdoc />
public string? ProtoDefinition { get; set; }

/// <inheritdoc />
public string? ProtoBufMessageType { get; set; }

/// <inheritdoc />
public object? BodyAsProtoBufJson { get; set; }

#endregion
}
3 changes: 3 additions & 0 deletions src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ private bool IsFault(IResponseMessage responseMessage)
var jsonBody = JsonConvert.SerializeObject(responseMessage.BodyData.BodyAsJson, new JsonSerializerSettings { Formatting = formatting, NullValueHandling = NullValueHandling.Ignore });
return (responseMessage.BodyData.Encoding ?? _utf8NoBom).GetBytes(jsonBody);

case BodyType.ProtoBuf:
return responseMessage.BodyData.BodyAsBytes;

case BodyType.Bytes:
return responseMessage.BodyData.BodyAsBytes;

Expand Down
4 changes: 2 additions & 2 deletions src/WireMock.Net/RequestBuilders/IProtoBufRequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface IProtoBufRequestBuilder : IGraphQLRequestBuilder
/// <param name="messageType">The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".</param>
/// <param name="matchBehaviour">The match behaviour. (default = "AcceptOnMatch")</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithGrpcProto(string protoDefinition, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
IRequestBuilder WithBodyAsProtoBuf(string protoDefinition, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);

/// <summary>
/// WithGrpcProto
Expand All @@ -24,5 +24,5 @@ public interface IProtoBufRequestBuilder : IGraphQLRequestBuilder
/// <param name="jsonMatcher">The jsonMatcher to use to match the ProtoBuf as (json) object.</param>
/// <param name="matchBehaviour">The match behaviour. (default = "AcceptOnMatch")</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithGrpcProto(string protoDefinition, string messageType, IJsonMatcher jsonMatcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
IRequestBuilder WithBodyAsProtoBuf(string protoDefinition, string messageType, IJsonMatcher jsonMatcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
}
4 changes: 2 additions & 2 deletions src/WireMock.Net/RequestBuilders/Request.WithGrpcProto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ namespace WireMock.RequestBuilders;
public partial class Request
{
/// <inheritdoc />
public IRequestBuilder WithGrpcProto(string protoDefinition, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
public IRequestBuilder WithBodyAsProtoBuf(string protoDefinition, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
{
_requestMatchers.Add(new RequestMessageProtoBufMatcher(matchBehaviour, protoDefinition, messageType));
return this;
}

/// <inheritdoc />
public IRequestBuilder WithGrpcProto(string protoDefinition, string messageType, IJsonMatcher jsonMatcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
public IRequestBuilder WithBodyAsProtoBuf(string protoDefinition, string messageType, IJsonMatcher jsonMatcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
{
_requestMatchers.Add(new RequestMessageProtoBufMatcher(matchBehaviour, protoDefinition, messageType, jsonMatcher));
return this;
Expand Down
7 changes: 5 additions & 2 deletions src/WireMock.Net/ResponseBuilders/Response.WithBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,11 @@ public IResponseBuilder WithBodyAsProtoBuf(
ResponseMessage.BodyDestination = null;
ResponseMessage.BodyData = new BodyData
{
DetectedBodyType = BodyType.Bytes,
BodyAsBytes = bytes
DetectedBodyType = BodyType.ProtoBuf,
BodyAsBytes = bytes,
BodyAsProtoBufJson = value,
ProtoDefinition = protoDefinition,
ProtoBufMessageType = messageType
};
#endif
return this;
Expand Down
88 changes: 51 additions & 37 deletions src/WireMock.Net/Serialization/MappingConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,43 +419,7 @@ public MappingModel ToMappingModel(IMapping mapping)
mappingModel.Response.UseTransformerForBodyAsFile = response.UseTransformerForBodyAsFile;
}

if (response.ResponseMessage.BodyData != null)
{
switch (response.ResponseMessage.BodyData?.DetectedBodyType)
{
case BodyType.String:
case BodyType.FormUrlEncoded:
mappingModel.Response.Body = response.ResponseMessage.BodyData.BodyAsString;
break;

case BodyType.Json:
mappingModel.Response.BodyAsJson = response.ResponseMessage.BodyData.BodyAsJson;
if (response.ResponseMessage.BodyData.BodyAsJsonIndented == true)
{
mappingModel.Response.BodyAsJsonIndented = response.ResponseMessage.BodyData.BodyAsJsonIndented;
}
break;

case BodyType.Bytes:
mappingModel.Response.BodyAsBytes = response.ResponseMessage.BodyData.BodyAsBytes;
break;

case BodyType.File:
mappingModel.Response.BodyAsFile = response.ResponseMessage.BodyData.BodyAsFile;
mappingModel.Response.BodyAsFileIsCached = response.ResponseMessage.BodyData.BodyAsFileIsCached;
break;
}

if (response.ResponseMessage.BodyData?.Encoding != null && response.ResponseMessage.BodyData.Encoding.WebName != "utf-8")
{
mappingModel.Response.BodyEncoding = new EncodingModel
{
EncodingName = response.ResponseMessage.BodyData.Encoding.EncodingName,
CodePage = response.ResponseMessage.BodyData.Encoding.CodePage,
WebName = response.ResponseMessage.BodyData.Encoding.WebName
};
}
}
MapResponse(response, mappingModel);

if (response.ResponseMessage.FaultType != FaultType.NONE)
{
Expand All @@ -470,6 +434,56 @@ public MappingModel ToMappingModel(IMapping mapping)
return mappingModel;
}

private static void MapResponse(Response response, MappingModel mappingModel)
{
if (response.ResponseMessage.BodyData == null)
{
return;
}

switch (response.ResponseMessage.BodyData?.DetectedBodyType)
{
case BodyType.String:
case BodyType.FormUrlEncoded:
mappingModel.Response.Body = response.ResponseMessage.BodyData.BodyAsString;
break;

case BodyType.Json:
mappingModel.Response.BodyAsJson = response.ResponseMessage.BodyData.BodyAsJson;
if (response.ResponseMessage.BodyData.BodyAsJsonIndented == true)
{
mappingModel.Response.BodyAsJsonIndented = response.ResponseMessage.BodyData.BodyAsJsonIndented;
}
break;

case BodyType.ProtoBuf:
mappingModel.Response.ProtoDefinition = response.ResponseMessage.BodyData.ProtoDefinition;
mappingModel.Response.ProtoBufMessageType = response.ResponseMessage.BodyData.ProtoBufMessageType;
mappingModel.Response.BodyAsBytes = null;
mappingModel.Response.BodyAsProtoBufJson = response.ResponseMessage.BodyData.BodyAsProtoBufJson;
break;

case BodyType.Bytes:
mappingModel.Response.BodyAsBytes = response.ResponseMessage.BodyData.BodyAsBytes;
break;

case BodyType.File:
mappingModel.Response.BodyAsFile = response.ResponseMessage.BodyData.BodyAsFile;
mappingModel.Response.BodyAsFileIsCached = response.ResponseMessage.BodyData.BodyAsFileIsCached;
break;
}

if (response.ResponseMessage.BodyData?.Encoding != null && response.ResponseMessage.BodyData.Encoding.WebName != "utf-8")
{
mappingModel.Response.BodyEncoding = new EncodingModel
{
EncodingName = response.ResponseMessage.BodyData.Encoding.EncodingName,
CodePage = response.ResponseMessage.BodyData.Encoding.CodePage,
WebName = response.ResponseMessage.BodyData.Encoding.WebName
};
}
}

private static string GetString(IStringMatcher stringMatcher)
{
return stringMatcher.GetPatterns().Select(p => ToCSharpStringLiteral(p.GetPattern())).First();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ message HelloReply {
public void RequestBuilder_WithGrpcProto_Without_JsonMatcher()
{
// Act
var requestBuilder = (Request)Request.Create().WithGrpcProto(TestProtoDefinition, MessageType);
var requestBuilder = (Request)Request.Create().WithBodyAsProtoBuf(TestProtoDefinition, MessageType);

// Assert
var matchers = requestBuilder.GetPrivateFieldValue<IList<IRequestMatcher>>("_requestMatchers");
Expand All @@ -50,7 +50,7 @@ public void RequestBuilder_WithGrpcProto_With_JsonMatcher()
{
// Act
var jsonMatcher = new JsonMatcher(new { name = "stef" });
var requestBuilder = (Request)Request.Create().WithGrpcProto(TestProtoDefinition, MessageType, jsonMatcher);
var requestBuilder = (Request)Request.Create().WithBodyAsProtoBuf(TestProtoDefinition, MessageType, jsonMatcher);

// Assert
var matchers = requestBuilder.GetPrivateFieldValue<IList<IRequestMatcher>>("_requestMatchers");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
UpdatedAt: DateTime_1,
Title: ,
Description: ,
Priority: 42,
Priority: 41,
Request: {
Path: {
Matchers: [
Expand Down Expand Up @@ -48,8 +48,6 @@ message HelloReply {
}
}
},
Response: {
BodyAsBytes: CgVoZWxsbw==
},
Response: {},
UseWebhooksFireAndForget: false
}
Loading

0 comments on commit 0679f58

Please sign in to comment.