Skip to content

Commit

Permalink
Add journal templating function readme and example simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
tommysitu committed Mar 9, 2024
1 parent 6903074 commit 8eadce7
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
53 changes: 53 additions & 0 deletions examples/journal-templating/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

# Journal templating

This new templating function allow you to query data from the journal to generate dynamic response.

Use the provided Hoverfly binary, run it in capture mode (which allows request to be passed through and recorded in the journal),
and also import the simulation `journal-templating.json`.

```commandline
hoverfly -capture -import journal-template.json -journal-indexing-key Request.QueryParam.id
curl --proxy localhost:8500 'time.jsontest.com?id=123'
```

The request is now recorded, and we can switch Hoverfly to simulate mode, and then this time we can call the stateful endpoint which will trigger the simulation:
```commandline
hoverctl mode simulate
curl --proxy localhost:8500 'time.jsontest.com/stateful'
```

You will see that the stateful endpoint response is the time field from the previous response for `localhost:8500 'time.jsontest.com?id=123`
That's because the simulation uses this templating function:

```json

"response": {
"status": 200,
"body": "{{ journal 'Request.QueryParam.id' '123' 'Response' 'jsonpath' '$.time' }}",
"encodedBody": false,
"templated" : true
}

```

What it does is that it look up the journal with this index key `Request.QueryParam.id`,
so if any of the previous request has a request query param with key `id` and value `123`,
then it will return the response body of that request, and then apply the jsonpath expression `$.time` to extract the time field from the response body.

The index key `Request.QueryParam.id` is just an example, you can specify other request data as index to look up using the same syntax as templating request data:
https://docs.hoverfly.io/en/latest/pages/keyconcepts/templating/templating.html#getting-data-from-the-request

eg. if you want to look up the journal by the second request path variable, you can use this index key: `Request.Path.[1]`

```commandline
hoverfly -journal-indexing-key Request.Path.[1]
```

You can also specify multiple index keys like this:

```commandline
hoverfly -journal-indexing-key Request.Path.[1] -journal-indexing-key Request.Header.X-Header-Id
```

In which case we will create multiple indices for the journal based on the given keys. It allows you to lookup all the past request/response using those keys.
35 changes: 35 additions & 0 deletions examples/journal-templating/journal-template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"data": {
"pairs": [
{
"request": {
"method": [
{
"matcher": "exact",
"value": "GET"
}],
"path": [
{
"matcher": "exact",
"value": "/stateful"
}
]
},
"response": {
"status": 200,
"body": "{{ journal 'Request.QueryParam.id' '123' 'Response' 'jsonpath' '$.time' }}",
"encodedBody": false,
"templated" : true
}
}
],
"globalActions": {
"delays": []
}
},
"meta": {
"schemaVersion": "v5.2",
"hoverflyVersion": "v0.10.2",
"timeExported": "2017-02-23T12:43:48Z"
}
}

0 comments on commit 8eadce7

Please sign in to comment.