forked from ory/kratos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
128 lines (108 loc) · 2.81 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
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
package x
import (
"io"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"testing"
"github.com/golang/gddo/httputil"
"github.com/ory/herodot"
"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 EasyGetJSON(t *testing.T, c *http.Client, url string) (*http.Response, []byte) {
req, err := http.NewRequest("GET", url, nil)
require.NoError(t, err)
req.Header.Set("Accept", "application/json")
res, err := c.Do(req)
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)
}
func NewTransportWithHost(host string) *TransportWithHost {
return &TransportWithHost{
RoundTripper: http.DefaultTransport,
host: host,
}
}
type TransportWithHost struct {
http.RoundTripper
host string
}
func (ct *TransportWithHost) RoundTrip(req *http.Request) (*http.Response, error) {
req.Host = ct.host
return ct.RoundTripper.RoundTrip(req)
}
func AcceptToRedirectOrJson(
w http.ResponseWriter, r *http.Request, writer herodot.Writer, out interface{}, redirectTo string,
) {
switch httputil.NegotiateContentType(r, []string{
"text/html",
"application/json",
}, "text/html") {
case "application/json":
writer.Write(w, r, out)
case "text/html":
fallthrough
default:
http.Redirect(w, r, redirectTo, http.StatusSeeOther)
}
}
func AcceptsJSON(r *http.Request) bool {
return httputil.NegotiateContentType(r, []string{
"text/html",
"application/json",
}, "text/html") == "application/json"
}