-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcommon_test.go
More file actions
174 lines (136 loc) · 3.06 KB
/
common_test.go
File metadata and controls
174 lines (136 loc) · 3.06 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
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
package gosnowth
import (
"bytes"
"encoding/xml"
"io"
"net/http"
"net/url"
"strings"
"testing"
"time"
)
func float64Ptr(f float64) *float64 {
return &f
}
func stringPtr(s string) *string {
return &s
}
type noOpReadCloser struct {
*bytes.Buffer
WasClosed bool
}
func (n *noOpReadCloser) Close() error {
n.WasClosed = true
return nil
}
func TestResolveURL(t *testing.T) {
t.Parallel()
base, _ := url.Parse("http://localhost:1234")
result := resolveURL(base, "/a/resource/path")
exp := "http://localhost:1234/a/resource/path"
if result != exp {
t.Errorf("Expected result: %v, got: %v", exp, result)
}
}
func TestDecodeJSON(t *testing.T) {
t.Parallel()
resp := &http.Response{
Body: &noOpReadCloser{
bytes.NewBufferString(`{
"something": 1,
"something_else": 2
}`),
false,
},
}
v := make(map[string]int)
if err := decodeJSON(resp.Body, &v); err != nil {
t.Error("error encountered from decode function: ", err)
}
if v["something"] != 1 {
t.Error("something should be 1")
}
if v["something_else"] != 2 {
t.Error("something_else should be 2")
}
}
func TestDecodeXML(t *testing.T) {
t.Parallel()
resp := &http.Response{
Body: &noOpReadCloser{
bytes.NewBufferString(`<data><something>1</something><somethingelse>2</somethingelse></data>`),
false,
},
}
type data struct {
XMLName xml.Name `xml:"data"`
Something int `xml:"something"`
SomethingElse int `xml:"somethingelse"`
}
decoded := &data{}
if err := decodeXML(resp.Body, decoded); err != nil {
t.Error("error encountered from decode function: ", err)
}
if decoded.Something != 1 {
t.Error("something should be 1")
}
if decoded.SomethingElse != 2 {
t.Error("something else should be 2")
}
}
func TestEncodeXML(t *testing.T) {
t.Parallel()
type data struct {
XMLName xml.Name `xml:"data"`
Something int `xml:"something"`
SomethingElse int `xml:"somethingelse"`
}
d := &data{
Something: 1,
SomethingElse: 2,
}
reader, err := encodeXML(d)
if err != nil {
t.Error("error encountered encoding: ", err)
}
b, _ := io.ReadAll(reader)
if !strings.Contains(string(b), "somethingelse") {
t.Error("Should contain somethingelse")
}
}
func TestFormatTimestamp(t *testing.T) {
t.Parallel()
tm := time.Unix(123456789, int64(time.Millisecond))
exp := "123456789.001"
res := formatTimestamp(tm)
if res != exp {
t.Errorf("Expected string: %v, got: %v", exp, res)
}
tm = time.Unix(123456789, 0)
exp = "123456789"
res = formatTimestamp(tm)
if res != exp {
t.Errorf("Expected string: %v, got: %v", exp, res)
}
}
func TestParseTimestamp(t *testing.T) {
t.Parallel()
res, err := parseTimestamp("123456789.001")
if err != nil {
t.Fatal(err)
}
exp := time.Unix(123456789, int64(time.Millisecond))
if !res.Equal(exp) {
t.Errorf("Expected time: %v, got: %v", exp, res)
}
}
func TestParseDuration(t *testing.T) {
t.Parallel()
res, err := parseDuration("1")
if err != nil {
t.Fatal(err)
}
if exp := time.Second; res != exp {
t.Errorf("Expected duration: %v, got: %v", exp, res)
}
}