DavidAJohn / PhotoPortfolio

Personal photo portfolio .NET web application which implements a Blazor and Tailwind CSS user interface with a MongoDb database, and includes integration with Stripe Checkout and Prodigi Print API
0 stars 1 forks source link

WireMock of Prodigi Print API needs improvement #64

Closed DavidAJohn closed 10 months ago

DavidAJohn commented 11 months ago

In the integration tests project, I've created a mock of the Prodigi Print API which is used via a WebApplicationFactory to provide fake responses to both the QuoteService and OrderService tests.

To put it mildly, it's pretty ugly. The request matching is far too specific at the moment and it's making the tests worringly brittle.

I need to go through the WireMock-Net docs again and try to find ways to add wildcards or ignore certain fields somehow.

DavidAJohn commented 11 months ago

I've simplified the Prodigi WireMock implementation by commenting out the request matching of the JSON body, which was way too specific and inflexible. That was done is this commit: 20ff4ea954acc42541f0564b2554b6643fca59f4

Now I'll have to venture into the rabbit hole that is JSONPath to add some level of request matching back in.

In that previous commit I also made a start on some response templating, which involves extracting values from the request and inserting them into the response. That allows the integration tests to check that crucial values like the idempotency key, etc. are being handled as expected.

DavidAJohn commented 11 months ago

This has turned out to be pretty tough going. There's been a lot of trial and error.

I've now switched some of the response templating to use the .WithCallback function, which I can't find any reference to in the docs, but discovered after seeing a reply on StackOverflow from Stef Heyenrath (the creator and maintainer of WireMock.Net). It's marginally less ugly than the previous method I used, in that it doesn't have insane double or triple quotes in verbatim strings everywhere. It's visible in this commit: 7c7a3a0c0df48c1ae7dbea945999cf34a7050ce7

DavidAJohn commented 11 months ago

I've also started to improve the request matching (mostly using JsonPartialWildcardMatcher).

I've really just tried to check that certain properties exist in the JSON body that is supplied.

After a lot of trial and error, I've settled on this method that I read about in the WireMock.Net Issues (again courtesy of Stef), which basically involves creating an array of matchers that get added via the .WithBody section.

For now I've just changed the order created response (in this commit: 503a99c763f01cb12cf845bb94ee3d9f82d3c844), but will obviously change the other responses as well as finding ways to move some of the more verbose code into extensions methods, etc.

DavidAJohn commented 10 months ago

Over a series of updates, I've now massively improved the responses that are returned so that they reflect the request that was sent.

Ultimately, I used the .WithCallback method to construct responses based on the incoming request object and removed all of the static methods containing triple-quoted verbatim strings.