forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoding_test.go
164 lines (147 loc) · 4.37 KB
/
encoding_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
package http
import (
"bytes"
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
var (
testString = "test string"
)
func TestResponseEncoder(t *testing.T) {
cases := []struct {
name string
contentType string
acceptType string
encoderType string
}{
{"no ct, no at", "", "", "*json.Encoder"},
{"no ct, at json", "", "application/json", "*json.Encoder"},
{"no ct, at xml", "", "application/xml", "*xml.Encoder"},
{"no ct, at gob", "", "application/gob", "*gob.Encoder"},
{"no ct, at html", "", "text/html", "*http.textEncoder"},
{"no ct, at plain", "", "text/plain", "*http.textEncoder"},
{"ct json", "application/json", "application/gob", "*json.Encoder"},
{"ct +json", "+json", "application/gob", "*json.Encoder"},
{"ct xml", "application/xml", "application/gob", "*xml.Encoder"},
{"ct +xml", "+xml", "application/gob", "*xml.Encoder"},
{"ct gob", "application/gob", "application/xml", "*gob.Encoder"},
{"ct +gob", "+gob", "application/xml", "*gob.Encoder"},
{"ct html", "text/html", "application/gob", "*http.textEncoder"},
{"ct +html", "+html", "application/gob", "*http.textEncoder"},
{"ct plain", "text/plain", "application/gob", "*http.textEncoder"},
{"ct +txt", "+txt", "application/gob", "*http.textEncoder"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
ctx := context.Background()
ctx = context.WithValue(ctx, AcceptTypeKey, c.acceptType)
ctx = context.WithValue(ctx, ContentTypeKey, c.contentType)
w := httptest.NewRecorder()
encoder := ResponseEncoder(ctx, w)
if c.encoderType != fmt.Sprintf("%T", encoder) {
t.Errorf("got encoder type %s, expected %s", fmt.Sprintf("%T", encoder), c.encoderType)
}
})
}
}
func TestResponseDecoder(t *testing.T) {
cases := []struct {
contentType string
decoderType string
}{
{"application/json", "*json.Decoder"},
{"+json", "*json.Decoder"},
{"application/xml", "*xml.Decoder"},
{"+xml", "*xml.Decoder"},
{"application/gob", "*gob.Decoder"},
{"+gob", "*gob.Decoder"},
{"text/html", "*http.textDecoder"},
{"+html", "*http.textDecoder"},
{"text/plain", "*http.textDecoder"},
{"+txt", "*http.textDecoder"},
}
for _, c := range cases {
t.Run(c.contentType, func(t *testing.T) {
r := &http.Response{
Header: map[string][]string{
"Content-Type": {c.contentType},
},
}
decoder := ResponseDecoder(r)
if c.decoderType != fmt.Sprintf("%T", decoder) {
t.Errorf("got decoder type %s, expected %s", fmt.Sprintf("%T", decoder), c.decoderType)
}
})
}
}
func TestTextEncoder_Encode(t *testing.T) {
cases := []struct {
name string
value interface{}
error error
}{
{"string", testString, nil},
{"*string", &testString, nil},
{"[]byte", []byte(testString), nil},
{"other", 123, fmt.Errorf("can't encode int as content/type")},
}
buffer := bytes.Buffer{}
encoder := newTextEncoder(&buffer, "content/type")
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
buffer.Reset()
err := encoder.Encode(c.value)
if c.error != nil {
if err == nil || c.error.Error() != err.Error() {
t.Errorf("got error %q, expected %q", err, c.error)
}
} else {
if err != nil {
t.Errorf("got error %q, expected <nil>", err)
}
if buffer.String() != testString {
t.Errorf("got string %s, expected %s", buffer.String(), testString)
}
}
})
}
}
func TestTextPlainDecoder_Decode_String(t *testing.T) {
decoder := makeTextDecoder()
var value string
err := decoder.Decode(&value)
if err != nil {
t.Errorf("got error %q, expected <nil>", err)
}
if testString != value {
t.Errorf("got string %s, expected %s", value, testString)
}
}
func TestTextPlainDecoder_Decode_Bytes(t *testing.T) {
decoder := makeTextDecoder()
var value []byte
err := decoder.Decode(&value)
if err != nil {
t.Errorf("got error %q, expected <nil>", err)
}
if testString != string(value) {
t.Errorf("got string %s, expected %s", value, testString)
}
}
func TestTextPlainDecoder_Decode_Other(t *testing.T) {
decoder := makeTextDecoder()
expectedErr := fmt.Errorf("can't decode content/type to *int")
var value int
err := decoder.Decode(&value)
if err == nil || err.Error() != expectedErr.Error() {
t.Errorf("got error %q, expectedErr %q", err, expectedErr)
}
}
func makeTextDecoder() Decoder {
buffer := bytes.Buffer{}
buffer.WriteString(testString)
return newTextDecoder(&buffer, "content/type")
}