-
Notifications
You must be signed in to change notification settings - Fork 12
/
txtprocessor_test.go
94 lines (68 loc) · 1.95 KB
/
txtprocessor_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
package negotiator
import (
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestTXTShouldProcessAcceptHeader(t *testing.T) {
var acceptTests = []struct {
acceptheader string
expected bool
}{
{"text/plain", true},
{"text/*", true},
{"text/csv", false},
}
processor := NewTXT()
for _, tt := range acceptTests {
result := processor.CanProcess(tt.acceptheader)
assert.Equal(t, tt.expected, result, "Should process "+tt.acceptheader)
}
}
func TestTXTShouldReturnNoContentIfNil(t *testing.T) {
recorder := httptest.NewRecorder()
processor := NewTXT()
processor.Process(recorder, nil, nil)
assert.Equal(t, 204, recorder.Code)
}
func TestTXTShouldSetDefaultContentTypeHeader(t *testing.T) {
recorder := httptest.NewRecorder()
processor := NewTXT()
processor.Process(recorder, nil, "Joe Bloggs")
assert.Equal(t, "text/plain", recorder.HeaderMap.Get("Content-Type"))
}
func TestTXTShouldSetContentTypeHeader(t *testing.T) {
recorder := httptest.NewRecorder()
processor := NewTXT().(ContentTypeSettable).SetContentType("text/rtf")
processor.Process(recorder, nil, "Joe Bloggs")
assert.Equal(t, "text/rtf", recorder.HeaderMap.Get("Content-Type"))
}
func TestTXTShouldSetResponseBody(t *testing.T) {
models := []struct {
stuff interface{}
expected string
}{
{"Joe Bloggs", "Joe Bloggs\n"},
{hidden{tt(2001, 10, 31)}, "(2001-10-31)\n"},
{tm{"Joe Bloggs"}, "Joe Bloggs\n"},
}
processor := NewTXT()
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 TestTXTShouldReturnErrorOnError(t *testing.T) {
recorder := httptest.NewRecorder()
processor := NewTXT()
err := processor.Process(recorder, nil, make(chan int, 0))
assert.Error(t, err)
}
type tm struct {
s string
}
func (tm tm) MarshalText() (text []byte, err error) {
return []byte(tm.s), nil
}