-
Notifications
You must be signed in to change notification settings - Fork 195
/
rawrequest.go
161 lines (132 loc) · 3.48 KB
/
rawrequest.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
package placeholder
import (
"crypto/sha256"
"io"
"net/http"
"strings"
"github.com/pkg/errors"
)
type RawRequest struct {
name string
}
type RawRequestConfig struct {
Method string
Path string
Headers map[string]string
Body string
}
var DefaultRawRequest = RawRequest{name: "RawRequest"}
var _ Placeholder = (*RawRequest)(nil)
var _ PlaceholderConfig = (*RawRequestConfig)(nil)
func (p RawRequest) newConfig(conf map[any]any) (PlaceholderConfig, error) {
result := &RawRequestConfig{}
method, ok := conf["method"]
if !ok {
return nil, &BadPlaceholderConfigError{
name: p.name,
err: errors.New("empty method"),
}
}
result.Method, ok = method.(string)
if !ok {
return nil, &BadPlaceholderConfigError{
name: p.name,
err: errors.Errorf("unknown type of 'method' field, expected string, got %T", method),
}
}
path, ok := conf["path"]
if !ok {
result.Path = "/"
} else {
result.Path, ok = path.(string)
if !ok {
return nil, &BadPlaceholderConfigError{
name: p.name,
err: errors.Errorf("unknown type of 'path' field, expected string, got %T", path),
}
}
if len(result.Path) == 0 {
result.Path = "/"
}
}
result.Headers = make(map[string]string)
headers, ok := conf["headers"]
if ok {
typedHeaders, ok := headers.(map[any]any)
if !ok {
return nil, &BadPlaceholderConfigError{
name: p.name,
err: errors.Errorf("unknown type of 'headers' field, expected map[string]string, got %T", typedHeaders),
}
}
for k, v := range typedHeaders {
header, okHeader := k.(string)
value, okValue := v.(string)
if !okHeader || !okValue {
return nil, &BadPlaceholderConfigError{
name: p.name,
err: errors.Errorf("unknown type of 'headers' field, expected map[string]string, got map[%T]%T", k, v),
}
}
result.Headers[header] = value
}
}
body, ok := conf["body"]
if ok {
result.Body, ok = body.(string)
if !ok {
return nil, &BadPlaceholderConfigError{
name: p.name,
err: errors.Errorf("unknown type of 'body' field, expected string, got %T", body),
}
}
}
return result, nil
}
func (p RawRequest) GetName() string {
return p.name
}
// CreateRequest creates a new request from config.
// config must be a RawRequestConfig struct.
func (p RawRequest) CreateRequest(requestURL, payload string, config PlaceholderConfig) (*http.Request, error) {
conf, ok := config.(*RawRequestConfig)
if !ok {
return nil, &BadPlaceholderConfigError{
name: p.name,
err: errors.Errorf("bad config type: got %T, expected: %T", config, &RawRequestConfig{}),
}
}
if !strings.HasSuffix(requestURL, "/") {
requestURL += "/"
}
if strings.HasPrefix(conf.Path, "/") {
requestURL += conf.Path[1:]
} else {
requestURL += conf.Path
}
requestURL = strings.ReplaceAll(requestURL, payloadPlaceholder, payload)
var bodyReader io.Reader
body := strings.ReplaceAll(conf.Body, payloadPlaceholder, payload)
if len(body) != 0 {
bodyReader = strings.NewReader(body)
}
req, err := http.NewRequest(conf.Method, requestURL, bodyReader)
if err != nil {
return nil, err
}
for k, v := range conf.Headers {
req.Header.Add(k, strings.ReplaceAll(v, payloadPlaceholder, payload))
}
return req, nil
}
func (r *RawRequestConfig) Hash() []byte {
sha256sum := sha256.New()
sha256sum.Write([]byte(r.Method))
sha256sum.Write([]byte(r.Path))
for header, value := range r.Headers {
sha256sum.Write([]byte(header))
sha256sum.Write([]byte(value))
}
sha256sum.Write([]byte(r.Body))
return sha256sum.Sum(nil)
}