mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-20 07:51:41 +02:00
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:
committed by
Stef Heyenrath
parent
55a0a6ee71
commit
06576ab317
@@ -2,6 +2,7 @@
|
|||||||
using MimeKit;
|
using MimeKit;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@@ -27,7 +28,18 @@ namespace WireMock.Util
|
|||||||
CONNECT - No defined body semantics
|
CONNECT - No defined body semantics
|
||||||
PATCH - Body supported.
|
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 = {
|
private static readonly IStringMatcher[] MultipartContentTypesMatchers = {
|
||||||
new WildcardMatcher("multipart/*", true)
|
new WildcardMatcher("multipart/*", true)
|
||||||
@@ -53,7 +65,19 @@ namespace WireMock.Util
|
|||||||
|
|
||||||
public static bool ShouldParseBody([CanBeNull] string method)
|
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)
|
public static BodyType DetectBodyTypeFromContentType([CanBeNull] string contentTypeValue)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using NFluent;
|
using NFluent;
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
@@ -172,5 +173,63 @@ namespace WireMock.Net.Tests
|
|||||||
Check.That(response.Headers.Contains("test")).IsTrue();
|
Check.That(response.Headers.Contains("test")).IsTrue();
|
||||||
Check.That(response.Headers.Contains("Transfer-Encoding")).IsFalse();
|
Check.That(response.Headers.Contains("Transfer-Encoding")).IsFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("GET")]
|
||||||
|
[InlineData("TRACE")]
|
||||||
|
[InlineData("DELETE")]
|
||||||
|
public async Task FluentMockServer_Should_exclude_body_for_methods_where_body_is_definitely_disallowed(string method)
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
string content = "hello";
|
||||||
|
var server = FluentMockServer.Start();
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request.Create().WithBody(b => true))
|
||||||
|
.AtPriority(0)
|
||||||
|
.RespondWith(Response.Create().WithStatusCode(400));
|
||||||
|
server
|
||||||
|
.Given(Request.Create())
|
||||||
|
.AtPriority(1)
|
||||||
|
.RespondWith(Response.Create().WithStatusCode(200));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var request = new HttpRequestMessage(new HttpMethod(method), "http://localhost:" + server.Ports[0] + "/");
|
||||||
|
request.Content = new StringContent(content);
|
||||||
|
var response = await new HttpClient().SendAsync(request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Check.That(response.StatusCode).Equals(HttpStatusCode.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("POST")]
|
||||||
|
[InlineData("PUT")]
|
||||||
|
[InlineData("OPTIONS")]
|
||||||
|
[InlineData("REPORT")]
|
||||||
|
[InlineData("SOME-UNKNOWN-METHOD")] // default behavior for unknown methods is to allow a body (see BodyParser.ShouldParseBody)
|
||||||
|
public async Task FluentMockServer_Should_not_exclude_body_for_supported_methods(string method)
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
string content = "hello";
|
||||||
|
var server = FluentMockServer.Start();
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request.Create().WithBody(content))
|
||||||
|
.AtPriority(0)
|
||||||
|
.RespondWith(Response.Create().WithStatusCode(200));
|
||||||
|
server
|
||||||
|
.Given(Request.Create())
|
||||||
|
.AtPriority(1)
|
||||||
|
.RespondWith(Response.Create().WithStatusCode(400));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var request = new HttpRequestMessage(new HttpMethod(method), "http://localhost:" + server.Ports[0] + "/");
|
||||||
|
request.Content = new StringContent(content);
|
||||||
|
var response = await new HttpClient().SendAsync(request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Check.That(response.StatusCode).Equals(HttpStatusCode.OK);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -125,5 +125,28 @@ Content-Type: text/html
|
|||||||
Check.That(body.DetectedBodyType).IsEqualTo(detectedBodyType);
|
Check.That(body.DetectedBodyType).IsEqualTo(detectedBodyType);
|
||||||
Check.That(body.DetectedBodyTypeFromContentType).IsEqualTo(detectedBodyTypeFromContentType);
|
Check.That(body.DetectedBodyTypeFromContentType).IsEqualTo(detectedBodyTypeFromContentType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("HEAD", false)]
|
||||||
|
[InlineData("GET", false)]
|
||||||
|
[InlineData("PUT", true)]
|
||||||
|
[InlineData("POST", true)]
|
||||||
|
[InlineData("DELETE", false)]
|
||||||
|
[InlineData("TRACE", false)]
|
||||||
|
[InlineData("OPTIONS", true)]
|
||||||
|
[InlineData("CONNECT", false)]
|
||||||
|
[InlineData("PATCH", true)]
|
||||||
|
public void BodyParser_ShouldParseBody_ExpectedResultForKnownMethods(string method, bool resultShouldBe)
|
||||||
|
{
|
||||||
|
Check.That(BodyParser.ShouldParseBody(method)).Equals(resultShouldBe);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("REPORT")]
|
||||||
|
[InlineData("SOME-UNKNOWN-METHOD")]
|
||||||
|
public void BodyParser_ShouldParseBody_DefaultIsTrueForUnknownMethods(string method)
|
||||||
|
{
|
||||||
|
Check.That(BodyParser.ShouldParseBody(method)).IsTrue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user