Skip to content

Commit

Permalink
Improve validation for setStatusCode templating function
Browse files Browse the repository at this point in the history
  • Loading branch information
tommysitu committed Jul 20, 2024
1 parent 632071f commit eaf4337
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
7 changes: 7 additions & 0 deletions core/templating/template_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,15 @@ func (t templateHelpers) hasJournalKey(indexName, keyValue string, options *raym
func (t templateHelpers) setStatusCode(statusCode string, options *raymond.Options) string {
intStatusCode, err := strconv.Atoi(statusCode)
if err != nil {
log.Error("status code is not a valid integer")
return ""
}

if intStatusCode < 100 || intStatusCode > 599 {
log.Error("status code is not valid")
return ""
}

internalVars := options.ValueFromAllCtx("InternalVars").(map[string]interface{})
internalVars["statusCode"] = intStatusCode
return ""
Expand Down
2 changes: 1 addition & 1 deletion core/templating/templating.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (t *Templator) RenderTemplate(tpl *raymond.Template, requestDetails *models
result, err := tpl.Exec(ctx)
if err == nil {
statusCode, ok := ctx.InternalVars["statusCode"]
if ok {
if ok && response != nil {
response.Status = statusCode.(int)
}
}
Expand Down
43 changes: 43 additions & 0 deletions core/templating/templating_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,49 @@ func Test_ApplyTemplate_PutAndGetValue_SuppressOutput(t *testing.T) {
Expect(template).To(Equal("The ID was 5553686208582"))
}

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

templator := templating.NewTemplator()

template, err := templator.ParseTemplate(`{{ setStatusCode 400 }}`)
Expect(err).To(BeNil())

response := &models.ResponseDetails{}
result, err := templator.RenderTemplate(template, &models.RequestDetails{}, response, &models.Literals{}, &models.Variables{}, make(map[string]string), &journal.Journal{})

Expect(err).To(BeNil())
Expect(result).To(Equal(""))
Expect(response.Status).To(Equal(400))
}

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

templator := templating.NewTemplator()

template, err := templator.ParseTemplate(`{{ setStatusCode 600 }}`)
Expect(err).To(BeNil())

response := &models.ResponseDetails{}
result, err := templator.RenderTemplate(template, &models.RequestDetails{}, response, &models.Literals{}, &models.Variables{}, make(map[string]string), &journal.Journal{})

Expect(err).To(BeNil())
Expect(result).To(Equal(""))
Expect(response.Status).To(Equal(0))
}

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

// ApplyTemplate pass a nil response value to RenderTemplate
template, err := ApplyTemplate(&models.RequestDetails{}, make(map[string]string), `{{ setStatusCode 400 }}`)

Expect(err).To(BeNil())

Expect(template).To(Equal(""))
}

func toInterfaceSlice(arguments []string) []interface{} {
argumentsArray := make([]interface{}, len(arguments))

Expand Down
9 changes: 8 additions & 1 deletion docs/pages/keyconcepts/templating/templating.rst
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ Syntax

.. code:: bash
journal "index name" "extracted value" "request/response" "xpath/jsonpath" "lookup query"
{{ journal "index name" "extracted value" "request/response" "xpath/jsonpath" "lookup query" }}
``index name`` should be the same key expression you have specified when you enable the journal index.
Expand All @@ -255,6 +255,13 @@ Example:
In the above example, we are querying the name from JSON response in the journal entry where index ``Request.QueryParam.id`` has a key value of 1.

If you only need to check if a journal contains a particular key, you can do so using the following function:

.. code:: bash
{{ hasJournalKey "index name" "key name" }}
Key Value Data Store
~~~~~~~~~~~~~~~~~~~~

Expand Down

0 comments on commit eaf4337

Please sign in to comment.