-
-
Notifications
You must be signed in to change notification settings - Fork 79
feat: JS dispatchers blog post #441
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
Merged
+188
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| --- | ||
| title: "Andrea Peruffo" | ||
| image: "images/blog/bio/aperuffo.jpg" | ||
| social: | ||
| - icon : "fab fa-linkedin-in" | ||
| name: "linkedin" | ||
| link : "https://www.linkedin.com/in/andrea-peruffo-32269178/" | ||
| - icon : "fab fa-twitter" | ||
| name: "twitter" | ||
| link : "https://twitter.com/and_prf" | ||
| - icon : "fab fa-github" | ||
| name: "github" | ||
| link : "https://github.com/andreaTP" | ||
| --- | ||
|
|
||
| Principal Software Engineer at IBM |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| --- | ||
| title: JavaScript dispatchers | ||
| date: 2026-01-21 | ||
| image: "images/blog/js-dispatchers-banner.png" | ||
| author: "Andrea Peruffo" | ||
andreaTP marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| type: "regular" | ||
| description: "Announcing JavaScript dispatcher in Microcks 1.13" | ||
| draft: false | ||
| --- | ||
|
|
||
| # Dynamic Mocking with JavaScript Dispatchers in Microcks | ||
|
|
||
| One of the most exciting new features introduced in [**Microcks 1.13**](https://microcks.io/blog/microcks-1.13.0-release/) is the ability to write [**JavaScript dispatchers**](https://microcks.io/documentation/explanations/dispatching/#javascript-scripting). | ||
| This gives you fine-grained control over how mock responses are selected and tailored—based not only on static examples but also on request headers, payloads, and parameters. | ||
|
|
||
| This is a powerful step forward: instead of static mocks, you can now create dynamic, context-aware simulations that behave much closer to real services. | ||
|
|
||
| > ℹ️ **Note:** Microcks already supported **Groovy-based dispatchers** in earlier releases. JavaScript is now offered as an alternative, making it easier for teams who prefer JS or want to align dispatcher logic with front-end or Node.js skills. | ||
|
|
||
| --- | ||
|
|
||
| ## Setting up Microcks locally | ||
|
|
||
| To get started quickly, you’ll need **Docker Compose**. | ||
| This sample command will give you a demo working environment in a one-liner: | ||
|
|
||
| ```bash | ||
| echo 'services: | ||
| mongo: | ||
| image: mongo:4.4.29 | ||
| app: | ||
| depends_on: | ||
| - mongo | ||
| image: microcks/microcks | ||
| ports: | ||
| - "8080:8080" | ||
| environment: | ||
| - SPRING_DATA_MONGODB_URI=mongodb://mongo:27017 | ||
| - SPRING_DATA_MONGODB_DATABASE=microcks | ||
| - KEYCLOAK_ENABLED=false' | docker compose -f - up | ||
| ``` | ||
|
|
||
| Once Microcks is up, try this quick path: | ||
|
|
||
| 1. Open [http://localhost:8080](http://localhost:8080) in your browser | ||
| 2. Go to **Microcks Hub** -> **MicrocksIO Samples APIs** -> **Pastry API - 2.0** | ||
| 3. Hit **Install** -> **+ Direct Import** | ||
| 4. In the left panel, open **APIs | Services** | ||
| 5. Pick **API Pastry - 2.0** -> select the second endpoint `⋮` (three dots) -> **Edit Properties** | ||
| 6. Scroll down and set **Dispatcher** to `JS` ✅ | ||
|
|
||
| ## Introducing JavaScript dispatchers | ||
|
|
||
| Now comes the fun part. In your API definition, you can attach a **JavaScript dispatcher script**. | ||
| This script inspects incoming requests and decides which example response to serve. | ||
|
|
||
| ### Example: dispatch by path parameter and Accept header | ||
|
|
||
| We’ll attach a script that looks at the `name` path parameter and the `Accept` header to choose the appropriate example. For `Eclair Cafe`, we’ll return a JSON or XML variant depending on the requested content type; for anything else, we’ll default to `Millefeuille`. | ||
|
|
||
| ```js | ||
| const contentType = mockRequest.getRequestHeader("Accept").toString(); | ||
| const nameParam = mockRequest.getURIParameter("name"); | ||
|
|
||
| if (nameParam === "Eclair Cafe") { | ||
| if (contentType === "text/xml") { | ||
| return "Eclair Cafe Xml"; | ||
| } else { | ||
| return "Eclair Cafe"; | ||
| } | ||
| } | ||
|
|
||
| return "Millefeuille"; | ||
| ``` | ||
|
|
||
| Try the following requests against `API Pastry - 2.0`: | ||
|
|
||
| ```bash | ||
| curl -X GET 'http://localhost:8080/rest/API+Pastry+-+2.0/2.0.0/pastry/Eclair+Cafe' -H 'Accept: application/json' | ||
| ``` | ||
|
|
||
| Returns: | ||
|
|
||
| ```json | ||
| { | ||
| "name": "Eclair Cafe", | ||
| "description": "Delicieux Eclair au Cafe pas calorique du tout", | ||
| "size": "M", | ||
| "price": 2.5, | ||
| "status": "available" | ||
| } | ||
| ``` | ||
|
|
||
| Requesting XML: | ||
|
|
||
| ```bash | ||
| curl -X GET 'http://localhost:8080/rest/API+Pastry+-+2.0/2.0.0/pastry/Eclair+Cafe' -H 'Accept: text/xml' | ||
| ``` | ||
|
|
||
| Returns: | ||
|
|
||
| ```xml | ||
| <pastry> | ||
| <name>Eclair Cafe</name> | ||
| <description>Delicieux Eclair au Cafe pas calorique du tout</description> | ||
| <size>M</size> | ||
| <price>2.5</price> | ||
| <status>available</status> | ||
| </pastry> | ||
| ``` | ||
|
|
||
| And the default variant when requesting another pastry name: | ||
|
|
||
| ```bash | ||
| curl -X GET 'http://localhost:8080/rest/API+Pastry+-+2.0/2.0.0/pastry/Millefeuille' -H 'Accept: application/json' | ||
| ``` | ||
|
|
||
| Returns: | ||
|
|
||
| ```json | ||
| { | ||
| "name": "Millefeuille", | ||
| "description": "Delicieux Millefeuille pas calorique du tout", | ||
| "size": "L", | ||
| "price": 4.4, | ||
| "status": "available" | ||
| } | ||
| ``` | ||
|
|
||
| ### Tweaking the script is easy (with server-side logging) | ||
|
|
||
| One of the benefits of JavaScript dispatchers is how quickly you can iterate: update the snippet, save, and re-run the same requests. You can also add server-side logs with the built-in `log.info` to trace what’s happening: | ||
|
|
||
| ```js | ||
| const accept = mockRequest.getRequestHeader("Accept").toString(); | ||
| const name = mockRequest.getURIParameter("name"); | ||
| log.info(`Dispatching for name=${name}, accept=${accept}`); | ||
|
|
||
| if (name === "Eclair Cafe" && accept === "text/xml") { | ||
| log.info("Returning XML example for Eclair Cafe"); | ||
| return "Eclair Cafe Xml"; | ||
| } | ||
|
|
||
| return "Millefeuille"; | ||
| ``` | ||
|
|
||
| Check your Microcks container/server logs to see these messages and understand which path the script took. | ||
|
|
||
| --- | ||
|
|
||
| ## Why it matters | ||
|
|
||
| With JavaScript dispatchers, Microcks becomes much more flexible: | ||
|
|
||
| * **Dynamic behavior** – responses vary depending on request context | ||
| * **Richer test scenarios** – simulate more realistic API flows | ||
| * **Fewer examples needed** – reuse existing payloads with smart logic | ||
andreaTP marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * **Maximum availability** - works in standard, uber, and native-compiled distributions of Microcks | ||
|
|
||
| This makes it easier to mock APIs for contract testing, early integration, or even demos where you want your mock to “feel alive”. | ||
|
|
||
| --- | ||
|
|
||
| ## Next steps | ||
|
|
||
| We’ve only scratched the surface here. In the full documentation you’ll find: | ||
|
|
||
| * How to write and attach dispatcher scripts | ||
| * The full request/response object model available in scripts | ||
| * Examples covering REST, gRPC, and event-based APIs | ||
|
|
||
| 👉 [Check the official documentation](https://microcks.io/documentation/) for more details and start experimenting with your own mocks. | ||
andreaTP marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.