-
Notifications
You must be signed in to change notification settings - Fork 1
/
serializers_test.go
167 lines (131 loc) · 5.32 KB
/
serializers_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
package shuttle
import (
"bytes"
"errors"
"testing"
)
func TestJSONDeserializer(t *testing.T) {
var value string
deserializer := newJSONDeserializer()
err := deserializer.Deserialize(&value, bytes.NewBufferString(`"hello"`))
Assert(t).That(err).IsNil()
Assert(t).That(value).Equals("hello")
}
func TestXMLDeserializer(t *testing.T) {
var value string
deserializer := newXMLDeserializer()
err := deserializer.Deserialize(&value, bytes.NewBufferString(`"<string>hello</string>"`))
Assert(t).That(err).IsNil()
Assert(t).That(value).Equals("hello")
}
func TestJSONDeserializer_ReturnError(t *testing.T) {
var value string
deserializer := newJSONDeserializer()
err := deserializer.Deserialize(&value, bytes.NewBufferString(`{`))
Assert(t).That(err).Equals(ErrDeserializationFailure)
Assert(t).That(value).Equals("")
}
func TestXMLDeserializer_ReturnError(t *testing.T) {
var value string
deserializer := newXMLDeserializer()
err := deserializer.Deserialize(&value, bytes.NewBufferString(`{`))
Assert(t).That(err).Equals(ErrDeserializationFailure)
Assert(t).That(value).Equals("")
}
func TestJSONDeserializer_CorruptedStream_IgnoreMalformedDataAfterValidJSON(t *testing.T) {
var items []int
deserializer := newJSONDeserializer()
err := deserializer.Deserialize(&items, bytes.NewBufferString(`[1,2,3]I AM MALFORMED`))
Assert(t).That(err).IsNil()
Assert(t).That(items).Equals([]int{1, 2, 3})
err = deserializer.Deserialize(&items, bytes.NewBufferString(`[1,2,3]`))
Assert(t).That(err).IsNil()
Assert(t).That(items).Equals([]int{1, 2, 3})
}
func TestJSONDeserializer_CorruptedStream_MultipleValues_UseFirst(t *testing.T) {
var items []int
deserializer := newJSONDeserializer()
err := deserializer.Deserialize(&items, bytes.NewBufferString(`[1,2,3] [4,5,6]`))
Assert(t).That(err).IsNil()
Assert(t).That(items).Equals([]int{1, 2, 3})
}
func TestJSONDeserializer_Multiple_InvocationsWithEmptyBuffer(t *testing.T) {
var items []int
deserializer := newJSONDeserializer()
err := deserializer.Deserialize(&items, bytes.NewBufferString(``))
Assert(t).That(err).IsNil()
Assert(t).That(items).IsNil()
err = deserializer.Deserialize(&items, bytes.NewBufferString(`[1,2,3]`))
Assert(t).That(err).IsNil()
Assert(t).That(items).Equals([]int{1, 2, 3})
}
func TestJSONDeserializer_SuccessAfterFailure(t *testing.T) {
var value1, value2 string
deserializer := newJSONDeserializer()
err1 := deserializer.Deserialize(&value1, &FakeFailingStream{})
err2 := deserializer.Deserialize(&value2, bytes.NewBufferString(`"hello"`))
Assert(t).That(err1).Equals(ErrDeserializationFailure)
Assert(t).That(err2).IsNil()
Assert(t).That(value2).Equals("hello")
}
func TestXMLDeserializer_SuccessAfterFailure(t *testing.T) {
var value1, value2 string
deserializer := newXMLDeserializer()
err1 := deserializer.Deserialize(&value1, &FakeFailingStream{})
err2 := deserializer.Deserialize(&value2, bytes.NewBufferString(`"<string>hello</string>"`))
Assert(t).That(err1).Equals(ErrDeserializationFailure)
Assert(t).That(err2).IsNil()
Assert(t).That(value2).Equals("hello")
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func TestJSONSerializer(t *testing.T) {
serializer := newJSONSerializer()
buffer := bytes.NewBufferString("")
err := serializer.Serialize(buffer, "hello")
Assert(t).That(err).IsNil()
Assert(t).That(buffer.String()).Equals(`"hello"` + "\n")
Assert(t).That(serializer.ContentType()).Equals("application/json; charset=utf-8")
}
func TestXMLSerializer(t *testing.T) {
serializer := newXMLSerializer()
buffer := bytes.NewBufferString("")
err := serializer.Serialize(buffer, "hello")
Assert(t).That(err).IsNil()
Assert(t).That(buffer.String()).Equals("<string>hello</string>")
Assert(t).That(serializer.ContentType()).Equals("application/xml; charset=utf-8")
}
func TestJSONSerializer_Failure(t *testing.T) {
serializer := newJSONSerializer()
buffer := bytes.NewBufferString("")
err := serializer.Serialize(buffer, make(chan string))
Assert(t).That(err).Equals(ErrSerializationFailure)
Assert(t).That(buffer.Len()).Equals(0)
}
func TestXMLSerializer_Failure(t *testing.T) {
serializer := newXMLSerializer()
buffer := bytes.NewBufferString("")
err := serializer.Serialize(buffer, make(chan string))
Assert(t).That(err).Equals(ErrSerializationFailure)
Assert(t).That(buffer.Len()).Equals(0)
}
func TestJSONSerializer_SuccessAfterFailure(t *testing.T) {
serializer := newJSONSerializer()
buffer := bytes.NewBufferString("")
err1 := serializer.Serialize(FakeFailingStream{}, "hello")
err2 := serializer.Serialize(buffer, "hello")
Assert(t).That(err1).Equals(ErrSerializationFailure)
Assert(t).That(err2).IsNil()
Assert(t).That(buffer.String()).Equals(`"hello"` + "\n")
}
func TestXMLSerializer_SuccessAfterFailure(t *testing.T) {
serializer := newXMLSerializer()
buffer := bytes.NewBufferString("")
err1 := serializer.Serialize(FakeFailingStream{}, "hello")
err2 := serializer.Serialize(buffer, "hello")
Assert(t).That(err1).Equals(ErrSerializationFailure)
Assert(t).That(err2).IsNil()
Assert(t).That(buffer.String()).Equals("<string>hello</string>")
}
type FakeFailingStream struct{}
func (this FakeFailingStream) Write([]byte) (int, error) { return 0, errors.New("write failure!") }
func (this FakeFailingStream) Read([]byte) (int, error) { return 0, errors.New("read failure!") }