forked from jchannon/negotiator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnegotiate_test.go
124 lines (86 loc) · 3.29 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
package negotiator
import (
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestShouldAddCustomResponseProcessors(t *testing.T) {
var fakeResponseProcessor = &fakeProcessor{}
negotiator := NewWithJSONAndXML(fakeResponseProcessor)
assert.Equal(t, 3, len(negotiator.processors))
}
func TestShouldAddCustomResponseProcessors2(t *testing.T) {
var fakeResponseProcessor = &fakeProcessor{}
negotiator := New().Add(NewJSON(), NewXML()).Add(fakeResponseProcessor)
assert.Equal(t, 3, len(negotiator.processors))
}
func TestShouldAddCustomResponseProcessorsToBeginning(t *testing.T) {
var fakeResponseProcessor = &fakeProcessor{}
negotiator := NewWithJSONAndXML(fakeResponseProcessor)
firstProcessor := negotiator.processors[0]
processorName := reflect.TypeOf(firstProcessor).String()
assert.Equal(t, "*negotiator.fakeProcessor", processorName)
}
func TestShouldReturnDefaultProcessorIfNoAcceptHeader(t *testing.T) {
var fakeResponseProcessor = &fakeProcessor{}
negotiator := New(fakeResponseProcessor)
req, _ := http.NewRequest("GET", "/", nil)
recorder := httptest.NewRecorder()
negotiator.Negotiate(recorder, req, "foo")
assert.Equal(t, "boo ya!", recorder.Body.String())
}
func TestShouldGiveJSONResponseForAjaxRequests(t *testing.T) {
negotiator := NewWithJSONAndXML()
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add(xRequestedWith, xmlHttpRequest)
recorder := httptest.NewRecorder()
model := &ValidXMLUser{
"Joe Bloggs",
}
negotiator.Negotiate(recorder, req, model)
assert.Equal(t, "{\"Name\":\"Joe Bloggs\"}\n", recorder.Body.String())
}
func TestShouldReturn406IfNoMatchingAcceptHeader(t *testing.T) {
var fakeResponseProcessor = &fakeProcessor{}
negotiator := New(fakeResponseProcessor)
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add("Accept", "image/png")
recorder := httptest.NewRecorder()
negotiator.Negotiate(recorder, req, "foo")
assert.Equal(t, http.StatusNotAcceptable, recorder.Code)
}
func TestShouldReturn406IfNoAcceptHeaderWithoutCustomResponseProcessor(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
recorder := httptest.NewRecorder()
Negotiate(recorder, req, "foo")
assert.Equal(t, http.StatusOK, recorder.Code)
}
func TestShouldNegotiateAndWriteToResponseBody(t *testing.T) {
var fakeResponseProcessor = &fakeProcessor{}
negotiator := New(fakeResponseProcessor)
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add("Accept", "application/negotiatortesting")
recorder := httptest.NewRecorder()
negotiator.Negotiate(recorder, req, "foo")
assert.Equal(t, "boo ya!", recorder.Body.String())
}
func TestShouldNegotiateADefaultProcessor(t *testing.T) {
var fakeResponseProcessor = &fakeProcessor{}
negotiator := New(fakeResponseProcessor)
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add("Accept", "*/*")
recorder := httptest.NewRecorder()
negotiator.Negotiate(recorder, req, "foo")
assert.Equal(t, "boo ya!", recorder.Body.String())
}
type fakeProcessor struct{}
func (*fakeProcessor) CanProcess(mediaRange string) bool {
return strings.EqualFold(mediaRange, "application/negotiatortesting")
}
func (*fakeProcessor) Process(w http.ResponseWriter, req *http.Request, model interface{}, context ...interface{}) error {
w.Write([]byte("boo ya!"))
return nil
}