-
Notifications
You must be signed in to change notification settings - Fork 2
/
runtime.go
160 lines (139 loc) · 3.22 KB
/
runtime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package rq
import (
"bytes"
"context"
"encoding/json"
"io"
"strings"
"github.com/dop251/goja"
)
type Runtime struct {
vm *goja.Runtime
environment map[string]string
request *Request
}
type Assertion struct {
Message string `json:"message"`
Success bool `json:"success"`
}
func (r *Runtime) extractEnvironment() {
value, err := r.vm.RunString(`environment`)
if err != nil {
panic(err)
}
r.environment = value.Export().(map[string]string)
}
func (r *Runtime) extractAssertions() []Assertion {
value, err := r.vm.RunString(`assertions`)
if err != nil {
panic(err)
}
return value.Export().([]Assertion)
}
func (r *Runtime) extractLogs() []string {
value, err := r.vm.RunString(`logs`)
if err != nil {
panic(err)
}
return value.Export().([]string)
}
func (r *Runtime) reset() {
r.request = nil
r.vm.Set("environment", r.environment)
r.vm.Set("request", nil)
r.vm.Set("response", nil)
r.resetLogs()
r.resetAssertions()
}
func (r *Runtime) resetAssertions() {
r.vm.Set("assertions", []Assertion{})
}
func (r *Runtime) resetLogs() {
r.vm.Set("logs", []string{})
}
func (r *Runtime) setRequest(req *Request) {
r.request = req
r.vm.Set("request", map[string]any{
"name": req.Name,
"body": req.Body,
"headers": req.Headers,
"method": req.Method,
"url": req.URL,
})
}
func (r *Runtime) executeScript(script string) error {
_, err := r.vm.RunString(script)
req := r.extractRequest()
if value, ok := req["skip"].(bool); ok {
r.request.Skip = value
}
return err
}
func (r *Runtime) extractRequest() map[string]any {
value, err := r.vm.RunString("request")
if err != nil {
panic(err)
}
return value.Export().(map[string]any)
}
func (r *Runtime) setResponse(resp *Response) {
// tee the body into a string buffer so we can read it multiple times
// without draining the body
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
resp.Body = io.NopCloser(bytes.NewBufferString(string(b)))
respData := map[string]any{
"body": string(b),
"headers": resp.Header,
"status": resp.Status,
"statusCode": resp.StatusCode,
}
if strings.Contains(resp.Header.Get("Content-Type"), "application/json") {
var data any
if err := json.Unmarshal(b, &data); err != nil {
panic(err)
}
respData["json"] = data
}
r.vm.Set("response", respData)
}
// scripts are javascript scripts that are loaded into each runtime instance.
var scripts = []string{
`function assert(condition, message) {
assertions.push({ Message: message, Success: condition })
}`,
`function setEnv(key, value) {
environment[key] = value
}`,
`function getEnv(key) {
return environment[key]
}`,
`function log(entry) {
logs.push(entry)
}
`,
}
type runtimeContextKey struct{}
func WithRuntime(ctx context.Context, rt *Runtime) context.Context {
return WithEnvironment(context.WithValue(ctx, runtimeContextKey{}, rt), rt.environment)
}
func getRuntime(ctx context.Context) *Runtime {
if rt, ok := ctx.Value(runtimeContextKey{}).(*Runtime); ok {
return rt
}
rt := &Runtime{
vm: goja.New(),
environment: GetEnvironment(ctx),
}
for _, script := range scripts {
_, err := rt.vm.RunString(script)
if err != nil {
panic(err)
}
}
rt.reset()
return rt
}