-
Notifications
You must be signed in to change notification settings - Fork 1
/
ted_test.go
91 lines (81 loc) · 1.65 KB
/
ted_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
package ted
import (
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestIsMessageNotModified(t *testing.T) {
var err error = Response{
OK: false,
ErrorCode: 400,
Description: "Bad Request: message is not modified: specified new message content and reply markup are exactly the same as a current content and reply markup of the message",
}
assert.True(t, IsMessageNotModified(err))
}
type tempError struct {
}
func (r tempError) Error() string {
return ""
}
func (r tempError) Temporary() bool {
return true
}
type result struct {
res *http.Response
err error
}
type httpClient struct {
results []result
}
func (h *httpClient) Do(*http.Request) (*http.Response, error) {
var result result
result, h.results = h.results[0], h.results[1:]
return result.res, result.err
}
func TestBot_doReq(t *testing.T) {
t.Run("retries up to 3 times", func(t *testing.T) {
client := &httpClient{
results: []result{
{
err: tempError{},
},
{
err: tempError{},
},
{
err: tempError{},
},
{
res: &http.Response{Body: ioutil.NopCloser(strings.NewReader(`{"ok":true}`))},
},
},
}
bot := Bot{HTTPClient: client}
response, err := bot.doReq(nil)
assert.NoError(t, err)
assert.True(t, response.OK)
})
t.Run("returns error after running out of retries", func(t *testing.T) {
client := &httpClient{
results: []result{
{
err: tempError{},
},
{
err: tempError{},
},
{
err: tempError{},
},
{
err: tempError{},
},
},
}
bot := Bot{HTTPClient: client}
_, err := bot.doReq(nil)
assert.Error(t, err)
})
}