-
Notifications
You must be signed in to change notification settings - Fork 5
/
gogi_test.go
82 lines (63 loc) · 1.52 KB
/
gogi_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package gogi
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
client *Client
mux *http.ServeMux
server *httptest.Server
)
func setUp() {
mux = http.NewServeMux()
server = httptest.NewServer(mux)
client, _ = NewHTTPClient()
url, _ := url.Parse(server.URL)
client.APIURL = url
}
func tearDown() {
server.Close()
}
func TestNewHTTPClient(t *testing.T) {
c, _ := NewHTTPClient()
assert.Equal(t, ua, c.UserAgent)
assert.Equal(t, defaultAPIURL, c.APIURL.String())
}
func TestNewrequest(t *testing.T) {
c, _ := NewHTTPClient()
req, _ := c.NewRequest("GET", "/foo", nil)
assert.Equal(t, defaultAPIURL+"/foo", req.URL.String())
assert.Equal(t, ua, req.Header.Get("User-Agent"))
}
func TestDo(t *testing.T) {
setUp()
defer tearDown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
_, _ = w.Write([]byte("foo"))
})
req, err := client.NewRequest("GET", "/", nil)
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
body, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
res := string(body)
expected := "foo"
assert.Equal(t, expected, res)
}
func TestWithAPIUrl(t *testing.T) {
c, _ := NewHTTPClient()
u := "https://cuonglm.xyz"
require.NoError(t, WithAPIUrl(u)(c))
assert.Equal(t, u, c.APIURL.String())
}
func TestWithHTTPClient(t *testing.T) {
c, _ := NewHTTPClient()
require.Error(t, WithHTTPClient(nil)(c))
}