How to best leverage WireMock #368

Closed
opened 2025-12-29 08:27:03 +01:00 by adam · 7 comments
Owner

Originally created by @The-Futurist on GitHub (Oct 9, 2021).

We use this REST service - Valence, here's a typical endpoint we use.

To do this I created (several years ago) a T4 based solution, with this I can define an endpoint in XML and the T4 code generator generates classes and methods that expose the endpoint as a pure .Net method.

This approach works extremely well and enables us to add definitions for endpoints that we've not yet used, takes less than 15 minutes to add a new XML definition and rebuild the code.

Most of the JSON data structures (there are a great many) exist as concrete .Net classes - e.g. this for example - in the solution so the developer sees a strongly typed model of the entire API.

So that's the background, I want to be able to change the code so that we can capture outbound requests and inbound responses and devise a way to have the API operate from this captured data, I'm not sure how I can leverage WireMock for this of even if that is a good use case for it.

The .Net API is a .Net Standard 2.0 nuget package at this stage so there's no need for any legacy or .Net Framework stuff.

The actual network send/recv is done using RestSharp and is not particularly complex.

Any thoughts, guidance much appreciated!

Thanks

Originally created by @The-Futurist on GitHub (Oct 9, 2021). We use this REST service - [Valence](https://docs.valence.desire2learn.com/reference.html), here's a typical [endpoint ](https://docs.valence.desire2learn.com/res/course.html#get--d2l-api-lp-(version)-courses-(orgUnitId))we use. To do this I created (several years ago) a T4 based solution, with this I can define an endpoint in XML and the T4 code generator generates classes and methods that expose the endpoint as a pure .Net method. This approach works extremely well and enables us to add definitions for endpoints that we've not yet used, takes less than 15 minutes to add a new XML definition and rebuild the code. Most of the JSON data structures (there are a great many) exist as concrete .Net classes - e.g. [this for example](https://docs.valence.desire2learn.com/res/course.html#Course.CourseOffering) - in the solution so the developer sees a strongly typed model of the entire API. So that's the background, I want to be able to change the code so that we can capture outbound requests and inbound responses and devise a way to have the API operate from this captured data, I'm not sure how I can leverage WireMock for this of even if that is a good use case for it. The .Net API is a .Net Standard 2.0 nuget package at this stage so there's no need for any legacy or .Net Framework stuff. The actual network send/recv is done using RestSharp and is not particularly complex. Any thoughts, guidance much appreciated! Thanks
adam added the question label 2025-12-29 08:27:03 +01:00
adam closed this issue 2025-12-29 08:27:03 +01:00
Author
Owner

@StefH commented on GitHub (Oct 10, 2021):

Good morning @Korporal ,

I've trying to understand what you want.

It is something like:

  • RestClient (RestSharp) using C# classes to get/post to Valence

And now you want something like?

Client <--> WireMock.Net <--> Valence

(If possible provide an example project or make a picture of the flow.)

@StefH commented on GitHub (Oct 10, 2021): Good morning @Korporal , I've trying to understand what you want. It is something like: - RestClient (RestSharp) using C# classes to get/post to Valence And now you want something like? ``` Client <--> WireMock.Net <--> Valence ``` (If possible provide an example project or make a picture of the flow.)
Author
Owner

@The-Futurist commented on GitHub (Oct 10, 2021):

@StefH - To soon for an code as such, also I'm a bit of a novice with respect to mocking which is part of the reason for the question.

I'd like to modify the Valence API so it can run against the "real" internet as it does now, but also against a "simulation" or "mock" of the service, so that we can issues data requests but have the data pulled from a log.

So instead of HTTP requests going to the internet to their remote service, they instead get intercept and the response is pulled from a log.

I say log because I can imagine running a test against the real service and logging what goes out and what comes back, for example an invocation of this endpoint will see a response that contains this data:

{
    "OrgId": <number:D2LID>,
    "UserId": <number:D2LID>,
    "FirstName": <string>,
    "MiddleName": <string>|null,
    "LastName": <string>,
    "UserName": <string>,
    "ExternalEmail": <string>|null,
    "OrgDefinedId": <string>|null,
    "UniqueIdentifier": <string>,
    "Activation": { <composite:User.UserActivationData> },
    "LastAccessedDate": <string:UTCDateTime>|null,  // Added as of LMS v20.20.3
    "Pronouns": <string>  // Added with LP API v1.33
}

this data (what is sent and it's response) could be logged and that log later used by something - Wiremock? - to make it look as if this was a genuine send/response.

So I'm not clear to what extent Wiremock can be used here, in what way, I have no idea how the "logging" (aka "recording") of request/response could be done or whether Wiremock can do that or how Wiremock could leverage said log later.

For all I know I've totally misunderstood what Wiremock is or the way I'm even envisaging this is all wrong!

@The-Futurist commented on GitHub (Oct 10, 2021): @StefH - To soon for an code as such, also I'm a bit of a novice with respect to mocking which is part of the reason for the question. I'd like to modify the Valence API so it can run against the "real" internet as it does now, but also against a "simulation" or "mock" of the service, so that we can issues data requests but have the data pulled from a log. So instead of HTTP requests going to the internet to their remote service, they instead get intercept and the response is pulled from a log. I say log because I can imagine running a test against the real service and logging what goes out and what comes back, for example an invocation of this [endpoint](https://docs.valence.desire2learn.com/res/user.html#get--d2l-api-lp-(version)-users-(userId)) will see a response that contains this data: ```xml { "OrgId": <number:D2LID>, "UserId": <number:D2LID>, "FirstName": <string>, "MiddleName": <string>|null, "LastName": <string>, "UserName": <string>, "ExternalEmail": <string>|null, "OrgDefinedId": <string>|null, "UniqueIdentifier": <string>, "Activation": { <composite:User.UserActivationData> }, "LastAccessedDate": <string:UTCDateTime>|null, // Added as of LMS v20.20.3 "Pronouns": <string> // Added with LP API v1.33 } ``` this data (what is sent and it's response) could be logged and that log later used by something - Wiremock? - to make it look as if this was a genuine send/response. So I'm not clear to what extent Wiremock can be used here, in what way, I have no idea how the "logging" (aka "recording") of request/response could be done or whether Wiremock can do that or how Wiremock could leverage said log later. For all I know I've totally misunderstood what Wiremock is or the way I'm even envisaging this is all wrong!
Author
Owner

@The-Futurist commented on GitHub (Oct 10, 2021):

Here's a crude representation of what I thinking:

Untitled Document

@The-Futurist commented on GitHub (Oct 10, 2021): Here's a crude representation of what I thinking: ![Untitled Document](https://user-images.githubusercontent.com/12262952/136705764-e95ac5ff-c03b-42b3-b66b-d51fa1c6192a.png)
Author
Owner

@The-Futurist commented on GitHub (Oct 10, 2021):

The Valence class library is our code, a .Net standard library, it uses RestSharp. I envisage some mechanism that can intercept the traffic and log to a file, invisible to the class library to all intents and purposes.

The diagram is intended to show that the Valence Class Library can interact either with "Logging, Internet" or with "Mocked Service/Log".

Then I envisage being able to run the same app which calls into the Valence class library as before but this time the class library (perhaps via Wiremock) is not interacting with real internet but with log/Wiremock and although it issues a GET that becomes a lookup into the log and the canned response is returned, again perhaps this can even be done without the Valence class library "knowing"...

As I mentioned I am a novice with mocking so if some of what I' envisaging seems silly that's the reason!

@The-Futurist commented on GitHub (Oct 10, 2021): The Valence class library is our code, a .Net standard library, it uses RestSharp. I envisage some mechanism that can intercept the traffic and log to a file, invisible to the class library to all intents and purposes. The diagram is intended to show that the Valence Class Library can interact either with "Logging, Internet" _**or**_ with "Mocked Service/Log". Then I envisage being able to run the same app which calls into the Valence class library as before but this time the class library (perhaps via Wiremock) is not interacting with real internet but with log/Wiremock and although it issues a GET that becomes a lookup into the log and the canned response is returned, again perhaps this can even be done without the Valence class library "knowing"... As I mentioned I am a novice with mocking so if some of what I' envisaging seems silly that's the reason!
Author
Owner

@StefH commented on GitHub (Oct 11, 2021):

Hello @Korporal ,

This is possible, I've change you picture to make it more compliant to the terms used within WireMock.Net:
image

Switching between the two modes (1 and 2) is just a matter of using a different endpoint or port.

@StefH commented on GitHub (Oct 11, 2021): Hello @Korporal , This is possible, I've change you picture to make it more compliant to the terms used within WireMock.Net: ![image](https://user-images.githubusercontent.com/249938/136744350-40283993-9291-470e-897a-833023c44276.png) Switching between the two modes (1 and 2) is just a matter of using a different endpoint or port.
Author
Owner

@StefH commented on GitHub (Oct 21, 2021):

Hello @Korporal, does this answer your question?

@StefH commented on GitHub (Oct 21, 2021): Hello @Korporal, does this answer your question?
Author
Owner

@StefH commented on GitHub (Oct 27, 2021):

@Korporal
I'll close this now.
If you have still a question, just reply here and I can reopen this.

@StefH commented on GitHub (Oct 27, 2021): @Korporal I'll close this now. If you have still a question, just reply here and I can reopen this.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#368