forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_test.go
62 lines (48 loc) · 1.28 KB
/
http_test.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
package provider
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/evcc-io/evcc/util"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type httpHandler struct {
val string
req *http.Request
}
func (h *httpHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
h.req = req
h.val = lo.RandomString(16, lo.LettersCharset)
_, _ = w.Write([]byte(h.val))
}
func TestHttpGet(t *testing.T) {
h := new(httpHandler)
srv := httptest.NewServer(h)
defer srv.Close()
uri := srv.URL + "/foo/bar"
p := NewHTTP(util.NewLogger("foo"), http.MethodGet, uri, false, 1, 0)
uriUrl, _ := url.Parse(uri)
g, err := p.StringGetter()
require.NoError(t, err)
res, err := g()
require.NoError(t, err)
assert.Equal(t, uriUrl.Path, h.req.URL.Path)
assert.Equal(t, h.val, res)
}
func TestHttpSet(t *testing.T) {
h := new(httpHandler)
srv := httptest.NewServer(h)
defer srv.Close()
uri := srv.URL + "/foo/bar?baz={{.baz}}"
p := NewHTTP(util.NewLogger("foo"), http.MethodGet, uri, false, 1, 0)
uriUrl, _ := url.Parse(uri)
s, err := p.StringSetter("baz")
require.NoError(t, err)
err = s("4711")
require.NoError(t, err)
assert.Equal(t, uriUrl.Path, h.req.URL.Path)
assert.Equal(t, "baz=4711", h.req.URL.RawQuery)
}