|
| 1 | +--- |
| 2 | +title: JavaScript dispatchers |
| 3 | +date: 2026-01-21 |
| 4 | +image: "images/blog/js-dispatchers-banner.png" |
| 5 | +author: "Andrea Peruffo" |
| 6 | +type: "regular" |
| 7 | +description: "Announcing JavaScript dispatcher in Microcks 1.13" |
| 8 | +draft: false |
| 9 | +--- |
| 10 | + |
| 11 | +# Dynamic Mocking with JavaScript Dispatchers in Microcks |
| 12 | + |
| 13 | +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). |
| 14 | +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. |
| 15 | + |
| 16 | +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. |
| 17 | + |
| 18 | +> ℹ️ **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. |
| 19 | +
|
| 20 | +--- |
| 21 | + |
| 22 | +## Setting up Microcks locally |
| 23 | + |
| 24 | +To get started quickly, you’ll need **Docker Compose**. |
| 25 | +This sample command will give you a demo working environment in a one-liner: |
| 26 | + |
| 27 | +```bash |
| 28 | +echo 'services: |
| 29 | + mongo: |
| 30 | + image: mongo:4.4.29 |
| 31 | + app: |
| 32 | + depends_on: |
| 33 | + - mongo |
| 34 | + image: microcks/microcks |
| 35 | + ports: |
| 36 | + - "8080:8080" |
| 37 | + environment: |
| 38 | + - SPRING_DATA_MONGODB_URI=mongodb://mongo:27017 |
| 39 | + - SPRING_DATA_MONGODB_DATABASE=microcks |
| 40 | + - KEYCLOAK_ENABLED=false' | docker compose -f - up |
| 41 | +``` |
| 42 | + |
| 43 | +Once Microcks is up, try this quick path: |
| 44 | + |
| 45 | +1. Open [http://localhost:8080](http://localhost:8080) in your browser |
| 46 | +2. Go to **Microcks Hub** -> **MicrocksIO Samples APIs** -> **Pastry API - 2.0** |
| 47 | +3. Hit **Install** -> **+ Direct Import** |
| 48 | +4. In the left panel, open **APIs | Services** |
| 49 | +5. Pick **API Pastry - 2.0** -> select the second endpoint `⋮` (three dots) -> **Edit Properties** |
| 50 | +6. Scroll down and set **Dispatcher** to `JS` ✅ |
| 51 | + |
| 52 | +## Introducing JavaScript dispatchers |
| 53 | + |
| 54 | +Now comes the fun part. In your API definition, you can attach a **JavaScript dispatcher script**. |
| 55 | +This script inspects incoming requests and decides which example response to serve. |
| 56 | + |
| 57 | +### Example: dispatch by path parameter and Accept header |
| 58 | + |
| 59 | +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`. |
| 60 | + |
| 61 | +```js |
| 62 | +const contentType = mockRequest.getRequestHeader("Accept").toString(); |
| 63 | +const nameParam = mockRequest.getURIParameter("name"); |
| 64 | + |
| 65 | +if (nameParam === "Eclair Cafe") { |
| 66 | + if (contentType === "text/xml") { |
| 67 | + return "Eclair Cafe Xml"; |
| 68 | + } else { |
| 69 | + return "Eclair Cafe"; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +return "Millefeuille"; |
| 74 | +``` |
| 75 | + |
| 76 | +Try the following requests against `API Pastry - 2.0`: |
| 77 | + |
| 78 | +```bash |
| 79 | +curl -X GET 'http://localhost:8080/rest/API+Pastry+-+2.0/2.0.0/pastry/Eclair+Cafe' -H 'Accept: application/json' |
| 80 | +``` |
| 81 | + |
| 82 | +Returns: |
| 83 | + |
| 84 | +```json |
| 85 | +{ |
| 86 | + "name": "Eclair Cafe", |
| 87 | + "description": "Delicieux Eclair au Cafe pas calorique du tout", |
| 88 | + "size": "M", |
| 89 | + "price": 2.5, |
| 90 | + "status": "available" |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +Requesting XML: |
| 95 | + |
| 96 | +```bash |
| 97 | +curl -X GET 'http://localhost:8080/rest/API+Pastry+-+2.0/2.0.0/pastry/Eclair+Cafe' -H 'Accept: text/xml' |
| 98 | +``` |
| 99 | + |
| 100 | +Returns: |
| 101 | + |
| 102 | +```xml |
| 103 | +<pastry> |
| 104 | + <name>Eclair Cafe</name> |
| 105 | + <description>Delicieux Eclair au Cafe pas calorique du tout</description> |
| 106 | + <size>M</size> |
| 107 | + <price>2.5</price> |
| 108 | + <status>available</status> |
| 109 | +</pastry> |
| 110 | +``` |
| 111 | + |
| 112 | +And the default variant when requesting another pastry name: |
| 113 | + |
| 114 | +```bash |
| 115 | +curl -X GET 'http://localhost:8080/rest/API+Pastry+-+2.0/2.0.0/pastry/Millefeuille' -H 'Accept: application/json' |
| 116 | +``` |
| 117 | + |
| 118 | +Returns: |
| 119 | + |
| 120 | +```json |
| 121 | +{ |
| 122 | + "name": "Millefeuille", |
| 123 | + "description": "Delicieux Millefeuille pas calorique du tout", |
| 124 | + "size": "L", |
| 125 | + "price": 4.4, |
| 126 | + "status": "available" |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +### Tweaking the script is easy (with server-side logging) |
| 131 | + |
| 132 | +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: |
| 133 | + |
| 134 | +```js |
| 135 | +const accept = mockRequest.getRequestHeader("Accept").toString(); |
| 136 | +const name = mockRequest.getURIParameter("name"); |
| 137 | +log.info(`Dispatching for name=${name}, accept=${accept}`); |
| 138 | + |
| 139 | +if (name === "Eclair Cafe" && accept === "text/xml") { |
| 140 | + log.info("Returning XML example for Eclair Cafe"); |
| 141 | + return "Eclair Cafe Xml"; |
| 142 | +} |
| 143 | + |
| 144 | +return "Millefeuille"; |
| 145 | +``` |
| 146 | + |
| 147 | +Check your Microcks container/server logs to see these messages and understand which path the script took. |
| 148 | + |
| 149 | +--- |
| 150 | + |
| 151 | +## Why it matters |
| 152 | + |
| 153 | +With JavaScript dispatchers, Microcks becomes much more flexible: |
| 154 | + |
| 155 | +* **Dynamic behavior** – responses vary depending on request context |
| 156 | +* **Richer test scenarios** – simulate more realistic API flows |
| 157 | +* **Fewer examples needed** – reuse existing payloads with smart logic |
| 158 | +* **Maximum availability** - works in standard, uber, and native-compiled distributions of Microcks |
| 159 | + |
| 160 | +This makes it easier to mock APIs for contract testing, early integration, or even demos where you want your mock to “feel alive”. |
| 161 | + |
| 162 | +--- |
| 163 | + |
| 164 | +## Next steps |
| 165 | + |
| 166 | +We’ve only scratched the surface here. In the full documentation you’ll find: |
| 167 | + |
| 168 | +* How to write and attach dispatcher scripts |
| 169 | +* The full request/response object model available in scripts |
| 170 | +* Examples covering REST, gRPC, and event-based APIs |
| 171 | + |
| 172 | +👉 [Check the official documentation](https://microcks.io/documentation/) for more details and start experimenting with your own mocks. |
0 commit comments