Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
zekroTJA committed Apr 6, 2023
2 parents 5001b54 + 81a9398 commit 19de563
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 16 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[VERSION]

- Added Log Sections [#23]
- Add `[PreScript]` request block. [#20]
- Fix log section spacers and add color to them. 🎨
8 changes: 7 additions & 1 deletion docs/goatfile-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ 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 | [Headers]
Headers | [Header]
| X-Requested-With: XMLHttpRequest
| Content-Type: application/json
| Hash: {{ sha256 .data }}
Expand Down Expand Up @@ -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.
Expand Down
21 changes: 21 additions & 0 deletions docs/implementation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion e2e/cases/01-general/test.goat
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ filterBy = ["date", "name", "age"]
page = 2
count = 100

[Headers]
[Header]
Content-Type: text/plain
X-Foo: bar

Expand Down
2 changes: 1 addition & 1 deletion e2e/cases/03-fileimport/a/_a.goat
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ filterBy = ["date", "name", "age"]
page = 2
count = 100

[Headers]
[Header]
Content-Type: text/plain
X-Foo: bar

Expand Down
2 changes: 1 addition & 1 deletion e2e/cases/03-fileimport/direct.goat
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ filterBy = ["date", "name", "age"]
page = 2
count = 100

[Headers]
[Header]
Content-Type: text/plain
X-Foo: bar

Expand Down
1 change: 1 addition & 0 deletions e2e/cases/05-prescript/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
goat test.goat
22 changes: 22 additions & 0 deletions e2e/cases/05-prescript/test.goat
Original file line number Diff line number Diff line change
@@ -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');
25 changes: 20 additions & 5 deletions pkg/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -390,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 {
Expand Down Expand Up @@ -437,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
Expand Down
2 changes: 1 addition & 1 deletion pkg/goatfile/goatfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions pkg/goatfile/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 7 additions & 5 deletions pkg/goatfile/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit 19de563

Please sign in to comment.