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

@@ -9,6 +9,7 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=ID/@EntryIndexedValue">ID</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=ID/@EntryIndexedValue">ID</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=IP/@EntryIndexedValue">IP</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=IP/@EntryIndexedValue">IP</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=MD/@EntryIndexedValue">MD5</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=MD/@EntryIndexedValue">MD5</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=OK/@EntryIndexedValue">OK</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=OPTIONS/@EntryIndexedValue">OPTIONS</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=OPTIONS/@EntryIndexedValue">OPTIONS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=OS/@EntryIndexedValue">OS</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=OS/@EntryIndexedValue">OS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=PATCH/@EntryIndexedValue">PATCH</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=PATCH/@EntryIndexedValue">PATCH</s:String>

View File

@@ -2,8 +2,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net;
using WireMock.Models; using WireMock.Models;
using WireMock.ResponseBuilders;
using WireMock.ResponseProviders; using WireMock.ResponseProviders;
using WireMock.Settings;
using WireMock.Types; using WireMock.Types;
namespace WireMock.Server; namespace WireMock.Server;
@@ -25,6 +28,27 @@ public interface IRespondWithAProvider
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns> /// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WithGuid(Guid guid); 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> /// <summary>
/// Define the TimeSettings for this mapping. /// Define the TimeSettings for this mapping.
/// </summary> /// </summary>
@@ -53,13 +77,6 @@ public interface IRespondWithAProvider
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns> /// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WithPath(string path); 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> /// <summary>
/// Define the priority for this mapping. /// Define the priority for this mapping.
/// </summary> /// </summary>
@@ -68,11 +85,43 @@ public interface IRespondWithAProvider
IRespondWithAProvider AtPriority(int priority); IRespondWithAProvider AtPriority(int priority);
/// <summary> /// <summary>
/// The respond with. /// RespondWith
/// </summary> /// </summary>
/// <param name="provider">The provider.</param> /// <param name="provider">The provider.</param>
void RespondWith(IResponseProvider provider); 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> /// <summary>
/// Sets the the scenario. /// Sets the the scenario.
/// </summary> /// </summary>

View File

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

View File

@@ -28,14 +28,14 @@ public partial class WireMockServerTests
QueryParameterMultipleValueSupport = QueryParameterMultipleValueSupport.NoComma QueryParameterMultipleValueSupport = QueryParameterMultipleValueSupport.NoComma
}; };
var server = WireMockServer.Start(settings); var server = WireMockServer.Start(settings);
server.Given( server
Request.Create() .WhenRequest(r => r
.UsingGet() .UsingGet()
.WithPath("/foo") .WithPath("/foo")
.WithParam("query", queryValue) .WithParam("query", queryValue)
) )
.RespondWith( .ThenRespondWith(r => r
Response.Create().WithStatusCode(200) .WithStatusCode(HttpStatusCode.Accepted)
); );
// Act // Act
@@ -43,7 +43,7 @@ public partial class WireMockServerTests
var response = await server.CreateClient().GetAsync(requestUri).ConfigureAwait(false); var response = await server.CreateClient().GetAsync(requestUri).ConfigureAwait(false);
// Assert // Assert
response.StatusCode.Should().Be(HttpStatusCode.OK); response.StatusCode.Should().Be(HttpStatusCode.Accepted);
server.Stop(); server.Stop();
} }
@@ -54,15 +54,13 @@ public partial class WireMockServerTests
// Arrange // Arrange
var queryValue = "1,2,3"; var queryValue = "1,2,3";
var server = WireMockServer.Start(); var server = WireMockServer.Start();
server.Given( server
Request.Create() .WhenRequest(r => r
.UsingGet() .UsingGet()
.WithPath("/foo") .WithPath("/foo")
.WithParam("query", "1", "2", "3") .WithParam("query", "1", "2", "3")
) )
.RespondWith( .ThenRespondWithStatusCode(200);
Response.Create().WithStatusCode(200)
);
// Act // Act
var requestUri = new Uri($"http://localhost:{server.Port}/foo?query={queryValue}"); var requestUri = new Uri($"http://localhost:{server.Port}/foo?query={queryValue}");
@@ -85,9 +83,7 @@ public partial class WireMockServerTests
.WithParam("delta_from", MatchBehaviour.RejectOnMatch) .WithParam("delta_from", MatchBehaviour.RejectOnMatch)
.UsingGet() .UsingGet()
) )
.RespondWith( .ThenRespondWithOK();
Response.Create()
);
// Act // Act
var requestUri = new Uri($"http://localhost:{server.Port}/v1/person/workers?showsourcesystem=true&count=700&page=1&sections=personal%2Corganizations%2Cemployment"); var requestUri = new Uri($"http://localhost:{server.Port}/v1/person/workers?showsourcesystem=true&count=700&page=1&sections=personal%2Corganizations%2Cemployment");
@@ -110,9 +106,7 @@ public partial class WireMockServerTests
.WithParam("delta_from") .WithParam("delta_from")
.UsingGet() .UsingGet()
) )
.RespondWith( .ThenRespondWithStatusCode("300");
Response.Create()
);
// Act // Act
var requestUri = new Uri($"http://localhost:{server.Port}/v1/person/workers?showsourcesystem=true&count=700&page=1&sections=personal%2Corganizations%2Cemployment"); var requestUri = new Uri($"http://localhost:{server.Port}/v1/person/workers?showsourcesystem=true&count=700&page=1&sections=personal%2Corganizations%2Cemployment");