Update + add fluent builder methods (#1042)

* Update some fluent builder methods

* fix

* sc

* ThenRespondWith

* // Copyright © WireMock.Net
This commit is contained in:
Stef Heyenrath
2024-07-27 16:16:11 +02:00
committed by GitHub
parent 275816c414
commit 69c829fae0
6 changed files with 168 additions and 41 deletions

View File

@@ -2,8 +2,11 @@
using System;
using System.Collections.Generic;
using System.Net;
using WireMock.Models;
using WireMock.ResponseBuilders;
using WireMock.ResponseProviders;
using WireMock.Settings;
using WireMock.Types;
namespace WireMock.Server;
@@ -25,6 +28,27 @@ public interface IRespondWithAProvider
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WithGuid(Guid guid);
/// <summary>
/// Define a unique identifier for this mapping.
/// </summary>
/// <param name="guid">The unique identifier.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WithGuid(string guid);
/// <summary>
/// Define a unique identifier for this mapping.
/// </summary>
/// <param name="guid">The unique identifier.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider DefineGuid(Guid guid);
/// <summary>
/// Define a unique identifier for this mapping.
/// </summary>
/// <param name="guid">The unique identifier.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider DefineGuid(string guid);
/// <summary>
/// Define the TimeSettings for this mapping.
/// </summary>
@@ -53,13 +77,6 @@ public interface IRespondWithAProvider
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WithPath(string path);
/// <summary>
/// Define a unique identifier for this mapping.
/// </summary>
/// <param name="guid">The unique identifier.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WithGuid(string guid);
/// <summary>
/// Define the priority for this mapping.
/// </summary>
@@ -68,11 +85,43 @@ public interface IRespondWithAProvider
IRespondWithAProvider AtPriority(int priority);
/// <summary>
/// The respond with.
/// RespondWith
/// </summary>
/// <param name="provider">The provider.</param>
void RespondWith(IResponseProvider provider);
/// <summary>
/// RespondWith
/// </summary>
/// <param name="action">The action to use the fluent <see cref="IResponseBuilder"/>.</param>
void ThenRespondWith(Action<IResponseBuilder> action);
/// <summary>
/// RespondWith a status code 200 (OK);
/// </summary>
void ThenRespondWithOK();
/// <summary>
/// RespondWith a status code.
/// By default all status codes are allowed, to change this behaviour, see <inheritdoc cref="WireMockServerSettings.AllowOnlyDefinedHttpStatusCodeInResponse"/>.
/// </summary>
/// <param name="code">The code.</param>
void ThenRespondWithStatusCode(int code);
/// <summary>
/// RespondWith a status code.
/// By default all status codes are allowed, to change this behaviour, see <inheritdoc cref="WireMockServerSettings.AllowOnlyDefinedHttpStatusCodeInResponse"/>.
/// </summary>
/// <param name="code">The code.</param>
void ThenRespondWithStatusCode(string code);
/// <summary>
/// RespondWith a status code.
/// By default all status codes are allowed, to change this behaviour, see <inheritdoc cref="WireMockServerSettings.AllowOnlyDefinedHttpStatusCodeInResponse"/>.
/// </summary>
/// <param name="code">The code.</param>
void ThenRespondWithStatusCode(HttpStatusCode code);
/// <summary>
/// Sets the the scenario.
/// </summary>

View File

@@ -4,9 +4,11 @@
// For more details see 'mock4net/LICENSE.txt' and 'mock4net/readme.md' in this project root.
using System;
using System.Collections.Generic;
using System.Net;
using Stef.Validation;
using WireMock.Matchers.Request;
using WireMock.Models;
using WireMock.ResponseBuilders;
using WireMock.ResponseProviders;
using WireMock.Settings;
using WireMock.Types;
@@ -73,10 +75,7 @@ internal class RespondWithAProvider : IRespondWithAProvider
Guid = guidUtils.NewGuid();
}
/// <summary>
/// The respond with.
/// </summary>
/// <param name="provider">The provider.</param>
/// <inheritdoc />
public void RespondWith(IResponseProvider provider)
{
var mapping = new Mapping
@@ -113,6 +112,48 @@ internal class RespondWithAProvider : IRespondWithAProvider
_registrationCallback(mapping, _saveToFile);
}
/// <inheritdoc />
public void ThenRespondWith(Action<IResponseBuilder> action)
{
var responseBuilder = Response.Create();
action(responseBuilder);
RespondWith(responseBuilder);
}
/// <inheritdoc />
public void ThenRespondWithOK()
{
var responseBuilder = Response.Create();
RespondWith(responseBuilder);
}
/// <inheritdoc />
public void ThenRespondWithStatusCode(int code)
{
var responseBuilder = Response.Create().WithStatusCode(code);
RespondWith(responseBuilder);
}
/// <inheritdoc />
public void ThenRespondWithStatusCode(string code)
{
var responseBuilder = Response.Create().WithStatusCode(code);
RespondWith(responseBuilder);
}
/// <inheritdoc />
public void ThenRespondWithStatusCode(HttpStatusCode code)
{
var responseBuilder = Response.Create().WithStatusCode(code);
RespondWith(responseBuilder);
}
/// <inheritdoc />
public IRespondWithAProvider WithData(object data)
{
@@ -133,6 +174,18 @@ internal class RespondWithAProvider : IRespondWithAProvider
return this;
}
/// <inheritdoc />
public IRespondWithAProvider DefineGuid(Guid guid)
{
return WithGuid(guid);
}
/// <inheritdoc />
public IRespondWithAProvider DefineGuid(string guid)
{
return WithGuid(guid);
}
/// <inheritdoc />
public IRespondWithAProvider WithTitle(string title)
{
@@ -148,7 +201,7 @@ internal class RespondWithAProvider : IRespondWithAProvider
return this;
}
/// <see cref="IRespondWithAProvider.WithPath"/>
/// <inheritdoc />
public IRespondWithAProvider WithPath(string path)
{
_path = path;

View File

@@ -0,0 +1,42 @@
// Copyright © WireMock.Net
using System;
using JetBrains.Annotations;
using Stef.Validation;
using WireMock.Matchers.Request;
using WireMock.RequestBuilders;
namespace WireMock.Server;
public partial class WireMockServer
{
/// <summary>
/// Given
/// </summary>
/// <param name="requestMatcher">The request matcher.</param>
/// <param name="saveToFile">Optional boolean to indicate if this mapping should be saved as static mapping file.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
[PublicAPI]
public IRespondWithAProvider Given(IRequestMatcher requestMatcher, bool saveToFile = false)
{
return _mappingBuilder.Given(requestMatcher, saveToFile);
}
/// <summary>
/// WhenRequest
/// </summary>
/// <param name="action">The action to use the fluent <see cref="IRequestBuilder"/>.</param>
/// <param name="saveToFile">Optional boolean to indicate if this mapping should be saved as static mapping file.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
[PublicAPI]
public IRespondWithAProvider WhenRequest(Action<IRequestBuilder> action, bool saveToFile = false)
{
Guard.NotNull(action);
var requestBuilder = Request.Create();
action(requestBuilder);
return Given(requestBuilder, saveToFile);
}
}

View File

@@ -588,18 +588,6 @@ public partial class WireMockServer : IWireMockServer
return this;
}
/// <summary>
/// The given.
/// </summary>
/// <param name="requestMatcher">The request matcher.</param>
/// <param name="saveToFile">Optional boolean to indicate if this mapping should be saved as static mapping file.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
[PublicAPI]
public IRespondWithAProvider Given(IRequestMatcher requestMatcher, bool saveToFile = false)
{
return _mappingBuilder.Given(requestMatcher, saveToFile);
}
/// <summary>
/// Add a Grpc ProtoDefinition at server-level.
/// </summary>