Skip to content

Commit

Permalink
Document and add test for split templating function
Browse files Browse the repository at this point in the history
  • Loading branch information
tommysitu committed Apr 20, 2024
1 parent e47729f commit 1af8d3b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
8 changes: 0 additions & 8 deletions core/templating/template_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,6 @@ func (t templateHelpers) concat(val1, val2 string) string {
return val1 + val2
}

func prepareJsonPathQuery(query string) string {
if query[0:1] != "{" && query[len(query)-1:] != "}" {
query = fmt.Sprintf("{%s}", query)
}

return query
}

func (t templateHelpers) faker(fakerType string) []reflect.Value {

if t.fakerSource == nil {
Expand Down
10 changes: 10 additions & 0 deletions core/templating/templating_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ func Test_ApplyTemplate_ParseCsvByPassingRequestParamAndReturnMatchValue(t *test
Expect(template).To(Equal(`55`))
}

func Test_ApplyTemplate_EachBlockWithSplitTemplatingFunction(t *testing.T) {
RegisterTestingT(t)

template, err := ApplyTemplate(&models.RequestDetails{}, make(map[string]string),
`{{#each (split 'one,two,three' ',') }}{{this}} | {{/each}}`)

Expect(err).To(BeNil())
Expect(template).To(Equal(`one | two | three | `))
}

func Test_ApplyTemplate_EachBlockWithCsvTemplatingFunction(t *testing.T) {
RegisterTestingT(t)

Expand Down
4 changes: 4 additions & 0 deletions docs/pages/keyconcepts/templating/templating.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ Additional data can come from helper methods. These are the ones Hoverfly curren
+-----------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
| Maths operations (add, subtract, multiply, divide, sum) | ``{{ divide 10 3 '0.00' }}`` | 3.33 |
+-----------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
| String concatenate | ``{{ concat 'bee' 'hive' }}`` | beehive |
+-----------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
| String splitting | ``{{ split 'bee,hive' ',' }}`` | []string{"bee", "hive"} |
+-----------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
| Replace all occurrences of the old value with the new | ``{{ replace (Request.Body 'jsonpath' '$.text')`` | |
| | ``'be' 'mock' }}`` | |
| value in the target string | (where Request.Body has the value of | |
Expand Down

0 comments on commit 1af8d3b

Please sign in to comment.