-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
72 lines (60 loc) · 1.48 KB
/
http.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
package x
import (
"io"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"testing"
"github.com/stretchr/testify/require"
"github.com/ory/x/stringsx"
)
func NewTestHTTPRequest(t *testing.T, method, url string, body io.Reader) *http.Request {
req, err := http.NewRequest(method, url, body)
require.NoError(t, err)
return req
}
func EasyGet(t *testing.T, c *http.Client, url string) (*http.Response, []byte) {
res, err := c.Get(url)
require.NoError(t, err)
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
require.NoError(t, err)
return res, body
}
func EasyGetBody(t *testing.T, c *http.Client, url string) []byte {
_, body := EasyGet(t, c, url) // nolint: bodyclose
return body
}
func EasyCookieJar(t *testing.T, o *cookiejar.Options) *cookiejar.Jar {
cj, err := cookiejar.New(o)
require.NoError(t, err)
return cj
}
func RequestURL(r *http.Request) *url.URL {
source := *r.URL
source.Host = stringsx.Coalesce(source.Host, r.Host)
if source.Scheme == "" {
source.Scheme = "https"
if r.TLS == nil {
source.Scheme = "http"
}
}
return &source
}
func NewTransportWithHeader(h http.Header) *TransportWithHeader {
return &TransportWithHeader{
RoundTripper: http.DefaultTransport,
h: h,
}
}
type TransportWithHeader struct {
http.RoundTripper
h http.Header
}
func (ct *TransportWithHeader) RoundTrip(req *http.Request) (*http.Response, error) {
for k := range ct.h {
req.Header.Set(k, ct.h.Get(k))
}
return ct.RoundTripper.RoundTrip(req)
}