don't strip request body if we don't recognize the request method (#294)

* don't strip request body if we don't recognize the request method

* use IsNullOrEmpty
This commit is contained in:
Eli Bishop
2019-07-03 09:15:17 -07:00
committed by Stef Heyenrath
parent 55a0a6ee71
commit 06576ab317
3 changed files with 108 additions and 2 deletions

View File

@@ -2,6 +2,7 @@
using MimeKit;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
@@ -27,7 +28,18 @@ namespace WireMock.Util
CONNECT - No defined body semantics
PATCH - Body supported.
*/
private static readonly string[] AllowedBodyParseMethods = { "PUT", "POST", "OPTIONS", "PATCH" };
private static readonly IDictionary<string, bool> BodyAllowedForMethods = new Dictionary<string, bool>
{
{ "HEAD", false },
{ "GET", false },
{ "PUT", true },
{ "POST", true },
{ "DELETE", false },
{ "TRACE", false },
{ "OPTIONS", true },
{ "CONNECT", false },
{ "PATCH", true }
};
private static readonly IStringMatcher[] MultipartContentTypesMatchers = {
new WildcardMatcher("multipart/*", true)
@@ -53,7 +65,19 @@ namespace WireMock.Util
public static bool ShouldParseBody([CanBeNull] string method)
{
return AllowedBodyParseMethods.Contains(method, StringComparer.OrdinalIgnoreCase);
if (String.IsNullOrEmpty(method))
{
return false;
}
if (BodyAllowedForMethods.TryGetValue(method.ToUpper(), out var allowed))
{
return allowed;
}
// If we don't have any knowledge of this method, we should assume that a body *may*
// be present, so we should parse it if it is. Therefore, if a new method is added to
// the HTTP Method Registry, we only really need to add it to BodyAllowedForMethods if
// we want to make it clear that a body is *not* allowed.
return true;
}
public static BodyType DetectBodyTypeFromContentType([CanBeNull] string contentTypeValue)