From 505c3bd455c237904982e48211c1ba88adacf852 Mon Sep 17 00:00:00 2001 From: zekroTJA Date: Wed, 5 Apr 2023 09:01:41 +0000 Subject: [PATCH 1/5] fix log section spacers; add color to log sections --- pkg/executor/executor.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/executor/executor.go b/pkg/executor/executor.go index 0ec1dde..249aeba 100644 --- a/pkg/executor/executor.go +++ b/pkg/executor/executor.go @@ -372,10 +372,11 @@ func (t *Executor) executeAction(log rogu.Logger, eng engine.Engine, act goatfil lenSpacerLeft := lenSpacer / 2 lenSpacerRight := lenSpacerLeft if lenSpacer%2 > 0 { - lenSpacerRight-- + lenSpacerRight++ } - log.Info().Msgf("%s %s %s", + msg := clr.Print(clr.Format("%s %s %s", clr.ColorFGPurple)) + log.Info().Msgf(msg, strings.Repeat("-", lenSpacerLeft), logSection, strings.Repeat("-", lenSpacerRight)) From 0cf46694f42327ba71756db2c452eb16e2f0b9ca Mon Sep 17 00:00:00 2001 From: zekroTJA Date: Thu, 6 Apr 2023 09:05:18 +0000 Subject: [PATCH 2/5] change [Headers] to [Header] --- docs/goatfile-spec.md | 2 +- e2e/cases/01-general/test.goat | 2 +- e2e/cases/03-fileimport/a/_a.goat | 2 +- e2e/cases/03-fileimport/direct.goat | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/goatfile-spec.md b/docs/goatfile-spec.md index c59655c..8652d8a 100644 --- a/docs/goatfile-spec.md +++ b/docs/goatfile-spec.md @@ -11,7 +11,7 @@ Section Heading | ### Setup | Method & URL | POST https://example.com | - Headers | [Headers] + Headers | [Header] | X-Requested-With: XMLHttpRequest | Content-Type: application/json | Hash: {{ sha256 .data }} diff --git a/e2e/cases/01-general/test.goat b/e2e/cases/01-general/test.goat index 7253af4..01061b7 100644 --- a/e2e/cases/01-general/test.goat +++ b/e2e/cases/01-general/test.goat @@ -6,7 +6,7 @@ filterBy = ["date", "name", "age"] page = 2 count = 100 -[Headers] +[Header] Content-Type: text/plain X-Foo: bar diff --git a/e2e/cases/03-fileimport/a/_a.goat b/e2e/cases/03-fileimport/a/_a.goat index 2067762..7654003 100644 --- a/e2e/cases/03-fileimport/a/_a.goat +++ b/e2e/cases/03-fileimport/a/_a.goat @@ -6,7 +6,7 @@ filterBy = ["date", "name", "age"] page = 2 count = 100 -[Headers] +[Header] Content-Type: text/plain X-Foo: bar diff --git a/e2e/cases/03-fileimport/direct.goat b/e2e/cases/03-fileimport/direct.goat index 4406f89..3a6c9d5 100644 --- a/e2e/cases/03-fileimport/direct.goat +++ b/e2e/cases/03-fileimport/direct.goat @@ -6,7 +6,7 @@ filterBy = ["date", "name", "age"] page = 2 count = 100 -[Headers] +[Header] Content-Type: text/plain X-Foo: bar From e782cc1f816c55fae3bd679d2ce8224d6c7c0978 Mon Sep 17 00:00:00 2001 From: zekroTJA Date: Thu, 6 Apr 2023 09:09:19 +0000 Subject: [PATCH 3/5] add `[PreScript]` implementation [#20] --- e2e/cases/05-prescript/run.sh | 1 + e2e/cases/05-prescript/test.goat | 22 ++++++++++++++++++++++ pkg/executor/executor.go | 20 +++++++++++++++++--- pkg/goatfile/goatfile.go | 2 +- pkg/goatfile/parser.go | 7 +++++++ pkg/goatfile/request.go | 12 +++++++----- 6 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 e2e/cases/05-prescript/run.sh create mode 100644 e2e/cases/05-prescript/test.goat diff --git a/e2e/cases/05-prescript/run.sh b/e2e/cases/05-prescript/run.sh new file mode 100644 index 0000000..94b0dfe --- /dev/null +++ b/e2e/cases/05-prescript/run.sh @@ -0,0 +1 @@ +goat test.goat \ No newline at end of file diff --git a/e2e/cases/05-prescript/test.goat b/e2e/cases/05-prescript/test.goat new file mode 100644 index 0000000..e884831 --- /dev/null +++ b/e2e/cases/05-prescript/test.goat @@ -0,0 +1,22 @@ +##### No Prescript + +GET {{.instance}}/test + +[Script] +assert(response.StatusCode === 200); + +##### With Prescript + +GET {{.instance}}/{{.path}} + +[PreScript] +var path = "somepath"; +var body = JSON.stringify({"foo": "bar"}); + +[Body] +{{.body}} + +[Script] +assert(response.StatusCode === 200); +assert(response.BodyJson.path === "/somepath"); +assert(response.BodyJson.body_string === '{"foo":"bar"}\n'); \ No newline at end of file diff --git a/pkg/executor/executor.go b/pkg/executor/executor.go index 249aeba..513c301 100644 --- a/pkg/executor/executor.go +++ b/pkg/executor/executor.go @@ -391,6 +391,18 @@ func (t *Executor) executeAction(log rogu.Logger, eng engine.Engine, act goatfil func (t *Executor) executeRequest(eng engine.Engine, req goatfile.Request) (err error) { + preScript, err := util.ReadReaderToString(req.PreScript.Reader()) + if err != nil { + return errs.WithPrefix("reading preScript failed:", err) + } + + if preScript != "" { + err = eng.Run(preScript) + if err != nil { + return errs.WithPrefix("preScript failed:", err) + } + } + state := eng.State() err = req.ParseWithParams(state) if err != nil { @@ -438,9 +450,11 @@ func (t *Executor) executeRequest(eng engine.Engine, req goatfile.Request) (err return errs.WithPrefix("reading script failed:", err) } - err = eng.Run(script) - if err != nil { - return errs.WithPrefix("script failed:", err) + if script != "" { + err = eng.Run(script) + if err != nil { + return errs.WithPrefix("script failed:", err) + } } return nil diff --git a/pkg/goatfile/goatfile.go b/pkg/goatfile/goatfile.go index 75a132c..03bafbc 100644 --- a/pkg/goatfile/goatfile.go +++ b/pkg/goatfile/goatfile.go @@ -27,9 +27,9 @@ const ( optionNameHeader = optionName("header") optionNameHeaders = optionName("headers") optionNameBody = optionName("body") + optionNamePreScript = optionName("prescript") optionNameScript = optionName("script") optionNameOptions = optionName("options") - abc = optionName("asd") ) // Goatfile holds all sections and diff --git a/pkg/goatfile/parser.go b/pkg/goatfile/parser.go index a482c52..711124f 100644 --- a/pkg/goatfile/parser.go +++ b/pkg/goatfile/parser.go @@ -298,6 +298,13 @@ func (t *Parser) parseBlock(req *requestParseChecker) error { } req.Body = raw + case optionNamePreScript: + raw, err := t.parseRaw() + if err != nil { + return err + } + req.PreScript = raw + case optionNameScript: raw, err := t.parseRaw() if err != nil { diff --git a/pkg/goatfile/request.go b/pkg/goatfile/request.go index a45d8e3..db3e445 100644 --- a/pkg/goatfile/request.go +++ b/pkg/goatfile/request.go @@ -16,11 +16,12 @@ import ( type Request struct { Opts - Method string - URI string - Header http.Header - Body Data - Script Data + Method string + URI string + Header http.Header + Body Data + PreScript Data + Script Data parsed bool } @@ -30,6 +31,7 @@ var _ Action = (*Request)(nil) func newRequest() (r Request) { r.Header = http.Header{} r.Body = NoContent{} + r.PreScript = NoContent{} r.Script = NoContent{} return r } From 27397692c4338005e1d9bb8d5a5741c631a97082 Mon Sep 17 00:00:00 2001 From: zekroTJA Date: Thu, 6 Apr 2023 09:20:45 +0000 Subject: [PATCH 4/5] update docs --- docs/goatfile-spec.md | 6 ++++++ docs/implementation.md | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/docs/goatfile-spec.md b/docs/goatfile-spec.md index 8652d8a..7a6ce6e 100644 --- a/docs/goatfile-spec.md +++ b/docs/goatfile-spec.md @@ -9,6 +9,8 @@ A Goatfile is an UTF-8 encoded plain text file with the file extension `.goat`. | Section Heading | ### Setup | +Context Section | ##### Upload Tests + | Method & URL | POST https://example.com | Headers | [Header] @@ -64,6 +66,10 @@ A Goatfile consists of sections containing one or more requests. Each section ha When two Goatfiles are merged (for example on importing one into another), all requests in all sections of one file are appended to the requests in the sections of the other file. The specific order of the sections in the Goatfile is irrelevant. +### Context Sections + +These sections do not alter the execution of the specified requests in a batch and are just there to be used to provide additional context during the execution. For example, these sections can be used to be logged to visualy separate different sections in a batch. + ### Requests A request consists of the following parts. diff --git a/docs/implementation.md b/docs/implementation.md index 1c39f91..f1ebb2e 100644 --- a/docs/implementation.md +++ b/docs/implementation.md @@ -18,6 +18,27 @@ Name | Type | Default | Description `condition` | `boolean` | `true` | Whether or not to execute the request. `delay` | `string` | `0` | A duration which is awaited before the request is executed. The duration must be formatted in compatibility to Go's [ParseDuration](https://pkg.go.dev/time#ParseDuration) function. +### `PreScript` + +The "PreScript" is executed before the actual request is infused with parameters and executed afterwards. This allows setting request parameters via scripting before the execution of a request. + +Example: +``` +GET https://echo.zekro.de/{{.path}} + +[PreScript] +var path = "somepath"; +var body = JSON.stringify({"foo": "bar"}); + +[Body] +{{.body}} + +[Script] +assert(response.StatusCode === 200); +assert(response.BodyJson.path === "/somepath"); +assert(response.BodyJson.body_string === '{"foo":"bar"}\n'); +``` + ## Script Implementation This implementation is using ECMAScript 5. Inm detail, it is using the [goja](https://github.com/dop251/goja) engine to perform the script part. From 81a9398ef3f30f33adfe38794e96eee9da442e67 Mon Sep 17 00:00:00 2001 From: zekroTJA Date: Thu, 6 Apr 2023 09:25:05 +0000 Subject: [PATCH 5/5] update changelog --- CHANGELOG.md | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 79504fa..c2e9aa5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,13 +1,4 @@ [VERSION] -> **Wran** -> The option name `[Headers]` is now **deprecated** and support will be removed in release v1.0.0! Please use `[Header]` as option name instead! -> -> Originally, the support for both `Header` and `Headers` as name for the headers option block has been added to avoid errors because both terms have been used somewhat interchangably in the past. But because we want the specification of Goatfiles to be as specific as possible, this support will now be dropped before the official release. - -- The definition of the same block multiple times in the same request will now fail the parsing step with an error message alike the following. [#19] - ``` - 2023-03-20T09:35:53Z FATAL execution failed error="failed parsing goatfile at test/test.goat:14:9: [script]: the section has been already defined" - ``` -- Allow passing multiple Goatfile locations as positional argument. [#21] -- Fixed a bug where the wrong files were listed on batch error. \ No newline at end of file +- Add `[PreScript]` request block. [#20] +- Fix log section spacers and add color to them. 🎨 \ No newline at end of file