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

Fluent API for RestClient MappingModel creation #621

Closed
danspark opened this issue Jul 17, 2021 · 6 comments
Closed

Fluent API for RestClient MappingModel creation #621

danspark opened this issue Jul 17, 2021 · 6 comments
Assignees
Labels

Comments

@danspark
Copy link

Is your feature request related to a problem? Please describe.
I'm thinking about using WireMock as a container to run alongside my application during tests, and as far as I searched, looks like I have to create a mapping by instantiating a new MappingModel directly.

Describe the solution you'd like
I think it would be really useful for users to have an experience similar to the fluent API to used to create requests when WireMock is used on the same process. I am no expert on the library but I imagine there would be some limitations, but for simple cases like matching a request and building a response, it would be really useful, since I wouldn't have to learn two ways to configure mappings.

Describe alternatives you've considered
Build the mapping model directly.

Is your feature request supported by WireMock (java version)? Please provide details.
I'm not familiar with the java version, but as far as I researched, I couldn't find anything like that yet.

Additional context
I don't if this is something that would be interesting for the main library or maybe it would be something that the creators would want the community to develop. But if this is something that is planned or wanted, I would be glad to help as I can. But as I said, I'm no expert and I'm not sure if I would be able to cover every possible scenario the current fluent API has.

I'm building a simple example and I'll post here when I make some progress.

@StefH
Copy link
Collaborator

StefH commented Jul 19, 2021

I can build a standard FluentBuilder for all the client model objects, however this builder will be only a simple version.

Like

var matcher = FluentBuilder.MatcherModel()
    .WithName("n")
    .WithPattern("p")
    .Build();

Will this be what you need?

@StefH
Copy link
Collaborator

StefH commented Jul 20, 2021

@danspark

See preview version on MyGet https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions

WireMock.Net.RestClient.1.4.19-preview-01-ci-15231.nupkg

@danspark
Copy link
Author

I can build a standard FluentBuilder for all the client model objects, however this builder will be only a simple version.

Like

var matcher = FluentBuilder.MatcherModel()
    .WithName("n")
    .WithPattern("p")
    .Build();

Will this be what you need?

Yes! That would make the experience similar to the fluent API, I'll take a look soon. Thank you!

@StefH
Copy link
Collaborator

StefH commented Aug 4, 2021

@danspark
Did you have time to take a look yet ?

@danspark
Copy link
Author

danspark commented Aug 4, 2021

Apologies for taking so long, I've been a bit busy. I took a look at the most recent version of the CI build, the API looks a little different, I don't know if it made a lot of difference. My simplest setup looked like this:

			var response = await _api.PostMappingAsync(new MappingModel {
				Request = new RequestModel {
					Url = "*"
				},
				Response = new ResponseModel {
					StatusCode = (int)statusCode
				}
			});

With the modification, it looks like this:

			var requestBuilder = new FluentBuilder.MappingModelBuilder()
				.WithRequest(new RequestModel {
					Path = "*"
				})
				.WithResponse(new ResponseModel {
					StatusCode = (int)statusCode
				}).Build();

Maybe the MappingModelBuilder WithRequest and WithResponse methods could have an overload that asks for an Action<RequestModelBuilder> and Action<ResponseModelBuilder>? That way it could look like this:

			var requestBuilder = new FluentBuilder.MappingModelBuilder()
				.WithRequest(b => b.WithPath("*"))
				.WithResponse(b => b.WithStatusCode((int) statusCode)}).Build();

@StefH StefH self-assigned this Aug 4, 2021
@StefH
Copy link
Collaborator

StefH commented Aug 4, 2021

You can work-around this problem by using a FluentBuilder.ResponseModelBuilder() and then assign the value to WithResponse(...)

To support your scenario, I've to see if I can update the FluentBuilder.
Can you create a new issue here? --> https://github.com/StefH/FluentBuilder

And for now, I'll merge the PR and close this issue.

In case I can implement your scenario in the FluentBuilder project, I'll post an update here and release a new WireMock.Net NuGet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants