forked from jchannon/negotiator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvprocessor_test.go
More file actions
156 lines (124 loc) · 4.25 KB
/
Copy pathcsvprocessor_test.go
File metadata and controls
156 lines (124 loc) · 4.25 KB
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 negotiator
import (
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"time"
)
func TestCSVShouldProcessAcceptHeader(t *testing.T) {
var acceptTests = []struct {
acceptheader string
expected bool
}{
{"text/csv", true},
{"text/*", true},
{"text/plain", false},
}
processor := NewCSV()
for _, tt := range acceptTests {
result := processor.CanProcess(tt.acceptheader)
assert.Equal(t, tt.expected, result, "Should process "+tt.acceptheader)
}
}
func TestCSVShouldReturnNoContentIfNil(t *testing.T) {
recorder := httptest.NewRecorder()
processor := NewCSV()
processor.Process(recorder, nil, nil)
assert.Equal(t, 204, recorder.Code)
}
func TestCSVShouldSetDefaultContentTypeHeader(t *testing.T) {
recorder := httptest.NewRecorder()
processor := NewCSV()
processor.Process(recorder, nil, "Joe Bloggs")
assert.Equal(t, "text/csv", recorder.HeaderMap.Get("Content-Type"))
}
func TestCSVShouldSetContentTypeHeader(t *testing.T) {
recorder := httptest.NewRecorder()
processor := NewCSV().(ContentTypeSettable).SetContentType("text/csv-schema")
processor.Process(recorder, nil, "Joe Bloggs")
assert.Equal(t, "text/csv-schema", recorder.HeaderMap.Get("Content-Type"))
}
func tt(y, m, d int) time.Time {
return time.Date(y, time.Month(m), d, 0, 0, 0, 0, time.UTC)
}
func TestCSVShouldSetResponseBody(t *testing.T) {
models := []struct {
stuff interface{}
expected string
}{
{"Joe Bloggs", "Joe Bloggs\n"},
{[]string{"Red", "Green", "Blue"}, "Red,Green,Blue\n"},
{[][]string{[]string{"Red", "Green", "Blue"}, []string{"Cyan", "Magenta", "Yellow"}}, "Red,Green,Blue\nCyan,Magenta,Yellow\n"},
{[]int{101, -5, 42}, "101,-5,42\n"},
{[]int8{101, -5, 42}, "101,-5,42\n"},
{[]uint{101, 42}, "101,42\n"},
{[]uint8{101, 42}, "101,42\n"},
{[][]int{[]int{101, 42}, []int{39, 7}}, "101,42\n39,7\n"},
{[][]uint{[]uint{101, 42}, []uint{39, 7}}, "101,42\n39,7\n"},
{Data{"x,y", 9, 4, true}, "\"x,y\",9,4,true\n"},
{[]Data{Data{"x", 9, 4, true}, Data{"y", 7, 1, false}}, "x,9,4,true\ny,7,1,false\n"},
{[]hidden{hidden{tt(2001, 11, 29)}, hidden{tt(2001, 11, 30)}}, "(2001-11-29),(2001-11-30)\n"},
{[][]hidden{[]hidden{hidden{tt(2001, 12, 30)}, hidden{tt(2001, 12, 31)}}}, "(2001-12-30),(2001-12-31)\n"},
{[]*hidden{&hidden{tt(2001, 11, 29)}, &hidden{tt(2001, 11, 30)}}, "(2001-11-29),(2001-11-30)\n"},
{[][]*hidden{[]*hidden{&hidden{tt(2001, 12, 30)}, &hidden{tt(2001, 12, 31)}}}, "(2001-12-30),(2001-12-31)\n"},
}
processor := NewCSV()
for _, m := range models {
recorder := httptest.NewRecorder()
err := processor.Process(recorder, nil, m.stuff)
assert.NoError(t, err)
assert.Equal(t, m.expected, recorder.Body.String())
}
}
func TestCSVShouldSetResponseBodyWithTabs(t *testing.T) {
models := []struct {
stuff interface{}
expected string
}{
{"Joe Bloggs", "Joe Bloggs\n"},
{[]string{"Red", "Green", "Blue"}, "Red\tGreen\tBlue\n"},
{[][]string{[]string{"Red", "Green", "Blue"}, []string{"Cyan", "Magenta", "Yellow"}}, "Red\tGreen\tBlue\nCyan\tMagenta\tYellow\n"},
{[]int{101, -5, 42}, "101\t-5\t42\n"},
{[]int8{101, -5, 42}, "101\t-5\t42\n"},
{[]uint{101, 42}, "101\t42\n"},
{[]uint8{101, 42}, "101\t42\n"},
{[][]int{[]int{101, 42}, []int{39, 7}}, "101\t42\n39\t7\n"},
{[][]uint{[]uint{101, 42}, []uint{39, 7}}, "101\t42\n39\t7\n"},
{Data{"x", 9, 4, true}, "x\t9\t4\ttrue\n"},
{[]Data{Data{"x", 9, 4, true}, Data{"y", 7, 1, false}}, "x\t9\t4\ttrue\ny\t7\t1\tfalse\n"},
}
processor := NewCSV('\t')
for _, m := range models {
recorder := httptest.NewRecorder()
err := processor.Process(recorder, nil, m.stuff)
assert.NoError(t, err)
assert.Equal(t, m.expected, recorder.Body.String())
}
}
func TestCSVShouldReturnErrorOnError(t *testing.T) {
recorder := httptest.NewRecorder()
processor := NewCSV()
err := processor.Process(recorder, nil, make(chan int, 0))
assert.Error(t, err)
}
type Data struct {
F1 string
F2 int
F3 uint
F4 bool
}
// has hidden fields
type hidden struct {
d time.Time
}
func (h hidden) String() string {
return "(" + h.d.Format("2006-01-02") + ")"
}
//func (u *User) MarshalCSV() ([]byte, error) {
// return nil, errors.New("oops")
//}
//
//func jsontestErrorHandler(w http.ResponseWriter, err error) {
// w.WriteHeader(500)
// w.Write([]byte(err.Error()))
//}