From 36303835c2b5794587bbe02023b249f1ddb4b640 Mon Sep 17 00:00:00 2001 From: Elisa Sawyer Date: Mon, 29 Jan 2024 08:21:46 -0800 Subject: [PATCH] fixed formatting error --- _site/docs/extending-wiremock.md | 37 +-- _site/docs/webhooks-and-callbacks.md | 390 +++++++++++++-------------- 2 files changed, 206 insertions(+), 221 deletions(-) diff --git a/_site/docs/extending-wiremock.md b/_site/docs/extending-wiremock.md index 8dd2baa0..ae0cea3b 100644 --- a/_site/docs/extending-wiremock.md +++ b/_site/docs/extending-wiremock.md @@ -12,17 +12,18 @@ You have the option of registering extensions programmatically using the class n An interface defines each extension point, extendibg from `Extension`. Extension implementations load at startup time. At present, the following extension interfaces are available: -* `RequestFilterV2`/`AdminRequestFilterV2`/`StubRequestFilterV2`: Intercept requests, modifying them or taking alternative actions based on their content. -* `ResponseDefinitionTransformerV2`: Modify the response definition used to generate a response. See [Transforming responses](./extensibility/transforming-responses.md). -* `ResponseTransformerV2`: Modify the response served to the client. See [Transforming responses](./extensibility/transforming-responses.md). -* `ServeEventListener`: Listen for events at various points in the request processing lifecycle. See [Listening for Serve Events](./extensibility/listening-for-serve-events.md). -* `AdminApiExtension`: Add admin API functions. See [Admin API Extensions](./extensibility/extending-the-admin-api.md). -* `RequestMatcherExtension`: Implement custom request matching logic. See [Custom matching](./extensibility/custom-matching.md). -* `GlobalSettingsListener`: Listen for changes to the settings object. See [Listening for Settings Changes](./extensibility/listening-for-settings-changes.md). -* `StubLifecycleListener`: Listen for changes to the stub mappings. See [Listening for Stub Changes](./extensibility/listening-for-stub-changes.md). -* `TemplateHelperProviderExtension`: Provide custom Handlebars helpers to the template engine. See [Adding Template Helpers](./extensibility/adding-template-helpers.md). -* `TemplateModelDataProviderExtension`: Provide additional data to the model passed to response templates. See [Adding Template Model Data](./extensibility/adding-template-model-data.md). -* `MappingsLoaderExtension`: Provide additional source to load the stub mappings. See [Adding Mappings Loader](./extensibility/adding-mappings-loader.md). + +- `RequestFilterV2`/`AdminRequestFilterV2`/`StubRequestFilterV2`: Intercept requests, modifying them or taking alternative actions based on their content. +- `ResponseDefinitionTransformerV2`: Modify the response definition used to generate a response. See [Transforming responses](./extensibility/transforming-responses.md). +- `ResponseTransformerV2`: Modify the response served to the client. See [Transforming responses](./extensibility/transforming-responses.md). +- `ServeEventListener`: Listen for events at various points in the request processing lifecycle. See [Listening for Serve Events](./extensibility/listening-for-serve-events.md). +- `AdminApiExtension`: Add admin API functions. See [Admin API Extensions](./extensibility/extending-the-admin-api.md). +- `RequestMatcherExtension`: Implement custom request matching logic. See [Custom matching](./extensibility/custom-matching.md). +- `GlobalSettingsListener`: Listen for changes to the settings object. See [Listening for Settings Changes](./extensibility/listening-for-settings-changes.md). +- `StubLifecycleListener`: Listen for changes to the stub mappings. See [Listening for Stub Changes](./extensibility/listening-for-stub-changes.md). +- `TemplateHelperProviderExtension`: Provide custom Handlebars helpers to the template engine. See [Adding Template Helpers](./extensibility/adding-template-helpers.md). +- `TemplateModelDataProviderExtension`: Provide additional data to the model passed to response templates. See [Adding Template Model Data](./extensibility/adding-template-model-data.md). +- `MappingsLoaderExtension`: Provide additional source to load the stub mappings. See [Adding Mappings Loader](./extensibility/adding-mappings-loader.md). The interfaces in this list ending with `V2` supercede deprecated equivalents with an older, more restrictive interface. Additionally `ServeEventListener` deprecates `PostServeAction`. @@ -63,14 +64,14 @@ new WireMockServer(wireMockConfig() Services currently available to extension factories are: -* `Admin`: the main WireMock functional interface for stubbing, verification and configuration tasks. -* `Options`: the configuration object built at startup. -* `Stores`: the root interface for gaining access to the various stores of WireMock's state and creating/using custom stores. -* `FileSource`: the `__files` directory where larger response body files are often kept. -* `Extensions`: the service for creating and providing extension implementations. -* `TemplateEngine`: the Handlebars template engine. +- `Admin`: the main WireMock functional interface for stubbing, verification and configuration tasks. +- `Options`: the configuration object built at startup. +- `Stores`: the root interface for gaining access to the various stores of WireMock's state and creating/using custom stores. +- `FileSource`: the `__files` directory where larger response body files are often kept. +- `Extensions`: the service for creating and providing extension implementations. +- `TemplateEngine`: the Handlebars template engine. -## Extension registration via service loading +## Extension registration using service loading Iif they are placed on the classpath, extensions that are packaged with the relevant [Java service loader framework](https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html) metadata will load automatically. diff --git a/_site/docs/webhooks-and-callbacks.md b/_site/docs/webhooks-and-callbacks.md index 795de311..7e48ed53 100644 --- a/_site/docs/webhooks-and-callbacks.md +++ b/_site/docs/webhooks-and-callbacks.md @@ -10,7 +10,7 @@ of events or perform long-running processing asynchronously without blocking. ## Enabling webhooks -Prior to WireMock 3.1.0 webhooks were provided via an extension and needed to be explicitly enabled. See [the 2.x docs](https://wiremock.org/2.x/docs/webhooks-and-callbacks/) for details on how to do this. +Prior to WireMock 3.1.0, webhooks were provided using an extension and needed to be explicitly enabled. See [the 2.x docs](https://wiremock.org/2.x/docs/webhooks-and-callbacks/) for details on how to do this. From version 3.1.0 the webhooks extension is part of WireMock's core and enabled by default. @@ -23,48 +23,48 @@ This article shows how to use this newer extension point, however the legacy `Po You can trigger a single webhook request to a fixed URL, with fixed data like this: -Java: - -```java -import static org.wiremock.webhooks.Webhooks.*; -... - -wm.stubFor(post(urlPathEqualTo("/something-async")) - .willReturn(ok()) - .withServeEventListener("webhook", webhook() - .withMethod(POST) - .withUrl("http://my-target-host/callback") - .withHeader("Content-Type", "application/json") - .withBody("{ \"result\": \"SUCCESS\" }")) - ); -``` - -JSON: - -```json -{ - "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\" }" +=== "Java" + + ```java + import static org.wiremock.webhooks.Webhooks.*; + ... + + wm.stubFor(post(urlPathEqualTo("/something-async")) + .willReturn(ok()) + .withServeEventListener("webhook", webhook() + .withMethod(POST) + .withUrl("http://my-target-host/callback") + .withHeader("Content-Type", "application/json") + .withBody("{ \"result\": \"SUCCESS\" }")) + ); + ``` + +=== "JSON" + + ```json + { + "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\" }" + } } - } - ] -} -``` + ] + } + ``` ## Using data from the original request @@ -86,104 +86,88 @@ For an original request body JSON like this: We could construct a JSON request body in the webhook like this: -Java: - -{% raw %} - -```java -wm.stubFor(post(urlPathEqualTo("/templating")) - .willReturn(ok()) - .withServeEventListener("webhook", webhook() - .withMethod(POST) - .withUrl("http://my-target-host/callback") - .withHeader("Content-Type", "application/json") - .withBody("{ \"message\": \"success\", \"transactionId\": \"{{jsonPath originalRequest.body '$.transactionId'}}\" }") - ); -``` - -{% endraw %} - -JSON: - -{% raw %} - -```json -{ - "request": { - "urlPath": "/templating", - "method": "POST" - }, - "response": { - "status": 200 - }, - "serveEventListeners": [ - { - "name": "webhook", - "parameters": { - "method": "POST", - "url": "http://my-target-host/callback", - "headers": { - "Content-Type": "application/json" - }, - "body": "{ \"message\": \"success\", \"transactionId\": \"{{jsonPath originalRequest.body '$.transactionId'}}\" }" +=== "Java" + + ```java + wm.stubFor(post(urlPathEqualTo("/templating")) + .willReturn(ok()) + .withServeEventListener("webhook", webhook() + .withMethod(POST) + .withUrl("http://my-target-host/callback") + .withHeader("Content-Type", "application/json") + .withBody("{ \"message\": \"success\", \"transactionId\": \"{{jsonPath originalRequest.body '$.transactionId'}}\" }") + ); + ``` + +=== "JSON" + + ```json + { + "request": { + "urlPath": "/templating", + "method": "POST" + }, + "response": { + "status": 200 + }, + "serveEventListeners": [ + { + "name": "webhook", + "parameters": { + "method": "POST", + "url": "http://my-target-host/callback", + "headers": { + "Content-Type": "application/json" + }, + "body": "{ \"message\": \"success\", \"transactionId\": \"{{jsonPath originalRequest.body '$.transactionId'}}\" }" + } } - } - ] -} -``` + ] + } + ``` -{% endraw %} +!!! note -> **note** -> -> Webhook templates currently do not support system or environment variables. + Webhook templates currently do not support system or environment variables. ## Implementing a callback using templating To implement the callback pattern, where the original request contains the target to be called on completion of a long-running task, we can use templating on the URL and method. -Java: - -{% raw %} - -```java -wm.stubFor(post(urlPathEqualTo("/something-async")) - .willReturn(ok()) - .withServeEventListener("webhook", webhook() - .withMethod("{{jsonPath originalRequest.body '$.callbackMethod'}}") - .withUrl("{{jsonPath originalRequest.body '$.callbackUrl'}}")) - ); -``` - -{% endraw %} - -JSON: - -{% raw %} - -```json -{ - "request": { - "urlPath": "/something-async", - "method": "POST" - }, - "response": { - "status": 200 - }, - "serveEventListeners": [ - { - "name": "webhook", - "parameters": { - "method": "{{jsonPath originalRequest.body '$.callbackMethod'}}", - "url": "{{jsonPath originalRequest.body '$.callbackUrl'}}" +=== "Java" + + ```java + wm.stubFor(post(urlPathEqualTo("/something-async")) + .willReturn(ok()) + .withServeEventListener("webhook", webhook() + .withMethod("{{jsonPath originalRequest.body '$.callbackMethod'}}") + .withUrl("{{jsonPath originalRequest.body '$.callbackUrl'}}")) + ); + ``` + +=== "JSON" + + ```json + { + "request": { + "urlPath": "/something-async", + "method": "POST" + }, + "response": { + "status": 200 + }, + "serveEventListeners": [ + { + "name": "webhook", + "parameters": { + "method": "{{jsonPath originalRequest.body '$.callbackMethod'}}", + "url": "{{jsonPath originalRequest.body '$.callbackUrl'}}" + } } - } - ] -} -``` - -{% endraw %} + ] + } + ``` ## Adding delays @@ -191,88 +175,88 @@ A fixed or random delay can be added before the webhook call is made, using the ### Fixed delays -Java: - -```java -wm.stubFor(post(urlPathEqualTo("/delayed")) - .willReturn(ok()) - .withServeEventListener("webhook", webhook() - .withFixedDelay(1000) - .withMethod(RequestMethod.GET) - .withUrl("http://my-target-host/callback") - ) -); -``` - -JSON: +=== "Java" -```json -{ - "request": { - "urlPath": "/delayed", - "method": "POST" - }, - "response": { - "status": 200 - }, - "serveEventListeners": [ - { - "name": "webhook", - "parameters": { - "method": "GET", - "url": "http://my-target-host/callback", - "delay": { - "type": "fixed", - "milliseconds": 1000 + ```java + wm.stubFor(post(urlPathEqualTo("/delayed")) + .willReturn(ok()) + .withServeEventListener("webhook", webhook() + .withFixedDelay(1000) + .withMethod(RequestMethod.GET) + .withUrl("http://my-target-host/callback") + ) + ); + ``` + +=== "JSON" + + ```json + { + "request": { + "urlPath": "/delayed", + "method": "POST" + }, + "response": { + "status": 200 + }, + "serveEventListeners": [ + { + "name": "webhook", + "parameters": { + "method": "GET", + "url": "http://my-target-host/callback", + "delay": { + "type": "fixed", + "milliseconds": 1000 + } } } - } - ] -} -``` + ] + } + ``` ### Random delays -Java: - -```java -wm.stubFor(post(urlPathEqualTo("/delayed")) - .willReturn(ok()) - .withServeEventListener("webhook", webhook() - .withDelay(new UniformDistribution(500, 1000)) - .withMethod(RequestMethod.GET) - .withUrl("http://my-target-host/callback") - ) -); -``` - -JSON: +=== "Java" -```json -{ - "request": { - "urlPath": "/delayed", - "method": "POST" - }, - "response": { - "status": 200 - }, - "serveEventListeners": [ - { - "name": "webhook", - "parameters": { - "method": "GET", - "url": "http://my-target-host/callback", - "delay": { - "type": "uniform", - "lower": 500, - "upper": 1000 + ```java + wm.stubFor(post(urlPathEqualTo("/delayed")) + .willReturn(ok()) + .withServeEventListener("webhook", webhook() + .withDelay(new UniformDistribution(500, 1000)) + .withMethod(RequestMethod.GET) + .withUrl("http://my-target-host/callback") + ) + ); + ``` + +=== "JSON" + + ```json + { + "request": { + "urlPath": "/delayed", + "method": "POST" + }, + "response": { + "status": 200 + }, + "serveEventListeners": [ + { + "name": "webhook", + "parameters": { + "method": "GET", + "url": "http://my-target-host/callback", + "delay": { + "type": "uniform", + "lower": 500, + "upper": 1000 + } } } - } - ] -} -``` + ] + } + ``` ## Extending webhooks