-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_test.go
134 lines (113 loc) · 3.76 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
126
127
128
129
130
131
132
133
134
package restconf
import (
"errors"
"io/ioutil"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
)
const (
testURL = "https://10.0.0.1"
)
func testClient() *Client {
client, _ := NewClient(testURL, "usr", "pwd", true, MaxRetries(0))
gock.InterceptClient(client.HttpClient)
gock.New(testURL).Get("/.well-known/host-meta").Reply(200).BodyString(`<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'><Link rel='restconf' href='/restconf'/></XRD>`)
gock.New(testURL).Get("/restconf/data/ietf-restconf-monitoring:restconf-state/capabilities").Reply(200).BodyString(`{"ietf-restconf-monitoring:capabilities": {"capability": ["urn:ietf:params:restconf:capability:yang-patch:1.0"]}}`)
return client
}
// ErrReader implements the io.Reader interface and fails on Read.
type ErrReader struct{}
// Read mocks failing io.Reader test cases.
func (r ErrReader) Read(buf []byte) (int, error) {
return 0, errors.New("fail")
}
// TestNewClient tests the NewClient function.
func TestNewClient(t *testing.T) {
client, _ := NewClient(testURL, "usr", "pwd", true, RequestTimeout(120), MaxRetries(0))
assert.Equal(t, client.HttpClient.Timeout, 120*time.Second)
assert.Equal(t, client.MaxRetries, 0)
}
// TestDiscoverRestconfEndpoint tests the Client::discoverRestconfEndpoint method.
func TestDiscoverRestconfEndpoint(t *testing.T) {
defer gock.Off()
client := testClient()
client.discoverRestconfEndpoint()
assert.Equal(t, client.RestconfEndpoint, "/restconf")
}
func TestDiscoverCapabilities(t *testing.T) {
defer gock.Off()
client := testClient()
client.discoverRestconfEndpoint()
client.discoverCapabilities()
assert.Equal(t, client.Capabilities, []string{"urn:ietf:params:restconf:capability:yang-patch:1.0"})
assert.Equal(t, client.YangPatchCapability, true)
}
// TestClientGet tests the Client::GetData method.
func TestClientGetData(t *testing.T) {
defer gock.Off()
client := testClient()
var err error
// Success
gock.New(testURL).Get("/restconf/data/url").Reply(200)
_, err = client.GetData("url")
assert.NoError(t, err)
// HTTP error
gock.New(testURL).Get("/restconf/data/url").ReplyError(errors.New("fail"))
_, err = client.GetData("url")
assert.Error(t, err)
// Invalid HTTP status code
gock.New(testURL).Get("/restconf/data/url").Reply(405)
res, _ := client.GetData("url")
assert.Equal(t, res.StatusCode, 405)
// Error decoding response body
gock.New(testURL).
Get("/restconf/data/url").
Reply(200).
Map(func(res *http.Response) *http.Response {
res.Body = ioutil.NopCloser(ErrReader{})
return res
})
_, err = client.GetData("url")
assert.Error(t, err)
}
// TestClientPostData tests the Client::PostData method.
func TestClientPostData(t *testing.T) {
defer gock.Off()
client := testClient()
var err error
// Success
gock.New(testURL).Post("/restconf/data/url").Reply(200)
_, err = client.PostData("url", "{}")
assert.NoError(t, err)
// HTTP error
gock.New(testURL).Post("/restconf/data/url").ReplyError(errors.New("fail"))
_, err = client.PostData("url", "{}")
assert.Error(t, err)
// Invalid HTTP status code
gock.New(testURL).Post("/restconf/data/url").Reply(405)
res, _ := client.PostData("url", "{}")
assert.Equal(t, res.StatusCode, 405)
// Error decoding response body
gock.New(testURL).
Post("/restconf/data/url").
Reply(200).
Map(func(res *http.Response) *http.Response {
res.Body = ioutil.NopCloser(ErrReader{})
return res
})
_, err = client.PostData("url", "{}")
assert.Error(t, err)
}
// TestBackoff tests the Client::Backoff method.
func TestBackoff(t *testing.T) {
client := testClient()
client.MaxRetries = 1
client.BackoffMinDelay = 1
start := time.Now()
client.Backoff(0)
duration := time.Since(start)
assert.GreaterOrEqual(t, duration.Seconds(), float64(client.BackoffMinDelay))
}