Skip to content

Commit 1221d52

Browse files
authored
Add DeserializeFormUrl Encoded to the settings (#905)
* Add DeserializeFormUrlEncoded to Settings * EmptyArray<>.Value * .
1 parent 52d2109 commit 1221d52

20 files changed

+101
-187
lines changed

src/WireMock.Net.Abstractions/Admin/Settings/SettingsModel.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,49 @@ public class SettingsModel
3131
public int? MaxRequestLogCount { get; set; }
3232

3333
/// <summary>
34-
/// Allow a Body for all HTTP Methods. (default set to false).
34+
/// Allow a Body for all HTTP Methods. (default set to <c>false</c>).
3535
/// </summary>
3636
public bool? AllowBodyForAllHttpMethods { get; set; }
3737

3838
/// <summary>
39-
/// Handle all requests synchronously. (default set to false).
39+
/// Allow only a HttpStatus Code in the response which is defined. (default set to <c>false</c>).
40+
/// - false : also null, 0, empty or invalid HttpStatus codes are allowed.
41+
/// - true : only codes defined in <see cref="System.Net.HttpStatusCode"/> are allowed.
42+
/// </summary>
43+
public bool? AllowOnlyDefinedHttpStatusCodeInResponse { get; set; }
44+
45+
/// <summary>
46+
/// Set to true to disable Json deserialization when processing requests. (default set to <c>false</c>).
47+
/// </summary>
48+
public bool? DisableJsonBodyParsing { get; set; }
49+
50+
/// <summary>
51+
/// Disable support for GZip and Deflate request body decompression. (default set to <c>false</c>).
52+
/// </summary>
53+
public bool? DisableRequestBodyDecompressing { get; set; }
54+
55+
/// <summary>
56+
/// Set to true to disable FormUrlEncoded deserializing when processing requests. (default set to <c>false</c>).
57+
/// </summary>
58+
public bool? DisableDeserializeFormUrlEncoded { get; set; }
59+
60+
/// <summary>
61+
/// Handle all requests synchronously. (default set to <c>false</c>).
4062
/// </summary>
4163
public bool? HandleRequestsSynchronously { get; set; }
4264

4365
/// <summary>
44-
/// Throw an exception when the Matcher fails because of invalid input. (default set to false).
66+
/// Throw an exception when the Matcher fails because of invalid input. (default set to <c>false</c>).
4567
/// </summary>
4668
public bool? ThrowExceptionWhenMatcherFails { get; set; }
4769

4870
/// <summary>
49-
/// Use the RegexExtended instead of the default <see cref="Regex"/>. (default set to true).
71+
/// Use the RegexExtended instead of the default <see cref="Regex"/>. (default set to <c>true</c>).
5072
/// </summary>
5173
public bool? UseRegexExtended { get; set; }
5274

5375
/// <summary>
54-
/// Save unmatched requests to a file using the <see cref="IFileSystemHandler"/>. (default set to false).
76+
/// Save unmatched requests to a file using the <see cref="IFileSystemHandler"/>. (default set to <c>false</c>).
5577
/// </summary>
5678
public bool? SaveUnmatchedRequests { get; set; }
5779

@@ -86,7 +108,7 @@ public class SettingsModel
86108
public HostingScheme? HostingScheme { get; set; }
87109

88110
/// <summary>
89-
/// Don't save the response-string in the LogEntry when WithBody(Func{IRequestMessage, string}) or WithBody(Func{IRequestMessage, Task{string}}) is used. (default set to false).
111+
/// Don't save the response-string in the LogEntry when WithBody(Func{IRequestMessage, string}) or WithBody(Func{IRequestMessage, Task{string}}) is used. (default set to <c>false</c>).
90112
/// </summary>
91113
public bool? DoNotSaveDynamicResponseInLogEntry { get; set; }
92114

src/WireMock.Net/Http/HttpResponseMessageHelper.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public static async Task<ResponseMessage> CreateAsync(
1515
Uri requiredUri,
1616
Uri originalUri,
1717
bool deserializeJson,
18-
bool decompressGzipAndDeflate)
18+
bool decompressGzipAndDeflate,
19+
bool deserializeFormUrlEncoded)
1920
{
2021
var responseMessage = new ResponseMessage { StatusCode = (int)httpResponseMessage.StatusCode };
2122

@@ -44,7 +45,8 @@ public static async Task<ResponseMessage> CreateAsync(
4445
ContentType = contentTypeHeader?.FirstOrDefault(),
4546
DeserializeJson = deserializeJson,
4647
ContentEncoding = contentEncodingHeader?.FirstOrDefault(),
47-
DecompressGZipAndDeflate = decompressGzipAndDeflate
48+
DecompressGZipAndDeflate = decompressGzipAndDeflate,
49+
DeserializeFormUrlEncoded = deserializeFormUrlEncoded
4850
};
4951
responseMessage.BodyData = await BodyParser.ParseAsync(bodyParserSettings).ConfigureAwait(false);
5052
}
@@ -55,7 +57,7 @@ public static async Task<ResponseMessage> CreateAsync(
5557
// If Location header contains absolute redirect URL, and base URL is one that we proxy to,
5658
// we need to replace it to original one.
5759
if (string.Equals(header.Key, HttpKnownHeaderNames.Location, StringComparison.OrdinalIgnoreCase)
58-
&& Uri.TryCreate(header.Value.First(), UriKind.Absolute, out Uri absoluteLocationUri)
60+
&& Uri.TryCreate(header.Value.First(), UriKind.Absolute, out var absoluteLocationUri)
5961
&& string.Equals(absoluteLocationUri.Host, requiredUri.Host, StringComparison.OrdinalIgnoreCase))
6062
{
6163
var replacedLocationUri = new Uri(originalUri, absoluteLocationUri.PathAndQuery);

src/WireMock.Net/Json/JObjectExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ internal static IEnumerable ToDynamicClassArray(this JArray? src, DynamicJsonCla
6060
{
6161
if (src == null)
6262
{
63-
return new object?[0];
63+
return EmptyArray<object?>.Value;
6464
}
6565

6666
return ConvertJTokenArray(src, options);

src/WireMock.Net/Matchers/NotNullOrEmptyMatcher.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Linq;
23
using AnyOfTypes;
34
using WireMock.Models;
@@ -62,7 +63,7 @@ public double IsMatch(string? input)
6263
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
6364
public AnyOf<string, StringPattern>[] GetPatterns()
6465
{
65-
return new AnyOf<string, StringPattern>[0];
66+
return EmptyArray<AnyOf<string, StringPattern>>.Value;
6667
}
6768

6869
/// <inheritdoc />

src/WireMock.Net/Proxy/ProxyHelper.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,16 @@ public ProxyHelper(WireMockServerSettings settings)
4545
// Create ResponseMessage
4646
bool deserializeJson = !_settings.DisableJsonBodyParsing.GetValueOrDefault(false);
4747
bool decompressGzipAndDeflate = !_settings.DisableRequestBodyDecompressing.GetValueOrDefault(false);
48+
bool deserializeFormUrlEncoded = !_settings.DisableDeserializeFormUrlEncoded.GetValueOrDefault(false);
4849

49-
var responseMessage = await HttpResponseMessageHelper.CreateAsync(httpResponseMessage, requiredUri, originalUri, deserializeJson, decompressGzipAndDeflate).ConfigureAwait(false);
50+
var responseMessage = await HttpResponseMessageHelper.CreateAsync(
51+
httpResponseMessage,
52+
requiredUri,
53+
originalUri,
54+
deserializeJson,
55+
decompressGzipAndDeflate,
56+
deserializeFormUrlEncoded
57+
).ConfigureAwait(false);
5058

5159
IMapping? newMapping = null;
5260

@@ -56,11 +64,11 @@ public ProxyHelper(WireMockServerSettings settings)
5664
if (saveMappingSettings != null)
5765
{
5866
save &= Check(saveMappingSettings.StatusCodePattern,
59-
() => HttpStatusRangeParser.IsMatch(saveMappingSettings.StatusCodePattern, responseMessage.StatusCode)
67+
() => saveMappingSettings.StatusCodePattern != null && HttpStatusRangeParser.IsMatch(saveMappingSettings.StatusCodePattern, responseMessage.StatusCode)
6068
);
6169

6270
save &= Check(saveMappingSettings.HttpMethods,
63-
() => saveMappingSettings.HttpMethods.Value.Contains(requestMessage.Method, StringComparer.OrdinalIgnoreCase)
71+
() => saveMappingSettings.HttpMethods != null && saveMappingSettings.HttpMethods.Value.Contains(requestMessage.Method, StringComparer.OrdinalIgnoreCase)
6472
);
6573
}
6674

src/WireMock.Net/Serialization/MappingConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public string ToCSharpCode(IMapping mapping, MappingConverterSettings? settings
140140
{
141141
sb.AppendLine($" .WithDelay({response.Delay.Value.TotalMilliseconds})");
142142
}
143-
else if (response.MinimumDelayMilliseconds > 0 && response.MaximumDelayMilliseconds > 0)
143+
else if (response is { MinimumDelayMilliseconds: > 0, MaximumDelayMilliseconds: > 0 })
144144
{
145145
sb.AppendLine($" .WithRandomDelay({response.MinimumDelayMilliseconds}, {response.MaximumDelayMilliseconds})");
146146
}

src/WireMock.Net/Server/WireMockServer.Admin.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,25 +217,29 @@ private IResponseMessage SettingsGet(IRequestMessage requestMessage)
217217
var model = new SettingsModel
218218
{
219219
AllowBodyForAllHttpMethods = _settings.AllowBodyForAllHttpMethods,
220+
AllowOnlyDefinedHttpStatusCodeInResponse = _settings.AllowOnlyDefinedHttpStatusCodeInResponse,
220221
AllowPartialMapping = _settings.AllowPartialMapping,
222+
DisableDeserializeFormUrlEncoded = _settings.DisableDeserializeFormUrlEncoded,
223+
DisableJsonBodyParsing = _settings.DisableJsonBodyParsing,
224+
DisableRequestBodyDecompressing = _settings.DisableRequestBodyDecompressing,
225+
DoNotSaveDynamicResponseInLogEntry = _settings.DoNotSaveDynamicResponseInLogEntry,
221226
GlobalProcessingDelay = (int?)_options.RequestProcessingDelay?.TotalMilliseconds,
222227
HandleRequestsSynchronously = _settings.HandleRequestsSynchronously,
228+
HostingScheme = _settings.HostingScheme,
223229
MaxRequestLogCount = _settings.MaxRequestLogCount,
230+
QueryParameterMultipleValueSupport = _settings.QueryParameterMultipleValueSupport,
224231
ReadStaticMappings = _settings.ReadStaticMappings,
225232
RequestLogExpirationDuration = _settings.RequestLogExpirationDuration,
226233
SaveUnmatchedRequests = _settings.SaveUnmatchedRequests,
227234
ThrowExceptionWhenMatcherFails = _settings.ThrowExceptionWhenMatcherFails,
228235
UseRegexExtended = _settings.UseRegexExtended,
229236
WatchStaticMappings = _settings.WatchStaticMappings,
230237
WatchStaticMappingsInSubdirectories = _settings.WatchStaticMappingsInSubdirectories,
231-
HostingScheme = _settings.HostingScheme,
232-
DoNotSaveDynamicResponseInLogEntry = _settings.DoNotSaveDynamicResponseInLogEntry,
233-
QueryParameterMultipleValueSupport = _settings.QueryParameterMultipleValueSupport,
234238

235239
#if USE_ASPNETCORE
236-
CorsPolicyOptions = _settings.CorsPolicyOptions?.ToString(),
240+
AcceptAnyClientCertificate = _settings.AcceptAnyClientCertificate,
237241
ClientCertificateMode = _settings.ClientCertificateMode,
238-
AcceptAnyClientCertificate = _settings.AcceptAnyClientCertificate
242+
CorsPolicyOptions = _settings.CorsPolicyOptions?.ToString()
239243
#endif
240244
};
241245

@@ -250,19 +254,23 @@ private IResponseMessage SettingsUpdate(IRequestMessage requestMessage)
250254

251255
// _settings
252256
_settings.AllowBodyForAllHttpMethods = settings.AllowBodyForAllHttpMethods;
257+
_settings.AllowOnlyDefinedHttpStatusCodeInResponse = settings.AllowOnlyDefinedHttpStatusCodeInResponse;
253258
_settings.AllowPartialMapping = settings.AllowPartialMapping;
259+
_settings.DisableDeserializeFormUrlEncoded = settings.DisableDeserializeFormUrlEncoded;
260+
_settings.DisableJsonBodyParsing = settings.DisableJsonBodyParsing;
261+
_settings.DisableRequestBodyDecompressing = settings.DisableRequestBodyDecompressing;
262+
_settings.DoNotSaveDynamicResponseInLogEntry = settings.DoNotSaveDynamicResponseInLogEntry;
254263
_settings.HandleRequestsSynchronously = settings.HandleRequestsSynchronously;
255264
_settings.MaxRequestLogCount = settings.MaxRequestLogCount;
256265
_settings.ProxyAndRecordSettings = TinyMapperUtils.Instance.Map(settings.ProxyAndRecordSettings);
266+
_settings.QueryParameterMultipleValueSupport = settings.QueryParameterMultipleValueSupport;
257267
_settings.ReadStaticMappings = settings.ReadStaticMappings;
258268
_settings.RequestLogExpirationDuration = settings.RequestLogExpirationDuration;
259269
_settings.SaveUnmatchedRequests = settings.SaveUnmatchedRequests;
260270
_settings.ThrowExceptionWhenMatcherFails = settings.ThrowExceptionWhenMatcherFails;
261271
_settings.UseRegexExtended = settings.UseRegexExtended;
262272
_settings.WatchStaticMappings = settings.WatchStaticMappings;
263273
_settings.WatchStaticMappingsInSubdirectories = settings.WatchStaticMappingsInSubdirectories;
264-
_settings.DoNotSaveDynamicResponseInLogEntry = settings.DoNotSaveDynamicResponseInLogEntry;
265-
_settings.QueryParameterMultipleValueSupport = settings.QueryParameterMultipleValueSupport;
266274

267275
InitSettings(_settings);
268276

src/WireMock.Net/Settings/ProxySaveMappingSetting.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ public class ProxySaveMappingSetting<T>
66
{
77
public MatchBehaviour MatchBehaviour { get; } = MatchBehaviour.AcceptOnMatch;
88

9-
public T Value { get; private set; }
9+
public T Value { get; }
1010

1111
public ProxySaveMappingSetting(T value, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
1212
{
1313
Value = value;
1414
MatchBehaviour = matchBehaviour;
1515
}
1616

17-
public static implicit operator ProxySaveMappingSetting<T>(T value) => new ProxySaveMappingSetting<T>(value);
17+
public static implicit operator ProxySaveMappingSetting<T>(T value) => new(value);
1818

1919
public static implicit operator T(ProxySaveMappingSetting<T> @this) => @this.Value;
2020
}

src/WireMock.Net/Settings/SimpleCommandLineParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void Parse(string[] arguments)
3232
}
3333
else if (string.IsNullOrEmpty(currentName))
3434
{
35-
Arguments[arg] = new string[0];
35+
Arguments[arg] = EmptyArray<string>.Value;
3636
}
3737
else
3838
{

src/WireMock.Net/Settings/WireMockServerSettings.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,39 +182,45 @@ public class WireMockServerSettings
182182
public bool? AllowCSharpCodeMatcher { get; set; }
183183

184184
/// <summary>
185-
/// Allow a Body for all HTTP Methods. (default set to false).
185+
/// Allow a Body for all HTTP Methods. (default set to <c>false</c>).
186186
/// </summary>
187187
[PublicAPI]
188188
public bool? AllowBodyForAllHttpMethods { get; set; }
189189

190190
/// <summary>
191-
/// Allow only a HttpStatus Code in the response which is defined. (default set to false).
191+
/// Allow only a HttpStatus Code in the response which is defined. (default set to <c>false</c>).
192192
/// - false : also null, 0, empty or invalid HttpStatus codes are allowed.
193193
/// - true : only codes defined in <see cref="System.Net.HttpStatusCode"/> are allowed.
194194
/// </summary>
195195
[PublicAPI]
196196
public bool? AllowOnlyDefinedHttpStatusCodeInResponse { get; set; }
197197

198198
/// <summary>
199-
/// Set to true to disable Json deserialization when processing requests. (default set to false).
199+
/// Set to true to disable Json deserialization when processing requests. (default set to <c>false</c>).
200200
/// </summary>
201201
[PublicAPI]
202202
public bool? DisableJsonBodyParsing { get; set; }
203203

204204
/// <summary>
205-
/// Disable support for GZip and Deflate request body decompression. (default set to false).
205+
/// Disable support for GZip and Deflate request body decompression. (default set to <c>false</c>).
206206
/// </summary>
207207
[PublicAPI]
208208
public bool? DisableRequestBodyDecompressing { get; set; }
209209

210210
/// <summary>
211-
/// Handle all requests synchronously. (default set to false).
211+
/// Set to true to disable FormUrlEncoded deserializing when processing requests. (default set to <c>false</c>).
212+
/// </summary>
213+
[PublicAPI]
214+
public bool? DisableDeserializeFormUrlEncoded { get; set; }
215+
216+
/// <summary>
217+
/// Handle all requests synchronously. (default set to <c>false</c>).
212218
/// </summary>
213219
[PublicAPI]
214220
public bool? HandleRequestsSynchronously { get; set; }
215221

216222
/// <summary>
217-
/// Throw an exception when the <see cref="IMatcher"/> fails because of invalid input. (default set to false).
223+
/// Throw an exception when the <see cref="IMatcher"/> fails because of invalid input. (default set to <c>false</c>).
218224
/// </summary>
219225
[PublicAPI]
220226
public bool? ThrowExceptionWhenMatcherFails { get; set; }
@@ -255,19 +261,19 @@ public class WireMockServerSettings
255261
public WebhookSettings? WebhookSettings { get; set; }
256262

257263
/// <summary>
258-
/// Use the <see cref="RegexExtended"/> instead of the default <see cref="Regex"/> (default set to true).
264+
/// Use the <see cref="RegexExtended"/> instead of the default <see cref="Regex"/> (default set to <c>true</c>).
259265
/// </summary>
260266
[PublicAPI]
261267
public bool? UseRegexExtended { get; set; } = true;
262268

263269
/// <summary>
264-
/// Save unmatched requests to a file using the <see cref="IFileSystemHandler"/> (default set to false).
270+
/// Save unmatched requests to a file using the <see cref="IFileSystemHandler"/> (default set to <c>false</c>).
265271
/// </summary>
266272
[PublicAPI]
267273
public bool? SaveUnmatchedRequests { get; set; }
268274

269275
/// <summary>
270-
/// Don't save the response-string in the LogEntry when WithBody(Func{IRequestMessage, string}) or WithBody(Func{IRequestMessage, Task{string}}) is used. (default set to false).
276+
/// Don't save the response-string in the LogEntry when WithBody(Func{IRequestMessage, string}) or WithBody(Func{IRequestMessage, Task{string}}) is used. (default set to <c>false</c>).
271277
/// </summary>
272278
[PublicAPI]
273279
public bool? DoNotSaveDynamicResponseInLogEntry { get; set; }

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy