-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathrequest_test.go
87 lines (75 loc) · 1.58 KB
/
request_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
package awsping
import (
"errors"
"net"
"net/http"
"testing"
)
type testTCPClient struct {
err error
}
func (c *testTCPClient) Dial(n, a string) (net.Conn, error) {
if c.err != nil {
return nil, c.err
}
var con net.Conn
return con, nil
}
type testHTTPClient struct {
err error
}
func (c *testHTTPClient) Do(r *http.Request) (*http.Response, error) {
if c.err != nil {
return nil, c.err
}
return &http.Response{}, nil
}
func TestRequestDoTCPError(t *testing.T) {
r := &AWSRequest{
tcpClient: &testTCPClient{
err: net.ErrWriteToConnected,
},
}
l, err := r.DoTCP("net", "some-addr")
if err == nil {
t.Errorf("Error should not be empty")
}
if !errors.Is(err, net.ErrWriteToConnected) {
t.Errorf("Want=%v, got=%v", net.ErrWriteToConnected, err)
}
if l != 0 {
t.Errorf("Latency for error should be 0, but got=%d", l)
}
}
func TestDoErr(t *testing.T) {
errTCP := errors.New("error from tcp")
errHTTP := errors.New("error from http")
r := &AWSRequest{
tcpClient: &testTCPClient{
err: errTCP,
},
httpClient: &testHTTPClient{
err: errHTTP,
},
}
l, err := r.Do("ua", "addr", RequestTypeTCP)
if err == nil {
t.Errorf("Error should not be empty")
}
if !errors.Is(err, errTCP) {
t.Errorf("Want=%v, got=%v", errTCP, err)
}
if l != 0 {
t.Errorf("Latency for error should be 0, but got=%d", l)
}
l, err = r.Do("ua", "addr", RequestTypeHTTP)
if err == nil {
t.Errorf("Error should not be empty")
}
if !errors.Is(err, errHTTP) {
t.Errorf("Want=%v, got=%v", errHTTP, err)
}
if l != 0 {
t.Errorf("Latency for error should be 0, but got=%d", l)
}
}