-
Notifications
You must be signed in to change notification settings - Fork 444
/
http_request.go
188 lines (157 loc) · 4.49 KB
/
http_request.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
180
181
182
183
184
185
186
187
188
package testutils
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"strings"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)
// HttpRequestBuilder simplifies the process of generating http requests in tests
type HttpRequestBuilder struct {
ctx context.Context
method string
scheme string
hostname string
port uint32
path string
body string
host string
headers map[string][]string
}
// DefaultRequestBuilder returns an HttpRequestBuilder with some default values
func DefaultRequestBuilder() *HttpRequestBuilder {
return &HttpRequestBuilder{
ctx: context.Background(),
method: http.MethodGet,
scheme: "http", // https://github.com/golang/go/issues/40587
hostname: "localhost",
port: 0,
path: "",
body: "",
host: "",
headers: make(map[string][]string),
}
}
func (h *HttpRequestBuilder) WithContext(ctx context.Context) *HttpRequestBuilder {
h.ctx = ctx
return h
}
func (h *HttpRequestBuilder) WithPostMethod() *HttpRequestBuilder {
h.method = http.MethodPost
return h
}
func (h *HttpRequestBuilder) WithOptionsMethod() *HttpRequestBuilder {
h.method = http.MethodOptions
return h
}
func (h *HttpRequestBuilder) WithScheme(scheme string) *HttpRequestBuilder {
h.scheme = scheme
return h
}
func (h *HttpRequestBuilder) WithHostname(hostname string) *HttpRequestBuilder {
h.hostname = hostname
return h
}
func (h *HttpRequestBuilder) WithPort(port uint32) *HttpRequestBuilder {
h.port = port
return h
}
func (h *HttpRequestBuilder) WithPath(path string) *HttpRequestBuilder {
h.path = path
return h
}
func (h *HttpRequestBuilder) WithBody(body string) *HttpRequestBuilder {
h.body = body
return h
}
// WithPostBody is syntactic sugar for updating the Method and Body for a POST request simultaneously
func (h *HttpRequestBuilder) WithPostBody(body string) *HttpRequestBuilder {
return h.WithBody(body).WithPostMethod()
}
func (h *HttpRequestBuilder) WithHost(host string) *HttpRequestBuilder {
h.host = host
return h
}
func (h *HttpRequestBuilder) WithContentType(contentType string) *HttpRequestBuilder {
return h.WithHeader("Content-Type", contentType)
}
func (h *HttpRequestBuilder) WithAcceptEncoding(acceptEncoding string) *HttpRequestBuilder {
return h.WithHeader("Accept-Encoding", acceptEncoding)
}
const headerDelimiter = ","
// WithHeader accepts a list of header values, separated by the headerDelimiter
// To set a single value for a header, call:
//
// WithHeader(`headerName`, `value1`)
//
// To set multiple values for a header, call:
//
// WithHeader(`headerName`, `value1,value2`)
func (h *HttpRequestBuilder) WithHeader(key, value string) *HttpRequestBuilder {
h.headers[key] = strings.Split(value, headerDelimiter)
return h
}
// WithRawHeader accepts multiple header values for a key.
// Unlike WithHeader, it does not split the value by a headerDelimiter (,) and instead allows for N values to be
// set as-is.
func (h *HttpRequestBuilder) WithRawHeader(key string, values ...string) *HttpRequestBuilder {
h.headers[key] = values
return h
}
func (h *HttpRequestBuilder) errorIfInvalid() error {
if h.scheme == "" {
return errors.New("scheme is empty, but required")
}
if h.hostname == "" {
return errors.New("hostname is empty, but required")
}
if h.port == 0 {
return errors.New("port is empty, but required")
}
return nil
}
func (h *HttpRequestBuilder) Clone() *HttpRequestBuilder {
if h == nil {
return nil
}
clone := new(HttpRequestBuilder)
clone.ctx = h.ctx
clone.method = h.method
clone.scheme = h.scheme
clone.hostname = h.hostname
clone.port = h.port
clone.path = h.path
clone.body = h.body
clone.host = h.host
clone.headers = make(map[string][]string)
for key, value := range h.headers {
clone.headers[key] = value
}
return clone
}
func (h *HttpRequestBuilder) Build() *http.Request {
if err := h.errorIfInvalid(); err != nil {
// We error loudly here
// These types of errors are intended to prevent developers from creating resources
// which are semantically correct, but lead to test flakes/confusion
ginkgo.Fail(err.Error())
}
// We instantiate a new buffer each time we build a request
var requestBody io.Reader
if h.body != "" {
requestBody = bytes.NewBufferString(h.body)
}
request, err := http.NewRequestWithContext(
h.ctx,
h.method,
fmt.Sprintf("%s://%s:%d/%s", h.scheme, h.hostname, h.port, h.path),
requestBody)
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "generating http request")
request.Host = h.host
request.Header = h.headers
return request
}