Skip to content

Commit 69c829f

Browse files
authored
Update + add fluent builder methods (#1042)
* Update some fluent builder methods * fix * sc * ThenRespondWith * // Copyright © WireMock.Net
1 parent 275816c commit 69c829f

6 files changed

+168
-41
lines changed

WireMock.Net Solution.sln.DotSettings

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=ID/@EntryIndexedValue">ID</s:String>
1010
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=IP/@EntryIndexedValue">IP</s:String>
1111
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=MD/@EntryIndexedValue">MD5</s:String>
12+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=OK/@EntryIndexedValue">OK</s:String>
1213
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=OPTIONS/@EntryIndexedValue">OPTIONS</s:String>
1314
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=OS/@EntryIndexedValue">OS</s:String>
1415
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=PATCH/@EntryIndexedValue">PATCH</s:String>

src/WireMock.Net/Server/IRespondWithAProvider.cs

+57-8
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
using System;
44
using System.Collections.Generic;
5+
using System.Net;
56
using WireMock.Models;
7+
using WireMock.ResponseBuilders;
68
using WireMock.ResponseProviders;
9+
using WireMock.Settings;
710
using WireMock.Types;
811

912
namespace WireMock.Server;
@@ -25,6 +28,27 @@ public interface IRespondWithAProvider
2528
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
2629
IRespondWithAProvider WithGuid(Guid guid);
2730

31+
/// <summary>
32+
/// Define a unique identifier for this mapping.
33+
/// </summary>
34+
/// <param name="guid">The unique identifier.</param>
35+
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
36+
IRespondWithAProvider WithGuid(string guid);
37+
38+
/// <summary>
39+
/// Define a unique identifier for this mapping.
40+
/// </summary>
41+
/// <param name="guid">The unique identifier.</param>
42+
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
43+
IRespondWithAProvider DefineGuid(Guid guid);
44+
45+
/// <summary>
46+
/// Define a unique identifier for this mapping.
47+
/// </summary>
48+
/// <param name="guid">The unique identifier.</param>
49+
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
50+
IRespondWithAProvider DefineGuid(string guid);
51+
2852
/// <summary>
2953
/// Define the TimeSettings for this mapping.
3054
/// </summary>
@@ -53,13 +77,6 @@ public interface IRespondWithAProvider
5377
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
5478
IRespondWithAProvider WithPath(string path);
5579

56-
/// <summary>
57-
/// Define a unique identifier for this mapping.
58-
/// </summary>
59-
/// <param name="guid">The unique identifier.</param>
60-
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
61-
IRespondWithAProvider WithGuid(string guid);
62-
6380
/// <summary>
6481
/// Define the priority for this mapping.
6582
/// </summary>
@@ -68,11 +85,43 @@ public interface IRespondWithAProvider
6885
IRespondWithAProvider AtPriority(int priority);
6986

7087
/// <summary>
71-
/// The respond with.
88+
/// RespondWith
7289
/// </summary>
7390
/// <param name="provider">The provider.</param>
7491
void RespondWith(IResponseProvider provider);
7592

93+
/// <summary>
94+
/// RespondWith
95+
/// </summary>
96+
/// <param name="action">The action to use the fluent <see cref="IResponseBuilder"/>.</param>
97+
void ThenRespondWith(Action<IResponseBuilder> action);
98+
99+
/// <summary>
100+
/// RespondWith a status code 200 (OK);
101+
/// </summary>
102+
void ThenRespondWithOK();
103+
104+
/// <summary>
105+
/// RespondWith a status code.
106+
/// By default all status codes are allowed, to change this behaviour, see <inheritdoc cref="WireMockServerSettings.AllowOnlyDefinedHttpStatusCodeInResponse"/>.
107+
/// </summary>
108+
/// <param name="code">The code.</param>
109+
void ThenRespondWithStatusCode(int code);
110+
111+
/// <summary>
112+
/// RespondWith a status code.
113+
/// By default all status codes are allowed, to change this behaviour, see <inheritdoc cref="WireMockServerSettings.AllowOnlyDefinedHttpStatusCodeInResponse"/>.
114+
/// </summary>
115+
/// <param name="code">The code.</param>
116+
void ThenRespondWithStatusCode(string code);
117+
118+
/// <summary>
119+
/// RespondWith a status code.
120+
/// By default all status codes are allowed, to change this behaviour, see <inheritdoc cref="WireMockServerSettings.AllowOnlyDefinedHttpStatusCodeInResponse"/>.
121+
/// </summary>
122+
/// <param name="code">The code.</param>
123+
void ThenRespondWithStatusCode(HttpStatusCode code);
124+
76125
/// <summary>
77126
/// Sets the the scenario.
78127
/// </summary>

src/WireMock.Net/Server/RespondWithAProvider.cs

+58-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
// For more details see 'mock4net/LICENSE.txt' and 'mock4net/readme.md' in this project root.
55
using System;
66
using System.Collections.Generic;
7+
using System.Net;
78
using Stef.Validation;
89
using WireMock.Matchers.Request;
910
using WireMock.Models;
11+
using WireMock.ResponseBuilders;
1012
using WireMock.ResponseProviders;
1113
using WireMock.Settings;
1214
using WireMock.Types;
@@ -73,10 +75,7 @@ public RespondWithAProvider(
7375
Guid = guidUtils.NewGuid();
7476
}
7577

76-
/// <summary>
77-
/// The respond with.
78-
/// </summary>
79-
/// <param name="provider">The provider.</param>
78+
/// <inheritdoc />
8079
public void RespondWith(IResponseProvider provider)
8180
{
8281
var mapping = new Mapping
@@ -113,6 +112,48 @@ public void RespondWith(IResponseProvider provider)
113112
_registrationCallback(mapping, _saveToFile);
114113
}
115114

115+
/// <inheritdoc />
116+
public void ThenRespondWith(Action<IResponseBuilder> action)
117+
{
118+
var responseBuilder = Response.Create();
119+
120+
action(responseBuilder);
121+
122+
RespondWith(responseBuilder);
123+
}
124+
125+
/// <inheritdoc />
126+
public void ThenRespondWithOK()
127+
{
128+
var responseBuilder = Response.Create();
129+
130+
RespondWith(responseBuilder);
131+
}
132+
133+
/// <inheritdoc />
134+
public void ThenRespondWithStatusCode(int code)
135+
{
136+
var responseBuilder = Response.Create().WithStatusCode(code);
137+
138+
RespondWith(responseBuilder);
139+
}
140+
141+
/// <inheritdoc />
142+
public void ThenRespondWithStatusCode(string code)
143+
{
144+
var responseBuilder = Response.Create().WithStatusCode(code);
145+
146+
RespondWith(responseBuilder);
147+
}
148+
149+
/// <inheritdoc />
150+
public void ThenRespondWithStatusCode(HttpStatusCode code)
151+
{
152+
var responseBuilder = Response.Create().WithStatusCode(code);
153+
154+
RespondWith(responseBuilder);
155+
}
156+
116157
/// <inheritdoc />
117158
public IRespondWithAProvider WithData(object data)
118159
{
@@ -133,6 +174,18 @@ public IRespondWithAProvider WithGuid(Guid guid)
133174
return this;
134175
}
135176

177+
/// <inheritdoc />
178+
public IRespondWithAProvider DefineGuid(Guid guid)
179+
{
180+
return WithGuid(guid);
181+
}
182+
183+
/// <inheritdoc />
184+
public IRespondWithAProvider DefineGuid(string guid)
185+
{
186+
return WithGuid(guid);
187+
}
188+
136189
/// <inheritdoc />
137190
public IRespondWithAProvider WithTitle(string title)
138191
{
@@ -148,7 +201,7 @@ public IRespondWithAProvider WithDescription(string description)
148201
return this;
149202
}
150203

151-
/// <see cref="IRespondWithAProvider.WithPath"/>
204+
/// <inheritdoc />
152205
public IRespondWithAProvider WithPath(string path)
153206
{
154207
_path = path;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright © WireMock.Net
2+
3+
using System;
4+
using JetBrains.Annotations;
5+
using Stef.Validation;
6+
using WireMock.Matchers.Request;
7+
using WireMock.RequestBuilders;
8+
9+
namespace WireMock.Server;
10+
11+
public partial class WireMockServer
12+
{
13+
/// <summary>
14+
/// Given
15+
/// </summary>
16+
/// <param name="requestMatcher">The request matcher.</param>
17+
/// <param name="saveToFile">Optional boolean to indicate if this mapping should be saved as static mapping file.</param>
18+
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
19+
[PublicAPI]
20+
public IRespondWithAProvider Given(IRequestMatcher requestMatcher, bool saveToFile = false)
21+
{
22+
return _mappingBuilder.Given(requestMatcher, saveToFile);
23+
}
24+
25+
/// <summary>
26+
/// WhenRequest
27+
/// </summary>
28+
/// <param name="action">The action to use the fluent <see cref="IRequestBuilder"/>.</param>
29+
/// <param name="saveToFile">Optional boolean to indicate if this mapping should be saved as static mapping file.</param>
30+
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
31+
[PublicAPI]
32+
public IRespondWithAProvider WhenRequest(Action<IRequestBuilder> action, bool saveToFile = false)
33+
{
34+
Guard.NotNull(action);
35+
36+
var requestBuilder = Request.Create();
37+
38+
action(requestBuilder);
39+
40+
return Given(requestBuilder, saveToFile);
41+
}
42+
}

src/WireMock.Net/Server/WireMockServer.cs

-12
Original file line numberDiff line numberDiff line change
@@ -588,18 +588,6 @@ public IWireMockServer WithMapping(string mappings)
588588
return this;
589589
}
590590

591-
/// <summary>
592-
/// The given.
593-
/// </summary>
594-
/// <param name="requestMatcher">The request matcher.</param>
595-
/// <param name="saveToFile">Optional boolean to indicate if this mapping should be saved as static mapping file.</param>
596-
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
597-
[PublicAPI]
598-
public IRespondWithAProvider Given(IRequestMatcher requestMatcher, bool saveToFile = false)
599-
{
600-
return _mappingBuilder.Given(requestMatcher, saveToFile);
601-
}
602-
603591
/// <summary>
604592
/// Add a Grpc ProtoDefinition at server-level.
605593
/// </summary>

test/WireMock.Net.Tests/WireMockServerTests.WithParam.cs

+10-16
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,22 @@ public async Task WireMockServer_WithParam_QueryParameterMultipleValueSupport_No
2828
QueryParameterMultipleValueSupport = QueryParameterMultipleValueSupport.NoComma
2929
};
3030
var server = WireMockServer.Start(settings);
31-
server.Given(
32-
Request.Create()
31+
server
32+
.WhenRequest(r => r
3333
.UsingGet()
3434
.WithPath("/foo")
3535
.WithParam("query", queryValue)
3636
)
37-
.RespondWith(
38-
Response.Create().WithStatusCode(200)
37+
.ThenRespondWith(r => r
38+
.WithStatusCode(HttpStatusCode.Accepted)
3939
);
4040

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

4545
// Assert
46-
response.StatusCode.Should().Be(HttpStatusCode.OK);
46+
response.StatusCode.Should().Be(HttpStatusCode.Accepted);
4747

4848
server.Stop();
4949
}
@@ -54,15 +54,13 @@ public async Task WireMockServer_WithParam_MultiValueComma()
5454
// Arrange
5555
var queryValue = "1,2,3";
5656
var server = WireMockServer.Start();
57-
server.Given(
58-
Request.Create()
57+
server
58+
.WhenRequest(r => r
5959
.UsingGet()
6060
.WithPath("/foo")
6161
.WithParam("query", "1", "2", "3")
6262
)
63-
.RespondWith(
64-
Response.Create().WithStatusCode(200)
65-
);
63+
.ThenRespondWithStatusCode(200);
6664

6765
// Act
6866
var requestUri = new Uri($"http://localhost:{server.Port}/foo?query={queryValue}");
@@ -85,9 +83,7 @@ public async Task WireMockServer_WithParam_RejectOnMatch_OnNonMatchingParam_Shou
8583
.WithParam("delta_from", MatchBehaviour.RejectOnMatch)
8684
.UsingGet()
8785
)
88-
.RespondWith(
89-
Response.Create()
90-
);
86+
.ThenRespondWithOK();
9187

9288
// Act
9389
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 async Task WireMockServer_WithParam_AcceptOnMatch_OnNonMatchingParam_Shou
110106
.WithParam("delta_from")
111107
.UsingGet()
112108
)
113-
.RespondWith(
114-
Response.Create()
115-
);
109+
.ThenRespondWithStatusCode("300");
116110

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

0 commit comments

Comments
 (0)