-
Notifications
You must be signed in to change notification settings - Fork 723
/
Copy pathutil_test.go
201 lines (168 loc) · 4.77 KB
/
util_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (c) 2015-present Jeevanandam M ([email protected]), All rights reserved.
// resty source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT
package resty
import (
"bytes"
"errors"
"io"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
)
func TestIsJSONContentType(t *testing.T) {
for _, test := range []struct {
input string
expect bool
}{
{"application/json", true},
{"application/xml+json", true},
{"application/vnd.foo+json", true},
{"application/json; charset=utf-8", true},
{"application/vnd.foo+json; charset=utf-8", true},
{"text/json", true},
{"text/vnd.foo+json", true},
{"application/foo-json", true},
{"application/foo.json", true},
{"application/vnd.foo-json", true},
{"application/vnd.foo.json", true},
{"application/x-amz-json-1.1", true},
{"text/foo-json", true},
{"text/foo.json", true},
{"text/vnd.foo-json", true},
{"text/vnd.foo.json", true},
} {
result := isJSONContentType(test.input)
if result != test.expect {
t.Errorf("failed on %q: want %v, got %v", test.input, test.expect, result)
}
}
}
func TestIsXMLContentType(t *testing.T) {
for _, test := range []struct {
input string
expect bool
}{
{"application/xml", true},
{"application/vnd.foo+xml", true},
{"application/xml; charset=utf-8", true},
{"application/vnd.foo+xml; charset=utf-8", true},
{"text/xml", true},
{"text/vnd.foo+xml", true},
{"application/foo-xml", true},
{"application/foo.xml", true},
{"application/vnd.foo-xml", true},
{"application/vnd.foo.xml", true},
{"text/foo-xml", true},
{"text/foo.xml", true},
{"text/vnd.foo-xml", true},
{"text/vnd.foo.xml", true},
} {
result := isXMLContentType(test.input)
if result != test.expect {
t.Errorf("failed on %q: want %v, got %v", test.input, test.expect, result)
}
}
}
func TestCloneURLValues(t *testing.T) {
v := url.Values{}
v.Add("foo", "bar")
v.Add("foo", "baz")
v.Add("qux", "quux")
c := cloneURLValues(v)
nilUrl := cloneURLValues(nil)
assertEqual(t, v, c)
assertNil(t, nilUrl)
}
func TestRestyErrorFuncs(t *testing.T) {
ne1 := errors.New("new error 1")
nie1 := errors.New("inner error 1")
assertNil(t, wrapErrors(nil, nil))
e := wrapErrors(ne1, nie1)
assertEqual(t, "new error 1", e.Error())
assertEqual(t, "inner error 1", errors.Unwrap(e).Error())
e = wrapErrors(ne1, nil)
assertEqual(t, "new error 1", e.Error())
e = wrapErrors(nil, nie1)
assertEqual(t, "inner error 1", e.Error())
}
func Test_createDirectory(t *testing.T) {
errMsg := "test dir error"
mkdirAll = func(path string, perm os.FileMode) error {
return errors.New(errMsg)
}
t.Cleanup(func() {
mkdirAll = os.MkdirAll
})
tempDir := filepath.Join(t.TempDir(), "test-dir")
err := createDirectory(tempDir)
assertEqual(t, errMsg, err.Error())
}
func TestUtil_readRandomUint32(t *testing.T) {
defer func() {
if r := recover(); r == nil {
// panic: resty - guid: unable to generate random object id
t.Errorf("The code did not panic")
}
}()
errMsg := "read full error"
ioReadFull = func(_ io.Reader, _ []byte) (int, error) {
return 0, errors.New(errMsg)
}
t.Cleanup(func() {
ioReadFull = io.ReadFull
})
readRandomUint32()
}
func TestUtil_readMachineID(t *testing.T) {
t.Run("hostname error", func(t *testing.T) {
errHostMsg := "hostname error"
osHostname = func() (string, error) {
return "", errors.New(errHostMsg)
}
t.Cleanup(func() {
osHostname = os.Hostname
})
readMachineID()
})
t.Run("hostname and read full error", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
// panic: resty - guid: unable to get hostname and random bytes
t.Errorf("The code did not panic")
}
}()
errHostMsg := "hostname error"
osHostname = func() (string, error) {
return "", errors.New(errHostMsg)
}
errReadMsg := "read full error"
ioReadFull = func(_ io.Reader, _ []byte) (int, error) {
return 0, errors.New(errReadMsg)
}
t.Cleanup(func() {
osHostname = os.Hostname
ioReadFull = io.ReadFull
})
readMachineID()
})
}
// This test methods exist for test coverage purpose
// to validate the getter and setter
func TestUtilMiscTestCoverage(t *testing.T) {
l := &limitReadCloser{r: strings.NewReader("hello test close for no io.Closer")}
assertNil(t, l.Close())
r := ©ReadCloser{s: strings.NewReader("hello test close for no io.Closer")}
assertNil(t, r.Close())
v := struct {
ID string `json:"id"`
Message string `json:"message"`
}{}
err := decodeJSON(bytes.NewReader([]byte(`{\" \": \"some value\"}`)), &v)
assertEqual(t, "invalid character '\\\\' looking for beginning of object key string", err.Error())
ireErr := &invalidRequestError{Err: errors.New("test coverage")}
assertEqual(t, "test coverage", ireErr.Error())
}