Skip to content

Commit 3edf2af

Browse files
committed
Add templating function to check journal key
1 parent c81076b commit 3edf2af

File tree

4 files changed

+68
-25
lines changed

4 files changed

+68
-25
lines changed

core/templating/template_helpers.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ func (t templateHelpers) parseJournalBasedOnIndex(indexName, keyValue, dataSourc
180180
return getEvaluationString("journal", options)
181181
}
182182

183+
func (t templateHelpers) hasJournalKey(indexName, keyValue string, options *raymond.Options) bool {
184+
journalDetails := options.Value("Journal").(Journal)
185+
journalEntry, _ := getIndexEntry(journalDetails, indexName, keyValue)
186+
187+
return journalEntry != nil
188+
}
189+
183190
func (t templateHelpers) setStatusCode(statusCode int, options *raymond.Options) string {
184191
internalVars := options.ValueFromAllCtx("InternalVars").(map[string]interface{})
185192
internalVars["statusCode"] = statusCode

core/templating/templating.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func NewTemplator() *Templator {
9191
helperMethodMap["requestBody"] = t.requestBody
9292
helperMethodMap["csv"] = t.parseCsv
9393
helperMethodMap["journal"] = t.parseJournalBasedOnIndex
94+
helperMethodMap["hasJournalKey"] = t.hasJournalKey
9495
helperMethodMap["setStatusCode"] = t.setStatusCode
9596
helperMethodMap["sum"] = t.sum
9697
helperMethodMap["add"] = t.add

functional-tests/core/ft_journal_indexing_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/dghubble/sling"
77
. "github.com/onsi/ginkgo"
88
. "github.com/onsi/gomega"
9+
"io"
910
"io/ioutil"
1011
"net/http"
1112
"net/http/httptest"
@@ -57,6 +58,15 @@ var _ = Describe("Manage journal indexing in hoverfly", func() {
5758

5859
Expect(string(body)).To(Equal("Application Testing"))
5960

61+
// hasJournalKey function should return true
62+
simulationResponse = hoverfly.Proxy(sling.New().Get("http://test-server.com/checkJournalKey"))
63+
Expect(simulationResponse.StatusCode).To(Equal(200))
64+
65+
body, err = io.ReadAll(simulationResponse.Body)
66+
Expect(err).To(BeNil())
67+
68+
Expect(string(body)).To(Equal("123: true 345: false"))
69+
6070
})
6171
})
6272

functional-tests/testdata/templating_enabled.go

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,31 +60,56 @@ var V3TemplatingEnabled = `{
6060
}`
6161

6262
var JournalTemplatingWithQueryParamIndexEnabled = `{
63-
"data": {
64-
"pairs": [
65-
{
66-
"response": {
67-
"status": 200,
68-
"body": "{{ journal 'Request.QueryParam.id' '123' 'Response' 'jsonpath' '$.name' }}",
69-
"encodedBody": false,
70-
"templated" : true
71-
},
72-
"request": {
73-
"method": {
74-
"exactMatch": "GET"
75-
}
76-
}
77-
}
78-
],
79-
"globalActions": {
80-
"delays": []
81-
}
82-
},
83-
"meta": {
84-
"schemaVersion": "v3",
85-
"hoverflyVersion": "v0.10.2",
86-
"timeExported": "2017-02-23T12:43:48Z"
87-
}
63+
"data": {
64+
"pairs": [
65+
{
66+
"request": {
67+
"method": [
68+
{
69+
"matcher": "exact",
70+
"value": "GET"
71+
}
72+
]
73+
},
74+
"response": {
75+
"status": 200,
76+
"body": "{{ journal 'Request.QueryParam.id' '123' 'Response' 'jsonpath' '$.name' }}",
77+
"encodedBody": false,
78+
"templated": true
79+
}
80+
},
81+
{
82+
"request": {
83+
"method": [
84+
{
85+
"matcher": "exact",
86+
"value": "GET"
87+
}
88+
],
89+
"path": [
90+
{
91+
"matcher": "exact",
92+
"value": "/checkJournalKey"
93+
}
94+
]
95+
},
96+
"response": {
97+
"status": 200,
98+
"body": "123: {{ hasJournalKey 'Request.QueryParam.id' '123' }} 345: {{ hasJournalKey 'Request.QueryParam.id' '345' }}",
99+
"encodedBody": false,
100+
"templated": true
101+
}
102+
}
103+
],
104+
"globalActions": {
105+
"delays": [],
106+
"delaysLogNormal": []
107+
}
108+
},
109+
"meta": {
110+
"schemaVersion": "v5.2",
111+
"hoverflyVersion": "v1.9.3"
112+
}
88113
}`
89114

90115
var JournalTemplatingWithBodyIndexEnabled = `{

0 commit comments

Comments
 (0)