[PR #49] [CLOSED] stateful behavior #747

Closed
opened 2025-12-29 15:33:01 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/wiremock/WireMock.Net/pull/49
Author: @dmtrrk
Created: 10/4/2017
Status: Closed

Base: masterHead: stateful-behavior


📝 Commits (3)

  • ab017be stateful behavior + tests
  • 22248c0 do not change state if WillSetStateTo is not defined
  • 8da005c the next state can not be null

📊 Changes

8 files changed (+233 additions, -3 deletions)

View changed files

📝 src/WireMock.Net/Mapping.cs (+28 -0)
📝 src/WireMock.Net/Owin/WireMockMiddleware.cs (+7 -0)
📝 src/WireMock.Net/Properties/AssemblyInfo.cs (+2 -1)
📝 src/WireMock.Net/Server/IRespondWithAProvider.cs (+14 -0)
📝 src/WireMock.Net/Server/RespondWithAProvider.cs (+20 -1)
📝 test/WireMock.Net.Tests/FluentMockServerTests.cs (+1 -1)
test/WireMock.Net.Tests/StatefulBehaviorTests.cs (+112 -0)
test/WireMock.Net.Tests/WireMockMiddlewareTests.cs (+49 -0)

📄 Description

This PR adds stateful behavior similar to wiremock.
By default there are no changes in semantic. All previous code will work without any changes.
There are 2 methods added in IRespondWithAProvider interface:
IRespondWithAProvider WhenStateIs(object state); - defined mapping will be processed only in case the current state is equals to specified.
IRespondWithAProvider WillSetStateTo(object state); - In case the concrete mapping was selected and executed with the success, the current state will be changed to the specified.

All states are per server. The default state is null so for the entry request it is not required to specify WhenStateIs.
Both methods can be used together or separately.
Dynamic typing is used over generics, so state is of object type.

Usage example:

  1. This mock will be executed only for the default state (as entry point).
    Once this mapping is executed, the state will be changed to Test state, so it will not be executed the next time.
        _server
            .Given(Request.Create()
                .WithPath("/foo")
                .UsingGet())
            .WillSetStateTo("Test state")
            .RespondWith(Response.Create()
                .WithStatusCode(200)
                .WithBody(@"No state msg"));
  1. This mapping will be executed when the current state is Test state.
        _server
            .Given(Request.Create()
                .WithPath("/foo")
                .UsingGet())
            .WhenStateIs("Test state")
            .RespondWith(Response.Create()
                .WithStatusCode(200)
                .WithBody(@"Test state msg"));

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/wiremock/WireMock.Net/pull/49 **Author:** [@dmtrrk](https://github.com/dmtrrk) **Created:** 10/4/2017 **Status:** ❌ Closed **Base:** `master` ← **Head:** `stateful-behavior` --- ### 📝 Commits (3) - [`ab017be`](https://github.com/wiremock/WireMock.Net/commit/ab017beaa611eba64861decadbb4a6ae10a196ac) stateful behavior + tests - [`22248c0`](https://github.com/wiremock/WireMock.Net/commit/22248c02330c4c279714a3d533aaaaf963a5fe9b) do not change state if WillSetStateTo is not defined - [`8da005c`](https://github.com/wiremock/WireMock.Net/commit/8da005c5ba09530f5cbd8eb10e2012602518b47c) the next state can not be null ### 📊 Changes **8 files changed** (+233 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `src/WireMock.Net/Mapping.cs` (+28 -0) 📝 `src/WireMock.Net/Owin/WireMockMiddleware.cs` (+7 -0) 📝 `src/WireMock.Net/Properties/AssemblyInfo.cs` (+2 -1) 📝 `src/WireMock.Net/Server/IRespondWithAProvider.cs` (+14 -0) 📝 `src/WireMock.Net/Server/RespondWithAProvider.cs` (+20 -1) 📝 `test/WireMock.Net.Tests/FluentMockServerTests.cs` (+1 -1) ➕ `test/WireMock.Net.Tests/StatefulBehaviorTests.cs` (+112 -0) ➕ `test/WireMock.Net.Tests/WireMockMiddlewareTests.cs` (+49 -0) </details> ### 📄 Description This PR adds stateful behavior similar to [wiremock](http://wiremock.org/docs/stateful-behaviour/). By default there are no changes in semantic. All previous code will work without any changes. There are 2 methods added in `IRespondWithAProvider` interface: `IRespondWithAProvider WhenStateIs(object state);` - defined mapping will be processed only in case the current state is equals to specified. `IRespondWithAProvider WillSetStateTo(object state);` - In case the concrete mapping was selected and executed with the success, the current state will be changed to the specified. All states are per server. The default state is `null` so for the entry request it is not required to specify `WhenStateIs`. Both methods can be used together or separately. Dynamic typing is used over generics, so state is of `object` type. Usage example: 1. This mock will be executed only for the default state (as entry point). Once this mapping is executed, the state will be changed to `Test state`, so it will not be executed the next time. > _server > .Given(Request.Create() > .WithPath("/foo") > .UsingGet()) > .WillSetStateTo("Test state") > .RespondWith(Response.Create() > .WithStatusCode(200) > .WithBody(@"No state msg")); 2. This mapping will be executed when the current state is `Test state`. > _server > .Given(Request.Create() > .WithPath("/foo") > .UsingGet()) > .WhenStateIs("Test state") > .RespondWith(Response.Create() > .WithStatusCode(200) > .WithBody(@"Test state msg")); --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-29 15:33:01 +01:00
adam closed this issue 2025-12-29 15:33:01 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#747