-
Notifications
You must be signed in to change notification settings - Fork 3
/
decoder_test.go
121 lines (91 loc) · 3.66 KB
/
decoder_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
package sugar
import (
"errors"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"os"
"testing"
)
func TestDecoderChain_Next_Returns_Error_If_No_Decoders(t *testing.T) {
c := &DecoderChain{}
assert.Equal(t, DecoderNotFound, c.Next())
}
func TestJsonDecoder_Decode_Returns_Error_If_Fail_To_Read_Body(t *testing.T) {
context := &ResponseContext{Response: &http.Response{Header: http.Header{ContentType: []string{ContentTypeJson}}, Body: readErrorBody{}}}
decoder := &JsonDecoder{}
assert.NotNil(t, decoder.Decode(context, nil))
}
func TestJsonDecoder_Decode_Returns_Error_If_Fail_To_Unmarshal(t *testing.T) {
context := &ResponseContext{Response: &http.Response{Header: http.Header{ContentType: []string{ContentTypeJson}}, Body: unmarshalErrorBody{}}}
decoder := &JsonDecoder{}
assert.NotNil(t, decoder.Decode(context, nil))
}
func TestXmlDecoder_Decode_Returns_Error_If_Fail_To_Read_Body(t *testing.T) {
context := &ResponseContext{Response: &http.Response{Header: http.Header{ContentType: []string{ContentTypeXml}}, Body: readErrorBody{}}}
decoder := &XmlDecoder{}
assert.NotNil(t, decoder.Decode(context, nil))
}
func TestXmlDecoder_Decode_Returns_Error_If_Fail_To_Unmarshal(t *testing.T) {
context := &ResponseContext{Response: &http.Response{Header: http.Header{ContentType: []string{ContentTypeXml}}, Body: unmarshalErrorBody{}}}
decoder := &XmlDecoder{}
assert.NotNil(t, decoder.Decode(context, nil))
}
func TestPlainTextDecoder_Decode_Returns_Error_If_Fail_To_Read_Body(t *testing.T) {
var out string
context := &ResponseContext{Response: &http.Response{Header: http.Header{ContentType: []string{ContentTypePlainText}}, Body: readErrorBody{}}, Out: &out}
decoder := &PlainTextDecoder{}
assert.NotNil(t, decoder.Decode(context, nil))
}
type mockDecoder struct {
Called bool
}
func (d *mockDecoder) Decode(context *ResponseContext, chain *DecoderChain) error {
d.Called = true
return chain.Next()
}
func TestPlainTextDecoder_Will_Propagate_If_Out_Is_Not_Type_Of_String_Pointer(t *testing.T) {
var out int
context := &ResponseContext{Response: &http.Response{Header: http.Header{ContentType: []string{ContentTypePlainText}}, Body: readErrorBody{}}, Out: &out}
decoder := &PlainTextDecoder{}
nextDecoder := &mockDecoder{Called: false}
decoder.Decode(context, NewDecoderChain(&ResponseContext{Response: nil, Out: nil}, nextDecoder))
assert.True(t, nextDecoder.Called)
}
func TestFileDecoder_Decode_Returns_Error_If_Header_Is_Wrong(t *testing.T) {
f, _ := os.Create("tmp.pdf")
defer func() { os.Remove("tmp.pdf") }()
context := &ResponseContext{Response: &http.Response{Header: http.Header{}}, Out: f}
chain := &DecoderChain{}
decoder := &FileDecoder{}
assert.Equal(t, DecoderNotFound, decoder.Decode(context, chain))
}
func TestFileDecoder_Decode_Returns_Error_If_Out_Is_Not_Ptr_Of_OsFile(t *testing.T) {
context := &ResponseContext{Out: "string"}
chain := &DecoderChain{}
decoder := &FileDecoder{}
assert.Equal(t, DecoderNotFound, decoder.Decode(context, chain))
}
type readErrorBody struct {
}
func (b readErrorBody) Read(p []byte) (n int, err error) {
return 0, errors.New("error")
}
func (b readErrorBody) Close() error {
return nil
}
type unmarshalErrorBody struct {
}
func (b unmarshalErrorBody) Read(p []byte) (n int, err error) {
return 0, io.EOF
}
func (b unmarshalErrorBody) Close() error {
return nil
}
func TestPlainTextDecoder_Decode_Returns_Error_If_Content_Type_Is_Wrong(t *testing.T) {
s := new(string)
context := &ResponseContext{Out: s, Response: &http.Response{Header: http.Header{ContentType: []string{ContentTypeForm}}}}
chain := &DecoderChain{}
decoder := &PlainTextDecoder{}
assert.Equal(t, DecoderNotFound, decoder.Decode(context, chain))
}