-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
request.go
173 lines (155 loc) · 3.97 KB
/
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
package http
import (
"context"
"crypto/tls"
"fmt"
"io"
"net/http"
"net/url"
"reflect"
"strings"
"testing"
"github.com/jtacoma/uritemplates"
"github.com/plgd-dev/go-coap/v3/message"
"github.com/plgd-dev/kit/v2/codec/cbor"
"github.com/plgd-dev/kit/v2/codec/json"
"github.com/stretchr/testify/require"
)
type HTTPRequestBuilder struct {
method string
body io.Reader
uri string
uriParams map[string]interface{}
header map[string]string
queryParams map[string][]string
resourceHref string
query string
}
func NewHTTPRequest(method, url string, body io.Reader) *HTTPRequestBuilder {
b := HTTPRequestBuilder{
method: method,
body: body,
uri: url,
uriParams: make(map[string]interface{}),
header: make(map[string]string),
queryParams: make(map[string][]string),
}
return &b
}
func (c *HTTPRequestBuilder) AuthToken(token string) *HTTPRequestBuilder {
c.header["Authorization"] = fmt.Sprintf("bearer %s", token)
return c
}
func (c *HTTPRequestBuilder) AddContentQuery(content string) *HTTPRequestBuilder {
c.AddQuery(ContentQueryKey, content)
return c
}
func (c *HTTPRequestBuilder) AddQuery(key string, value ...string) *HTTPRequestBuilder {
c.queryParams[key] = append(c.queryParams[key], value...)
return c
}
func (c *HTTPRequestBuilder) Accept(accept string) *HTTPRequestBuilder {
if accept == "" {
return c
}
c.header["Accept"] = accept
return c
}
func (c *HTTPRequestBuilder) DeviceId(deviceID string) *HTTPRequestBuilder {
if deviceID == "" {
return c
}
c.uriParams[DeviceIDKey] = deviceID
return c
}
func (c *HTTPRequestBuilder) ResourceHref(resourceHref string) *HTTPRequestBuilder {
if resourceHref == "" {
return c
}
if len(resourceHref) > 0 && resourceHref[0] == '/' {
resourceHref = resourceHref[1:]
}
c.resourceHref = resourceHref
return c
}
func (c *HTTPRequestBuilder) SubscriptionID(subscriptionID string) *HTTPRequestBuilder {
if subscriptionID == "" {
return c
}
c.uriParams[SubscriptionIDKey] = subscriptionID
return c
}
func (c *HTTPRequestBuilder) SetQuery(value string) *HTTPRequestBuilder {
c.query = value
return c
}
func (c *HTTPRequestBuilder) Build(ctx context.Context, t *testing.T) *http.Request {
u := c.uri
if len(c.resourceHref) > 0 {
u = strings.ReplaceAll(c.uri, "{"+ResourceHrefKey+"}", c.resourceHref)
}
tmp, err := uritemplates.Parse(u)
require.NoError(t, err)
uri, err := tmp.Expand(c.uriParams)
require.NoError(t, err)
url, err := url.Parse(uri)
require.NoError(t, err)
query := url.Query()
for k, vals := range c.queryParams {
for _, v := range vals {
query.Add(k, v)
}
}
if c.query != "" {
url.RawQuery = c.query
} else {
url.RawQuery = query.Encode()
}
url.RawQuery = query.Encode()
fmt.Printf("URL %v\n", url.String())
request, _ := http.NewRequestWithContext(ctx, c.method, url.String(), c.body)
for k, v := range c.header {
request.Header.Add(k, v)
}
return request
}
func DoHTTPRequest(t *testing.T, req *http.Request) *http.Response {
trans := http.DefaultTransport.(*http.Transport).Clone()
trans.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
c := http.Client{
Transport: trans,
}
resp, err := c.Do(req)
require.NoError(t, err)
return resp
}
func ReadHTTPResponse(t *testing.T, w io.Reader, contentType string) interface{} {
var data interface{}
readFrom := func(w io.Reader, v interface{}) error {
return fmt.Errorf("not supported")
}
switch contentType {
case message.AppJSON.String():
readFrom = json.ReadFrom
case message.AppCBOR.String(), message.AppOcfCbor.String():
readFrom = cbor.ReadFrom
case "text/plain":
readFrom = func(w io.Reader, v interface{}) error {
b, err := io.ReadAll(w)
if err != nil {
return err
}
val := reflect.ValueOf(v)
if val.Kind() != reflect.Ptr {
return fmt.Errorf("some: check must be a pointer")
}
val.Elem().Set(reflect.ValueOf(string(b)))
return nil
}
}
err := readFrom(w, &data)
require.NoError(t, err)
return data
}