diff --git a/WireMock.Net Solution.sln.DotSettings b/WireMock.Net Solution.sln.DotSettings index 95eb13a0..a76391a1 100644 --- a/WireMock.Net Solution.sln.DotSettings +++ b/WireMock.Net Solution.sln.DotSettings @@ -9,6 +9,7 @@ ID IP MD5 + OK OPTIONS OS PATCH diff --git a/src/WireMock.Net/Server/IRespondWithAProvider.cs b/src/WireMock.Net/Server/IRespondWithAProvider.cs index e2b57a37..c0067a3d 100644 --- a/src/WireMock.Net/Server/IRespondWithAProvider.cs +++ b/src/WireMock.Net/Server/IRespondWithAProvider.cs @@ -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 /// The . IRespondWithAProvider WithGuid(Guid guid); + /// + /// Define a unique identifier for this mapping. + /// + /// The unique identifier. + /// The . + IRespondWithAProvider WithGuid(string guid); + + /// + /// Define a unique identifier for this mapping. + /// + /// The unique identifier. + /// The . + IRespondWithAProvider DefineGuid(Guid guid); + + /// + /// Define a unique identifier for this mapping. + /// + /// The unique identifier. + /// The . + IRespondWithAProvider DefineGuid(string guid); + /// /// Define the TimeSettings for this mapping. /// @@ -53,13 +77,6 @@ public interface IRespondWithAProvider /// The . IRespondWithAProvider WithPath(string path); - /// - /// Define a unique identifier for this mapping. - /// - /// The unique identifier. - /// The . - IRespondWithAProvider WithGuid(string guid); - /// /// Define the priority for this mapping. /// @@ -68,11 +85,43 @@ public interface IRespondWithAProvider IRespondWithAProvider AtPriority(int priority); /// - /// The respond with. + /// RespondWith /// /// The provider. void RespondWith(IResponseProvider provider); + /// + /// RespondWith + /// + /// The action to use the fluent . + void ThenRespondWith(Action action); + + /// + /// RespondWith a status code 200 (OK); + /// + void ThenRespondWithOK(); + + /// + /// RespondWith a status code. + /// By default all status codes are allowed, to change this behaviour, see . + /// + /// The code. + void ThenRespondWithStatusCode(int code); + + /// + /// RespondWith a status code. + /// By default all status codes are allowed, to change this behaviour, see . + /// + /// The code. + void ThenRespondWithStatusCode(string code); + + /// + /// RespondWith a status code. + /// By default all status codes are allowed, to change this behaviour, see . + /// + /// The code. + void ThenRespondWithStatusCode(HttpStatusCode code); + /// /// Sets the the scenario. /// diff --git a/src/WireMock.Net/Server/RespondWithAProvider.cs b/src/WireMock.Net/Server/RespondWithAProvider.cs index c10e50bc..8d82057d 100644 --- a/src/WireMock.Net/Server/RespondWithAProvider.cs +++ b/src/WireMock.Net/Server/RespondWithAProvider.cs @@ -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 @@ public RespondWithAProvider( Guid = guidUtils.NewGuid(); } - /// - /// The respond with. - /// - /// The provider. + /// public void RespondWith(IResponseProvider provider) { var mapping = new Mapping @@ -113,6 +112,48 @@ public void RespondWith(IResponseProvider provider) _registrationCallback(mapping, _saveToFile); } + /// + public void ThenRespondWith(Action action) + { + var responseBuilder = Response.Create(); + + action(responseBuilder); + + RespondWith(responseBuilder); + } + + /// + public void ThenRespondWithOK() + { + var responseBuilder = Response.Create(); + + RespondWith(responseBuilder); + } + + /// + public void ThenRespondWithStatusCode(int code) + { + var responseBuilder = Response.Create().WithStatusCode(code); + + RespondWith(responseBuilder); + } + + /// + public void ThenRespondWithStatusCode(string code) + { + var responseBuilder = Response.Create().WithStatusCode(code); + + RespondWith(responseBuilder); + } + + /// + public void ThenRespondWithStatusCode(HttpStatusCode code) + { + var responseBuilder = Response.Create().WithStatusCode(code); + + RespondWith(responseBuilder); + } + /// public IRespondWithAProvider WithData(object data) { @@ -133,6 +174,18 @@ public IRespondWithAProvider WithGuid(Guid guid) return this; } + /// + public IRespondWithAProvider DefineGuid(Guid guid) + { + return WithGuid(guid); + } + + /// + public IRespondWithAProvider DefineGuid(string guid) + { + return WithGuid(guid); + } + /// public IRespondWithAProvider WithTitle(string title) { @@ -148,7 +201,7 @@ public IRespondWithAProvider WithDescription(string description) return this; } - /// + /// public IRespondWithAProvider WithPath(string path) { _path = path; diff --git a/src/WireMock.Net/Server/WireMockServer.Fluent.cs b/src/WireMock.Net/Server/WireMockServer.Fluent.cs new file mode 100644 index 00000000..a253c871 --- /dev/null +++ b/src/WireMock.Net/Server/WireMockServer.Fluent.cs @@ -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 +{ + /// + /// Given + /// + /// The request matcher. + /// Optional boolean to indicate if this mapping should be saved as static mapping file. + /// The . + [PublicAPI] + public IRespondWithAProvider Given(IRequestMatcher requestMatcher, bool saveToFile = false) + { + return _mappingBuilder.Given(requestMatcher, saveToFile); + } + + /// + /// WhenRequest + /// + /// The action to use the fluent . + /// Optional boolean to indicate if this mapping should be saved as static mapping file. + /// The . + [PublicAPI] + public IRespondWithAProvider WhenRequest(Action action, bool saveToFile = false) + { + Guard.NotNull(action); + + var requestBuilder = Request.Create(); + + action(requestBuilder); + + return Given(requestBuilder, saveToFile); + } +} \ No newline at end of file diff --git a/src/WireMock.Net/Server/WireMockServer.cs b/src/WireMock.Net/Server/WireMockServer.cs index 12876dee..a9963c9a 100644 --- a/src/WireMock.Net/Server/WireMockServer.cs +++ b/src/WireMock.Net/Server/WireMockServer.cs @@ -588,18 +588,6 @@ public IWireMockServer WithMapping(string mappings) return this; } - /// - /// The given. - /// - /// The request matcher. - /// Optional boolean to indicate if this mapping should be saved as static mapping file. - /// The . - [PublicAPI] - public IRespondWithAProvider Given(IRequestMatcher requestMatcher, bool saveToFile = false) - { - return _mappingBuilder.Given(requestMatcher, saveToFile); - } - /// /// Add a Grpc ProtoDefinition at server-level. /// diff --git a/test/WireMock.Net.Tests/WireMockServerTests.WithParam.cs b/test/WireMock.Net.Tests/WireMockServerTests.WithParam.cs index 91f006e6..0a7ed22d 100644 --- a/test/WireMock.Net.Tests/WireMockServerTests.WithParam.cs +++ b/test/WireMock.Net.Tests/WireMockServerTests.WithParam.cs @@ -28,14 +28,14 @@ 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 @@ -43,7 +43,7 @@ public async Task WireMockServer_WithParam_QueryParameterMultipleValueSupport_No var response = await server.CreateClient().GetAsync(requestUri).ConfigureAwait(false); // Assert - response.StatusCode.Should().Be(HttpStatusCode.OK); + response.StatusCode.Should().Be(HttpStatusCode.Accepted); server.Stop(); } @@ -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}"); @@ -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§ions=personal%2Corganizations%2Cemployment"); @@ -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§ions=personal%2Corganizations%2Cemployment");