-
Notifications
You must be signed in to change notification settings - Fork 35
/
run_builder.go
179 lines (154 loc) · 3.63 KB
/
run_builder.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package postman
import (
"encoding/json"
"fmt"
"strings"
)
// build to build the objects that will serialize into the run.yaml, send.yaml, request.json and environment json files
const RunYaml = `
init:
{{INIT_PART}}
pipeline:
info:
action: print
message: $environment
runRequests:
tag: $pathMatch
data:
'${tagId}.[]requests': '@request.json'
subPath: requests/${index}_*
range: 1..{{REQUEST_LEN}}
template:
run:
init:
tagId: $tagId
action: run
request: '@send'
`
const SendYaml = `
init:
req: ${data.${tagId}.requests}
pipeline:
send:
action: 'http/runner:send'
requests: $req
test:
workflow: assert
actual:
Code: $send.Responses[0].Code
expected:
Code: 404
`
type cookieBuilder struct {
Domain string
Expires string
HttpOnly bool
MaxAge int
Name string
Path string
Raw string
RawExpires string
SameSite int
Secure bool
Unparsed []string
Value string
}
type requestBuilder struct {
Body string `json:",omitempty"`
Cookies []*cookieBuilder `json:",omitempty"`
Header map[string][]string `json:",omitempty"`
Method string
URL string
name string
}
func (b *requestBuilder) addCookie() *cookieBuilder {
c := &cookieBuilder{}
b.Cookies = append(b.Cookies, c)
return c
}
func (b *requestBuilder) addHeader(key string, value []string) {
b.Header[key] = value
}
func (b *requestBuilder) TOJson() (string, error) {
s, err := json.MarshalIndent(b, "", " ")
if err != nil {
return "", err
}
return string(s), nil
}
type environmentBuilder struct {
Variables map[string]string
name string
}
func (b *environmentBuilder) addVariable(key string, value string) {
b.Variables[key] = value
}
func (b *environmentBuilder) TOJson() (string, error) {
s, err := json.MarshalIndent(b.Variables, "", " ")
if err != nil {
return "", err
}
return string(s), nil
}
type runBuilder struct {
variables map[string]interface{}
requests []*requestBuilder
environments []*environmentBuilder
}
func (b *runBuilder) addEnvironment(name string) *environmentBuilder {
e := &environmentBuilder{
Variables: make(map[string]string),
name: name,
}
b.environments = append(b.environments, e)
return e
}
func (b *runBuilder) addRequest(method string, uRL string, name string) *requestBuilder {
r := &requestBuilder{
Method: method,
URL: uRL,
name: name,
Header: make(map[string][]string),
}
b.requests = append(b.requests, r)
return r
}
func (b *runBuilder) addVariable(key string, value interface{}) {
b.variables[key] = value
}
func (b *runBuilder) sendToYaml() string {
return SendYaml
}
func (b *runBuilder) toYamlInitPart() string {
var sb strings.Builder
if len(b.environments) > 0 {
sb.WriteString("environment: ")
sb.WriteString("$AsData($Cat('")
sb.WriteString(makeDirOrFileName(b.environments[0].name))
sb.WriteString(".json'))\n")
for k := range b.environments[0].Variables {
sb.WriteString(" ")
sb.WriteString(k)
sb.WriteString(": $environment.")
sb.WriteString(k)
sb.WriteString("\n")
}
}
for k, v := range b.variables {
sb.WriteString(" ")
sb.WriteString(k)
sb.WriteString(": ")
sb.WriteString(v.(string))
sb.WriteString("\n")
}
return sb.String()
}
func (b *runBuilder) runToYaml() string {
l := strings.TrimLeft(fmt.Sprintf("%03s", fmt.Sprint(len(b.requests))), " ")
return strings.ReplaceAll(strings.ReplaceAll(RunYaml, "{{INIT_PART}}", b.toYamlInitPart()), "{{REQUEST_LEN}}", l)
}
func NewRunBuilder() *runBuilder {
return &runBuilder{
variables: make(map[string]interface{}),
}
}