diff --git a/core/templating/template_helpers.go b/core/templating/template_helpers.go index 82a711d6b..26b5149f6 100644 --- a/core/templating/template_helpers.go +++ b/core/templating/template_helpers.go @@ -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 "" diff --git a/core/templating/templating.go b/core/templating/templating.go index 60b6e29a9..1016328a8 100644 --- a/core/templating/templating.go +++ b/core/templating/templating.go @@ -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) } } diff --git a/core/templating/templating_test.go b/core/templating/templating_test.go index c64ad16b4..ca419deb7 100644 --- a/core/templating/templating_test.go +++ b/core/templating/templating_test.go @@ -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)) diff --git a/docs/pages/keyconcepts/templating/templating.rst b/docs/pages/keyconcepts/templating/templating.rst index f481e7098..51018b6c9 100644 --- a/docs/pages/keyconcepts/templating/templating.rst +++ b/docs/pages/keyconcepts/templating/templating.rst @@ -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. @@ -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 ~~~~~~~~~~~~~~~~~~~~