admin管理员组

文章数量:1299985

I have my wiremock service running on a kubernetes pod. One of the mappings is as follows:

 {
  "request": {
    "method": "POST",
    "urlPattern": "/v1/queue/*."
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json"
    },
    "body": "{\"id\":\"GR3e9g\",\"partition\":4,\"time\":07448}"
  }
}

Now whenever the wiremock service receives an API call matching the urlPattern /v1/queue/*., I want to add custom logic for the wiremock service to make a call to DynamoDB table. Can I get some pointers on how to implement that. What changes do I make to this Dockerfile to accomodate the custom logic implementation?

FROM wiremock/wiremock:latest

COPY stubs /home/wiremock
EXPOSE 8080

I have my wiremock service running on a kubernetes pod. One of the mappings is as follows:

 {
  "request": {
    "method": "POST",
    "urlPattern": "/v1/queue/*."
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json"
    },
    "body": "{\"id\":\"GR3e9g\",\"partition\":4,\"time\":07448}"
  }
}

Now whenever the wiremock service receives an API call matching the urlPattern /v1/queue/*., I want to add custom logic for the wiremock service to make a call to DynamoDB table. Can I get some pointers on how to implement that. What changes do I make to this Dockerfile to accomodate the custom logic implementation?

FROM wiremock/wiremock:latest

COPY stubs /home/wiremock
EXPOSE 8080
Share asked Feb 12 at 9:03 Sai KrishnaSai Krishna 6211 gold badge12 silver badges34 bronze badges 2
  • Given the stub already has a response defined, am I correct in thinking the call to the DynamoDB table isn't to use any data returned from the DB in the response of the stub? You still want the stub to return {\"id\":\"GR3e9g\",\"partition\":4,\"time\":07448} and then make a call to the DB? – Lee Turner Commented Feb 12 at 9:27
  • @LeeTurner yes, the response has nothing to do with the data returned from db. Infact, I want to make the db call to put a record in the dynamodb table – Sai Krishna Commented Feb 12 at 16:29
Add a comment  | 

1 Answer 1

Reset to default 0

Given you don't need the data from the DB query in the response for the stub I would probably look at building a ServeEventListener. These listeners allow you to take an action at a specific point in the request processing flow, without affecting processing in any way. More information can be found here - https://wiremock./docs/extensibility/listening-for-serve-events/

You would then need to bundle the listener into a WireMock extension. To help with that we have a number of extensions you could copy from on the WireMock github anisation along with the WireMock extension template.

Once you have built your extension you can include it in your WireMock instance by copying it to the extensions directory. I have a similar example in this repository - https://github/leeturner/wiremock-standalone-docker-example

The extension jar is placed in the extensions directory here and the extensions directory is mapped to the same directory inside the container here.

Once all of that is in place, you add the ServeEventListener to your stub. When you make a request that matches your stub, the ServeEventListener is executed. An example of this is the webhook functionality in WireMock. This is implemented as a ServeEventListener. The stub json looks something like this:

{
    "request": {
        "urlPath": "/something-async",
        "method": "POST"
    },
    "response": {
        "status": 200
    },
    "serveEventListeners": [
        {
            "name": "webhook",
            "parameters": {
                "method": "POST",
                "url": "http://my-target-host/callback",
                "headers": {
                    "Content-Type": "application/json"
                },
                "body": "{ \"result\": \"SUCCESS\" }"
            }
        }
    ]
}

Obviously you would replace the webhook ServeEventListener with your own (with its own name and parameters).

本文标签: dockerCustom Action for a wiremock stubStack Overflow