mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-17 14:40:00 +02:00
UsingOptions, UsingConnect and UsingTrace (#427)
This commit is contained in:
18
src/WireMock.Net/Http/HttpRequestMethods.cs
Normal file
18
src/WireMock.Net/Http/HttpRequestMethods.cs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
namespace WireMock.Http
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
|
||||||
|
/// </summary>
|
||||||
|
internal static class HttpRequestMethods
|
||||||
|
{
|
||||||
|
public const string CONNECT = "CONNECT";
|
||||||
|
public const string DELETE = "DELETE";
|
||||||
|
public const string GET = "GET";
|
||||||
|
public const string HEAD = "HEAD";
|
||||||
|
public const string OPTIONS = "OPTIONS";
|
||||||
|
public const string PATCH = "PATCH";
|
||||||
|
public const string POST = "POST";
|
||||||
|
public const string PUT = "PUT";
|
||||||
|
public const string TRACE = "TRACE";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,13 @@ namespace WireMock.RequestBuilders
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IMethodRequestBuilder : IHeadersRequestBuilder
|
public interface IMethodRequestBuilder : IHeadersRequestBuilder
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// UsingConnect: add HTTP Method matching on `CONNECT` and matchBehaviour (optional).
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
||||||
|
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
||||||
|
IRequestBuilder UsingConnect(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// UsingDelete: add HTTP Method matching on `DELETE` and matchBehaviour (optional).
|
/// UsingDelete: add HTTP Method matching on `DELETE` and matchBehaviour (optional).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -24,7 +31,7 @@ namespace WireMock.RequestBuilders
|
|||||||
IRequestBuilder UsingGet(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
|
IRequestBuilder UsingGet(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Add HTTP Method matching on `HEAD` and matchBehaviour (optional).
|
/// UsingHead: Add HTTP Method matching on `HEAD` and matchBehaviour (optional).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="matchBehaviour">The match behaviour.</param>
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
||||||
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
||||||
@@ -44,6 +51,13 @@ namespace WireMock.RequestBuilders
|
|||||||
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
||||||
IRequestBuilder UsingPatch(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
|
IRequestBuilder UsingPatch(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// UsingPut: add HTTP Method matching on `OPTIONS` and matchBehaviour (optional).
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
||||||
|
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
||||||
|
IRequestBuilder UsingOptions(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// UsingPut: add HTTP Method matching on `PUT` and matchBehaviour (optional).
|
/// UsingPut: add HTTP Method matching on `PUT` and matchBehaviour (optional).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -51,6 +65,13 @@ namespace WireMock.RequestBuilders
|
|||||||
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
||||||
IRequestBuilder UsingPut(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
|
IRequestBuilder UsingPut(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// UsingTrace: add HTTP Method matching on `TRACE` and matchBehaviour (optional).
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
||||||
|
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
||||||
|
IRequestBuilder UsingTrace(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// UsingAnyMethod: add HTTP Method matching on any method.
|
/// UsingAnyMethod: add HTTP Method matching on any method.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
113
src/WireMock.Net/RequestBuilders/Request.UsingMethods.cs
Normal file
113
src/WireMock.Net/RequestBuilders/Request.UsingMethods.cs
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using WireMock.Http;
|
||||||
|
using WireMock.Matchers;
|
||||||
|
using WireMock.Matchers.Request;
|
||||||
|
using WireMock.Validation;
|
||||||
|
|
||||||
|
namespace WireMock.RequestBuilders
|
||||||
|
{
|
||||||
|
public partial class Request
|
||||||
|
{
|
||||||
|
/// <inheritdoc cref="IMethodRequestBuilder.UsingConnect(MatchBehaviour)"/>
|
||||||
|
public IRequestBuilder UsingConnect(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
||||||
|
{
|
||||||
|
_requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, HttpRequestMethods.CONNECT));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IMethodRequestBuilder.UsingDelete(MatchBehaviour)"/>
|
||||||
|
public IRequestBuilder UsingDelete(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
||||||
|
{
|
||||||
|
_requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, HttpRequestMethods.DELETE));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IMethodRequestBuilder.UsingGet(MatchBehaviour)"/>
|
||||||
|
public IRequestBuilder UsingGet(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
||||||
|
{
|
||||||
|
_requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, HttpRequestMethods.GET));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IMethodRequestBuilder.UsingHead(MatchBehaviour)"/>
|
||||||
|
public IRequestBuilder UsingHead(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
||||||
|
{
|
||||||
|
_requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, HttpRequestMethods.HEAD));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IMethodRequestBuilder.UsingOptions(MatchBehaviour)"/>
|
||||||
|
public IRequestBuilder UsingOptions(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
||||||
|
{
|
||||||
|
_requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, HttpRequestMethods.OPTIONS));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IMethodRequestBuilder.UsingPost(MatchBehaviour)"/>
|
||||||
|
public IRequestBuilder UsingPost(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
||||||
|
{
|
||||||
|
_requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, HttpRequestMethods.POST));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IMethodRequestBuilder.UsingPatch(MatchBehaviour)"/>
|
||||||
|
public IRequestBuilder UsingPatch(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
||||||
|
{
|
||||||
|
_requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, HttpRequestMethods.PATCH));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IMethodRequestBuilder.UsingPut(MatchBehaviour)"/>
|
||||||
|
public IRequestBuilder UsingPut(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
||||||
|
{
|
||||||
|
_requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, HttpRequestMethods.PUT));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IMethodRequestBuilder.UsingTrace(MatchBehaviour)"/>
|
||||||
|
public IRequestBuilder UsingTrace(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
||||||
|
{
|
||||||
|
_requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, HttpRequestMethods.TRACE));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IMethodRequestBuilder.UsingAnyMethod"/>
|
||||||
|
public IRequestBuilder UsingAnyMethod()
|
||||||
|
{
|
||||||
|
var matchers = _requestMatchers.Where(m => m is RequestMessageMethodMatcher).ToList();
|
||||||
|
foreach (var matcher in matchers)
|
||||||
|
{
|
||||||
|
_requestMatchers.Remove(matcher);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IMethodRequestBuilder.UsingAnyVerb"/>
|
||||||
|
public IRequestBuilder UsingAnyVerb()
|
||||||
|
{
|
||||||
|
return UsingAnyMethod();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IMethodRequestBuilder.UsingMethod(string[])"/>
|
||||||
|
public IRequestBuilder UsingMethod(params string[] methods)
|
||||||
|
{
|
||||||
|
return UsingMethod(MatchBehaviour.AcceptOnMatch, methods);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IMethodRequestBuilder.UsingVerb(string[])"/>
|
||||||
|
public IRequestBuilder UsingVerb(params string[] verbs)
|
||||||
|
{
|
||||||
|
return UsingMethod(verbs);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IMethodRequestBuilder.UsingMethod(MatchBehaviour, string[])"/>
|
||||||
|
public IRequestBuilder UsingMethod(MatchBehaviour matchBehaviour, params string[] methods)
|
||||||
|
{
|
||||||
|
Check.NotNullOrEmpty(methods, nameof(methods));
|
||||||
|
|
||||||
|
_requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, methods));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -151,86 +151,5 @@ namespace WireMock.RequestBuilders
|
|||||||
_requestMatchers.Add(new RequestMessageUrlMatcher(funcs));
|
_requestMatchers.Add(new RequestMessageUrlMatcher(funcs));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IMethodRequestBuilder.UsingDelete(MatchBehaviour)"/>
|
|
||||||
public IRequestBuilder UsingDelete(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
|
||||||
{
|
|
||||||
_requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, "DELETE"));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IMethodRequestBuilder.UsingGet(MatchBehaviour)"/>
|
|
||||||
public IRequestBuilder UsingGet(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
|
||||||
{
|
|
||||||
_requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, "GET"));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IMethodRequestBuilder.UsingHead(MatchBehaviour)"/>
|
|
||||||
public IRequestBuilder UsingHead(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
|
||||||
{
|
|
||||||
_requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, "HEAD"));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IMethodRequestBuilder.UsingPost(MatchBehaviour)"/>
|
|
||||||
public IRequestBuilder UsingPost(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
|
||||||
{
|
|
||||||
_requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, "POST"));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IMethodRequestBuilder.UsingPatch(MatchBehaviour)"/>
|
|
||||||
public IRequestBuilder UsingPatch(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
|
||||||
{
|
|
||||||
_requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, "PATCH"));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IMethodRequestBuilder.UsingPut(MatchBehaviour)"/>
|
|
||||||
public IRequestBuilder UsingPut(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
|
||||||
{
|
|
||||||
_requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, "PUT"));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IMethodRequestBuilder.UsingAnyMethod"/>
|
|
||||||
public IRequestBuilder UsingAnyMethod()
|
|
||||||
{
|
|
||||||
var matchers = _requestMatchers.Where(m => m is RequestMessageMethodMatcher).ToList();
|
|
||||||
foreach (var matcher in matchers)
|
|
||||||
{
|
|
||||||
_requestMatchers.Remove(matcher);
|
|
||||||
}
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IMethodRequestBuilder.UsingAnyVerb"/>
|
|
||||||
public IRequestBuilder UsingAnyVerb()
|
|
||||||
{
|
|
||||||
return UsingAnyMethod();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IMethodRequestBuilder.UsingMethod(string[])"/>
|
|
||||||
public IRequestBuilder UsingMethod(params string[] methods)
|
|
||||||
{
|
|
||||||
return UsingMethod(MatchBehaviour.AcceptOnMatch, methods);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IMethodRequestBuilder.UsingVerb(string[])"/>
|
|
||||||
public IRequestBuilder UsingVerb(params string[] verbs)
|
|
||||||
{
|
|
||||||
return UsingMethod(verbs);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IMethodRequestBuilder.UsingMethod(MatchBehaviour, string[])"/>
|
|
||||||
public IRequestBuilder UsingMethod(MatchBehaviour matchBehaviour, params string[] methods)
|
|
||||||
{
|
|
||||||
Check.NotNullOrEmpty(methods, nameof(methods));
|
|
||||||
|
|
||||||
_requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, methods));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,7 @@ using System.Text;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using WireMock.Http;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
using WireMock.Types;
|
using WireMock.Types;
|
||||||
using WireMock.Validation;
|
using WireMock.Validation;
|
||||||
@@ -30,15 +31,15 @@ namespace WireMock.Util
|
|||||||
*/
|
*/
|
||||||
private static readonly IDictionary<string, bool> BodyAllowedForMethods = new Dictionary<string, bool>
|
private static readonly IDictionary<string, bool> BodyAllowedForMethods = new Dictionary<string, bool>
|
||||||
{
|
{
|
||||||
{ "HEAD", false },
|
{ HttpRequestMethods.HEAD, false },
|
||||||
{ "GET", false },
|
{ HttpRequestMethods.GET, false },
|
||||||
{ "PUT", true },
|
{ HttpRequestMethods.PUT, true },
|
||||||
{ "POST", true },
|
{ HttpRequestMethods.POST, true },
|
||||||
{ "DELETE", true },
|
{ HttpRequestMethods.DELETE, true },
|
||||||
{ "TRACE", false },
|
{ HttpRequestMethods.TRACE, false },
|
||||||
{ "OPTIONS", true },
|
{ HttpRequestMethods.OPTIONS, true },
|
||||||
{ "CONNECT", false },
|
{ HttpRequestMethods.CONNECT, false },
|
||||||
{ "PATCH", true }
|
{ HttpRequestMethods.PATCH, true }
|
||||||
};
|
};
|
||||||
|
|
||||||
private static readonly IStringMatcher[] MultipartContentTypesMatchers = {
|
private static readonly IStringMatcher[] MultipartContentTypesMatchers = {
|
||||||
|
|||||||
@@ -8,18 +8,54 @@ namespace WireMock.Net.Tests.RequestBuilders
|
|||||||
{
|
{
|
||||||
public class RequestBuilderUsingMethodTests
|
public class RequestBuilderUsingMethodTests
|
||||||
{
|
{
|
||||||
|
[Fact]
|
||||||
|
public void RequestBuilder_UsingConnect()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var requestBuilder = (Request)Request.Create().UsingConnect();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var matchers = requestBuilder.GetPrivateFieldValue<IList<IRequestMatcher>>("_requestMatchers");
|
||||||
|
Check.That(matchers.Count).IsEqualTo(1);
|
||||||
|
Check.That((matchers[0] as RequestMessageMethodMatcher).Methods).ContainsExactly("CONNECT");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RequestBuilder_UsingOptions()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var requestBuilder = (Request)Request.Create().UsingOptions();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var matchers = requestBuilder.GetPrivateFieldValue<IList<IRequestMatcher>>("_requestMatchers");
|
||||||
|
Check.That(matchers.Count).IsEqualTo(1);
|
||||||
|
Check.That((matchers[0] as RequestMessageMethodMatcher).Methods).ContainsExactly("OPTIONS");
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void RequestBuilder_UsingPatch()
|
public void RequestBuilder_UsingPatch()
|
||||||
{
|
{
|
||||||
// Act
|
// Act
|
||||||
var requestBuilder = (Request)Request.Create().UsingPatch();
|
var requestBuilder = (Request)Request.Create().UsingPatch();
|
||||||
|
|
||||||
// Assert 1
|
// Assert
|
||||||
var matchers = requestBuilder.GetPrivateFieldValue<IList<IRequestMatcher>>("_requestMatchers");
|
var matchers = requestBuilder.GetPrivateFieldValue<IList<IRequestMatcher>>("_requestMatchers");
|
||||||
Check.That(matchers.Count).IsEqualTo(1);
|
Check.That(matchers.Count).IsEqualTo(1);
|
||||||
Check.That((matchers[0] as RequestMessageMethodMatcher).Methods).ContainsExactly("PATCH");
|
Check.That((matchers[0] as RequestMessageMethodMatcher).Methods).ContainsExactly("PATCH");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RequestBuilder_UsingTrace()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var requestBuilder = (Request)Request.Create().UsingTrace();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var matchers = requestBuilder.GetPrivateFieldValue<IList<IRequestMatcher>>("_requestMatchers");
|
||||||
|
Check.That(matchers.Count).IsEqualTo(1);
|
||||||
|
Check.That((matchers[0] as RequestMessageMethodMatcher).Methods).ContainsExactly("TRACE");
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void RequestBuilder_UsingAnyMethod_ClearsAllOtherMatches()
|
public void RequestBuilder_UsingAnyMethod_ClearsAllOtherMatches()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user