4

I am using WireMock in java to stub for a POST request. The request returns a json body file that is stored in my local. The stub looks like this:

wireMockServer.stubFor(get(urlMatching("/v1/invoices/.*"))
            .willReturn(aResponse()
                .withStatus(200)
                .withBodyFile("testgetupgradeprorationamount/stubThree")));

Part of the response body file, "stubThree" looks like this:

"id": "ii_1EmM93Htp4Kkdrs8",
                "object": "line_item",
                "amount": 9600,
                "currency": "usd",
                "description": "Remaining time on 3 × Business after 17 Jun 2019",
                "discountable": false,
                "invoice_item": "ii_1EmM93HtpLyYzpmOC4Kkdrs8",
                "livemode": false,
                "metadata": {
                },
                "period": {
                    "end": 1563374954,
                    "start": 1560782957
                }

The request url has a number of paramters and looks like this:

/v1/invoices/?subscription_items[0][quantity]=3&subscription_proration_date=1560892137&customer=cus_FHNIIE4b8LH7qL"

The stubbing works fine, but my goal is to give a dynamic response using response templating. I want to update the "start" field of the json file only, using the "subscription_proration_date" value from the request url.

I changed the start field and the stub like this:

"period": {
                    "end": 1566053354,
                    "start": "{{request.query.subscription_proration_date}}"
                },

wireMockServer.stubFor(get(urlMatching("/v1/invoices/.*"))
            .willReturn(aResponse()
                .withStatus(200)
                .withBodyFile("testgetupgradeprorationamount/stubThree")
                .withTransformers("response-template")));




This is not working for me, so any directions would be really helpful. I also tried removing the quotations around the start field handlebar in the file, and that did not work either.

Thank you in advance!

Rayan Ahmed
  • 165
  • 2
  • 11

1 Answers1

5

so I was able to resolve my issue. The problem was that I did not add the proper extension to my WireMockServer instance:

.extensions(new ResponseTemplateTransformer(false));

If the boolean value is false, you additionally need to specify the transformer on a per stub basis like I did above. Otherwise, the extension is applied to all stubs.

Rayan Ahmed
  • 165
  • 2
  • 11