-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
125 lines (111 loc) · 2.77 KB
/
client_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
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
package httpx_test
import (
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/tflyons/httpx"
)
var echoHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h := w.Header()
for k, v := range r.Header {
for i := range v {
h.Add(k, v[i])
}
}
for _, cookie := range r.Cookies() {
http.SetCookie(w, cookie)
}
_, err:=io.Copy(w, r.Body)
if err != nil {
log.Fatal(err)
}
})
func TestClient_JSON(t *testing.T) {
srv := httptest.NewServer(echoHandler)
defer srv.Close()
var c httpx.Client = srv.Client()
input := map[string]string{
"hello": "world",
}
output := make(map[string]string)
c = httpx.SetRequestBodyJSON(c, input)
c = httpx.SetResponseBodyHandlerJSON(c, &output)
c = httpx.SetRequest(c, http.MethodPost, srv.URL)
if _, err := c.Do(nil); err != nil {
t.Fatal(err)
}
if output["hello"] != "world" {
t.Fatal(output)
}
}
func TestClient_SetCookies(t *testing.T) {
srv := httptest.NewServer(echoHandler)
defer srv.Close()
var c httpx.Client = srv.Client()
c = httpx.AddCookies(c, &http.Cookie{
Name: "my_cookie",
Domain: "",
Path: "",
})
c = httpx.SetRequest(c, http.MethodGet, srv.URL)
resp, err := c.Do(nil)
if err != nil {
t.Fatal(err)
}
cookie := resp.Cookies()
if cookie[0].Name != "my_cookie" {
t.Fatal(cookie[0])
}
}
func TestRateLimit(t *testing.T) {
srv := httptest.NewServer(echoHandler)
defer srv.Close()
var c httpx.Client = srv.Client()
rateLimit := 20
d := time.Millisecond * 50
n := 10
c = httpx.SetRateLimit(c, rateLimit, d)
c = httpx.SetRequest(c, http.MethodGet, srv.URL)
// make enough requests to get rate limited n times
start := time.Now()
for i := 0; i < rateLimit*n; i++ {
if _, err := c.Do(nil); err != nil {
t.Fatal(err)
}
}
end := time.Now()
// the time difference should be greater than or equal to n durations
if end.Sub(start) < d*time.Duration(n) {
t.Fatal("expected time delay due to rate limit")
}
}
func ExampleClient() {
c := httpx.DefaultClient
// set a header to be sent on every request
c = httpx.SetHeader(c, "some-header", "1234")
// limit the total request volume to 100 per minute
c = httpx.SetRateLimit(c, 100, time.Minute)
// limit every request to 30 seconds round trip
c = httpx.SetTimeout(c, time.Second*30)
// set an initializer to load a token from a file into the header prior to doing calls
c = httpx.SetInitializer(c, func(next httpx.Client) (httpx.ClientFunc, error) {
token, err := os.ReadFile("mytoken.txt")
if err != nil {
return nil, err
}
return httpx.SetHeader(next, "SOME-TOKEN", string(token)), nil
})
req, err := http.NewRequest(http.MethodGet, "example.com", nil)
if err != nil {
log.Fatal(err)
}
resp, err := c.Do(req)
if err != nil {
log.Fatal(err)
}
log.Println(resp.StatusCode)
}