Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update + add fluent builder methods #1042

Merged
merged 18 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions WireMock.Net Solution.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -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/=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/=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/=OS/@EntryIndexedValue">OS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=PATCH/@EntryIndexedValue">PATCH</s:String>
Expand Down
65 changes: 57 additions & 8 deletions src/WireMock.Net/Server/IRespondWithAProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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>
Expand Down Expand Up @@ -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>
Expand All @@ -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>
Expand Down
63 changes: 58 additions & 5 deletions src/WireMock.Net/Server/RespondWithAProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,10 +75,7 @@ public RespondWithAProvider(
Guid = guidUtils.NewGuid();
}

/// <summary>
/// The respond with.
/// </summary>
/// <param name="provider">The provider.</param>
/// <inheritdoc />
public void RespondWith(IResponseProvider provider)
{
var mapping = new Mapping
Expand Down Expand Up @@ -113,6 +112,48 @@ public void RespondWith(IResponseProvider provider)
_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)
{
Expand All @@ -133,6 +174,18 @@ public IRespondWithAProvider WithGuid(Guid guid)
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)
{
Expand All @@ -148,7 +201,7 @@ public IRespondWithAProvider WithDescription(string description)
return this;
}

/// <see cref="IRespondWithAProvider.WithPath"/>
/// <inheritdoc />
public IRespondWithAProvider WithPath(string path)
{
_path = path;
Expand Down
42 changes: 42 additions & 0 deletions src/WireMock.Net/Server/WireMockServer.Fluent.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
12 changes: 0 additions & 12 deletions src/WireMock.Net/Server/WireMockServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -588,18 +588,6 @@ public IWireMockServer WithMapping(string mappings)
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>
Expand Down
26 changes: 10 additions & 16 deletions test/WireMock.Net.Tests/WireMockServerTests.WithParam.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ public async Task WireMockServer_WithParam_QueryParameterMultipleValueSupport_No
QueryParameterMultipleValueSupport = QueryParameterMultipleValueSupport.NoComma
};
var server = WireMockServer.Start(settings);
server.Given(
Request.Create()
server
.WhenRequest(r => r
.UsingGet()
.WithPath("/foo")
.WithParam("query", queryValue)
)
.RespondWith(
Response.Create().WithStatusCode(200)
.ThenRespondWith(r => r
.WithStatusCode(HttpStatusCode.Accepted)
);

// Act
var requestUri = new Uri($"http://localhost:{server.Port}/foo?query={queryValue}");
var response = await server.CreateClient().GetAsync(requestUri).ConfigureAwait(false);

// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
response.StatusCode.Should().Be(HttpStatusCode.Accepted);

server.Stop();
}
Expand All @@ -54,15 +54,13 @@ public async Task WireMockServer_WithParam_MultiValueComma()
// Arrange
var queryValue = "1,2,3";
var server = WireMockServer.Start();
server.Given(
Request.Create()
server
.WhenRequest(r => r
.UsingGet()
.WithPath("/foo")
.WithParam("query", "1", "2", "3")
)
.RespondWith(
Response.Create().WithStatusCode(200)
);
.ThenRespondWithStatusCode(200);

// Act
var requestUri = new Uri($"http://localhost:{server.Port}/foo?query={queryValue}");
Expand All @@ -85,9 +83,7 @@ public async Task WireMockServer_WithParam_RejectOnMatch_OnNonMatchingParam_Shou
.WithParam("delta_from", MatchBehaviour.RejectOnMatch)
.UsingGet()
)
.RespondWith(
Response.Create()
);
.ThenRespondWithOK();

// Act
var requestUri = new Uri($"http://localhost:{server.Port}/v1/person/workers?showsourcesystem=true&count=700&page=1&sections=personal%2Corganizations%2Cemployment");
Expand All @@ -110,9 +106,7 @@ public async Task WireMockServer_WithParam_AcceptOnMatch_OnNonMatchingParam_Shou
.WithParam("delta_from")
.UsingGet()
)
.RespondWith(
Response.Create()
);
.ThenRespondWithStatusCode("300");

// Act
var requestUri = new Uri($"http://localhost:{server.Port}/v1/person/workers?showsourcesystem=true&count=700&page=1&sections=personal%2Corganizations%2Cemployment");
Expand Down