-
Notifications
You must be signed in to change notification settings - Fork 0
/
negotiate_test.go
156 lines (144 loc) · 3.43 KB
/
negotiate_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
package ep
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http/httptest"
"reflect"
"strconv"
"strings"
"testing"
"github.com/advanderveer/ep/epcoding"
)
func TestNegotiateResponseEncoder(t *testing.T) {
for i, c := range []struct {
accept string
encs []epcoding.Encoding
expEnc epcoding.Encoder
expErr error
expCT string
}{
{
expErr: Err(Op("negotiateEncoder"), ServerError),
},
{
encs: []epcoding.Encoding{epcoding.JSON{}},
expEnc: new(json.Encoder),
expCT: "application/json",
},
{
accept: "foo/bar",
encs: []epcoding.Encoding{epcoding.JSON{}},
expErr: Err(Op("negotiateEncoder"), UnacceptableError),
},
{
accept: "application/json",
encs: []epcoding.Encoding{epcoding.JSON{}},
expEnc: new(json.Encoder),
expCT: "application/json",
},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
r := httptest.NewRequest("GET", "/", nil)
r.Header.Set("Accept", c.accept)
w := httptest.NewRecorder()
enc, ct, err := negotiateEncoder(r, w, c.encs)
if !errors.Is(err, c.expErr) {
t.Fatalf("expected error %#v, got: %#v", c.expErr, err)
}
if ct != c.expCT {
t.Fatalf("expected encoding ct to be %s, got: %s", c.expCT, ct)
}
if c.expEnc == nil && enc != c.expEnc {
t.Fatalf("expected nil decoder, got: %#v", enc)
} else if reflect.TypeOf(enc) != reflect.TypeOf(c.expEnc) {
t.Fatalf("expected decoder type %T, got: %T", c.expEnc, enc)
}
})
}
}
func TestNegotiateRequestDecoder(t *testing.T) {
for i, c := range []struct {
body string
ct string
decs []epcoding.Decoding
expDec epcoding.Decoder
expErr error
}{
{
"", "application/json", nil, nil,
Err(Op("negotiateDecoder"), UnsupportedError),
},
{
"{}", "application/json ; charset=UTF-8", nil, nil,
Err(Op("negotiateDecoder"), UnsupportedError),
},
{
"{}", "foo/bar ; charset=UTF-8",
[]epcoding.Decoding{epcoding.JSON{}}, nil,
Err(Op("negotiateDecoder"), UnsupportedError),
},
{
"{}", "application/json ; charset=UTF-8",
[]epcoding.Decoding{epcoding.JSON{}}, &json.Decoder{}, nil,
},
{
" {", "",
[]epcoding.Decoding{epcoding.JSON{}}, &json.Decoder{}, nil,
},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
r := httptest.NewRequest("POST", "/", strings.NewReader(c.body))
if c.ct != "" {
r.Header.Set("Content-Type", c.ct)
}
bdec, err := negotiateDecoder(r, c.decs)
if !errors.Is(err, c.expErr) {
t.Fatalf("expected error %#v, got: %#v", c.expErr, err)
}
if c.expDec == nil && bdec != c.expDec {
t.Fatalf("expected nil decoder, got: %#v", bdec)
} else if reflect.TypeOf(bdec) != reflect.TypeOf(c.expDec) {
t.Fatalf("expected decoder type %T, got: %T", c.expDec, bdec)
}
all, _ := ioutil.ReadAll(r.Body)
if string(all) != c.body {
t.Fatalf("body not intact after negotiate, got: %v", string(all))
}
})
}
}
func TestDetectContentType(t *testing.T) {
for i, c := range []struct {
content string
expCT string
}{
{
" ",
"text/plain; charset=utf-8",
},
{
" {",
"application/json; charset=utf-8",
},
{
"[",
"application/json; charset=utf-8",
},
{
`"`,
"application/json; charset=utf-8",
},
{
`<?xml version="1.0" ?>`,
"text/xml; charset=utf-8",
},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
ct := detectContentType([]byte(c.content))
if ct != c.expCT {
t.Fatalf("expected ct to be: %v, got: %v", c.expCT, ct)
}
})
}
}