-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_test.go
More file actions
127 lines (110 loc) · 2.8 KB
/
fetch_test.go
File metadata and controls
127 lines (110 loc) · 2.8 KB
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
package fetch_test
import (
"testing"
"time"
"github.com/sony/gobreaker/v2"
"github.com/stretchr/testify/require"
"github.com/tinh-tinh/fetch/v2"
)
func Test_Get(t *testing.T) {
instance := fetch.Create(&fetch.Config{
BaseUrl: "https://jsonplaceholder.typicode.com",
})
type Post struct {
UserID int `json:"userId"`
ID int `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
}
res := instance.Get("posts")
require.Nil(t, res.Error)
require.Equal(t, 200, res.Status)
type QueryComment struct {
PostID int `query:"postId"`
}
res = instance.Post("posts", &Post{
UserID: 1,
Title: "foo",
Body: "bar",
}, &QueryComment{
PostID: 1,
})
require.Nil(t, res.Error)
require.Equal(t, 201, res.Status)
res = instance.Put("posts/1", &Post{
UserID: 1,
Title: "foo",
Body: "bar",
}, &QueryComment{
PostID: 1,
})
require.Nil(t, res.Error)
require.Equal(t, 200, res.Status)
res = instance.Patch("posts/1", &Post{
UserID: 1,
Title: "foo",
Body: "bar",
}, &QueryComment{
PostID: 1,
})
require.Nil(t, res.Error)
require.Equal(t, 200, res.Status)
res = instance.Delete("posts/1", &QueryComment{
PostID: 1,
})
require.Nil(t, res.Error)
require.Equal(t, 200, res.Status)
}
func Test_Timeout(t *testing.T) {
instance := fetch.Create(&fetch.Config{
BaseUrl: "https://jsonplaceholder.typicode.com",
Timeout: 10 * time.Millisecond,
})
resp := instance.Get("comments")
require.NotNil(t, resp.Error)
}
func Test_Cookies(t *testing.T) {
instance := fetch.Create(&fetch.Config{
BaseUrl: "https://google.com",
WithCredentials: true,
})
resp := instance.Get("")
require.Nil(t, resp.Error)
req, err := instance.GetConfig("GET", "", nil)
require.Nil(t, err)
require.NotEmpty(t, req.Cookies())
}
func Test_CircuitBreaker(t *testing.T) {
instance := fetch.Create(&fetch.Config{
BaseUrl: "https://jsonplaceholder.com",
CBSettings: &gobreaker.Settings{
Name: "CB",
ReadyToTrip: func(counts gobreaker.Counts) bool {
failureRatio := float64(counts.TotalFailures) / float64(counts.Requests)
return counts.Requests >= 3 && failureRatio >= 0.6
},
},
Timeout: 5 * time.Second,
})
var resp *fetch.Response
// Intentionally cause failures
for range 5 {
resp = instance.Get("abc")
}
require.NotNil(t, resp.Error)
require.Equal(t, resp.Error, gobreaker.ErrOpenState)
instance2 := fetch.Create(&fetch.Config{
BaseUrl: "https://jsonplaceholder.typicode.com",
CBSettings: &gobreaker.Settings{
Name: "CB2",
ReadyToTrip: func(counts gobreaker.Counts) bool {
failureRatio := float64(counts.TotalFailures) / float64(counts.Requests)
return counts.Requests >= 3 && failureRatio >= 0.6
},
},
Timeout: 5 * time.Second,
})
resp = instance2.Get("posts")
require.Nil(t, resp.Error)
require.Equal(t, 200, resp.Status)
}