-
Notifications
You must be signed in to change notification settings - Fork 7
/
codec_test.go
170 lines (145 loc) · 3.7 KB
/
codec_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
package hasaki
import (
"bytes"
"encoding/xml"
"github.com/pkg/errors"
"io"
"net"
"net/http"
"net/url"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestForm_encoder_Encode(t *testing.T) {
_, err1 := FormCodec.Encode(struct {
Name string
}{Name: "caster"})
assert.True(t, errors.Is(err1, errUnsupportedData))
_, err2 := FormCodec.Encode(url.Values{
"name": []string{"caster"},
})
assert.NoError(t, err2)
_, err3 := FormCodec.Encode(nil)
assert.NoError(t, err3)
var netConn *net.TCPConn
_, err4 := FormCodec.Encode(net.Conn(netConn))
assert.Error(t, err4)
_, err5 := FormCodec.Encode("a=xxx")
assert.NoError(t, err5)
}
func TestJson_encoder_Encode(t *testing.T) {
_, err1 := JsonCodec.Encode(struct {
Name string
}{Name: "caster"})
assert.NoError(t, err1)
_, err2 := JsonCodec.Encode(map[string]interface{}{
"name": "caster",
})
assert.NoError(t, err2)
_, err3 := JsonCodec.Encode(nil)
assert.NoError(t, err3)
}
func TestStreamEncoder(t *testing.T) {
encoder := NewStreamEncoder("text/plain")
assert.Equal(t, encoder.ContentType(), "text/plain")
_, err1 := encoder.Encode("aha")
assert.NoError(t, err1)
_, err2 := encoder.Encode([]byte("aha"))
assert.NoError(t, err2)
_, err3 := encoder.Encode(bytes.NewBufferString("oh"))
assert.NoError(t, err3)
_, err4 := encoder.Encode(123)
assert.Error(t, err4)
}
func TestFormDecode(t *testing.T) {
t.Run("ok", func(t *testing.T) {
var params = url.Values{}
var text = "a=xxx&b=1"
var err = FormCodec.Decode(strings.NewReader(text), ¶ms)
assert.NoError(t, err)
assert.Equal(t, params["a"][0], "xxx")
})
t.Run("bind", func(t *testing.T) {
var text = "a=xxx&b=1"
var resp = &Response{
Response: &http.Response{
Body: io.NopCloser(strings.NewReader(text)),
},
}
var params = url.Values{}
var err = resp.BindForm(¶ms)
assert.NoError(t, err)
assert.Equal(t, params["a"][0], "xxx")
})
t.Run("unsupported type", func(t *testing.T) {
var params = url.Values{}
var err = FormCodec.Decode(strings.NewReader(""), params)
assert.True(t, errors.Is(err, errUnsupportedData))
})
t.Run("error", func(t *testing.T) {
var params = url.Values{}
var text = "a;b;c"
var err = FormCodec.Decode(strings.NewReader(text), ¶ms)
assert.Error(t, err)
})
}
func TestXmlEncoder(t *testing.T) {
t.Run("type", func(t *testing.T) {
assert.Equal(t, XmlCodec.ContentType(), MimeXml)
})
t.Run("", func(t *testing.T) {
var params = struct {
XMLName xml.Name `xml:"xml"`
Name string `xml:"name"`
Age int `xml:"age"`
}{
Name: "cas",
}
_, err := XmlCodec.Encode(params)
assert.NoError(t, err)
})
t.Run("nil", func(t *testing.T) {
_, err := XmlCodec.Encode(nil)
assert.NoError(t, err)
})
}
func TestXmlDecode(t *testing.T) {
var text = `
<?xml version="1.0" encoding="UTF-8" ?>
<peoples version="0.9">
<people id="888">
<name>msr</name>
<address>中国上海</address>
</people>
<people id="998">
<name>maishuren</name>
<address>中国上海</address>
</people>
</peoples>
`
type Peoples struct {
XMLName xml.Name `xml:"peoples"`
Version string `xml:"version,attr"`
Peos []struct {
XMLName xml.Name `xml:"people"`
Id int `xml:"id,attr"`
Name string `xml:"name"`
Address string `xml:"address"`
} `xml:"people"`
}
var p = &Peoples{}
var err = XmlCodec.Decode(strings.NewReader(text), p)
assert.NoError(t, err)
t.Run("bind", func(t *testing.T) {
var resp = &Response{
Response: &http.Response{
Body: io.NopCloser(strings.NewReader(text)),
},
}
var params = Peoples{}
var err = resp.BindXML(¶ms)
assert.NoError(t, err)
assert.Equal(t, params.Peos[0].Id, 888)
})
}