@@ -12,17 +12,39 @@ import (
1212 "net/http/httptest"
1313 "testing"
1414
15- "github.com/stretchr/testify/assert"
1615 "io/ioutil"
16+
17+ "github.com/stretchr/testify/assert"
1718)
1819
19- func TestRespondJSON (t * testing.T ) {
20+ func TestRespondAcceptCSV (t * testing.T ) {
21+ reg := NewDefaultRegistry ()
22+ w := httptest .NewRecorder ()
23+ r := httptest .NewRequest ("GET" , "https://example.com/foo/bar" , nil )
24+ r .Header .Set ("Accept" , "text/csv" )
25+ data := map [string ]interface {}{"foo" : "bar" }
26+ status := http .StatusOK
27+ err := Respond (w , r , reg , data , status , "" )
28+ if ! assert .NoError (t , err ) {
29+ return
30+ }
31+ resp := w .Result ()
32+ assert .Equal (t , http .StatusOK , resp .StatusCode )
33+ body , err := ioutil .ReadAll (resp .Body )
34+ if ! assert .NoError (t , err ) {
35+ return
36+ }
37+ assert .Equal (t , "foo\n bar\n " , string (body ))
38+ }
39+
40+ func TestRespondAcceptJSON (t * testing.T ) {
41+ reg := NewDefaultRegistry ()
2042 w := httptest .NewRecorder ()
2143 r := httptest .NewRequest ("GET" , "https://example.com/foo/bar" , nil )
2244 r .Header .Set ("Accept" , "application/json" )
2345 data := map [string ]interface {}{"foo" : "bar" }
2446 status := http .StatusOK
25- err := Respond (w , r , data , status , "" )
47+ err := Respond (w , r , reg , data , status , "" )
2648 if ! assert .NoError (t , err ) {
2749 return
2850 }
@@ -35,13 +57,71 @@ func TestRespondJSON(t *testing.T) {
3557 assert .Equal (t , "{\" foo\" :\" bar\" }" , string (body ))
3658}
3759
38- func TestRespondYAML (t * testing.T ) {
60+ func TestRespondAcceptYAML (t * testing.T ) {
61+ reg := NewDefaultRegistry ()
3962 w := httptest .NewRecorder ()
4063 r := httptest .NewRequest ("GET" , "https://example.com/foo/bar" , nil )
4164 r .Header .Set ("Accept" , "application/json;q=0.8, text/yaml;q=0.9" )
4265 data := map [string ]interface {}{"foo" : "bar" }
4366 status := http .StatusOK
44- err := Respond (w , r , data , status , "" )
67+ err := Respond (w , r , reg , data , status , "" )
68+ if ! assert .NoError (t , err ) {
69+ return
70+ }
71+ resp := w .Result ()
72+ assert .Equal (t , http .StatusOK , resp .StatusCode )
73+ body , err := ioutil .ReadAll (resp .Body )
74+ if ! assert .NoError (t , err ) {
75+ return
76+ }
77+ assert .Equal (t , "foo: bar\n " , string (body ))
78+ }
79+
80+ func TestRespondExtensionCSV (t * testing.T ) {
81+ reg := NewDefaultRegistry ()
82+ w := httptest .NewRecorder ()
83+ r := httptest .NewRequest ("GET" , "https://example.com/foo/bar.csv" , nil )
84+ data := map [string ]interface {}{"foo" : "bar" }
85+ status := http .StatusOK
86+ err := Respond (w , r , reg , data , status , "" )
87+ if ! assert .NoError (t , err ) {
88+ return
89+ }
90+ resp := w .Result ()
91+ assert .Equal (t , http .StatusOK , resp .StatusCode )
92+ body , err := ioutil .ReadAll (resp .Body )
93+ if ! assert .NoError (t , err ) {
94+ return
95+ }
96+ assert .Equal (t , "foo\n bar\n " , string (body ))
97+ }
98+
99+ func TestRespondExtensionJSON (t * testing.T ) {
100+ reg := NewDefaultRegistry ()
101+ w := httptest .NewRecorder ()
102+ r := httptest .NewRequest ("GET" , "https://example.com/foo/bar.json" , nil )
103+ data := map [string ]interface {}{"foo" : "bar" }
104+ status := http .StatusOK
105+ err := Respond (w , r , reg , data , status , "" )
106+ if ! assert .NoError (t , err ) {
107+ return
108+ }
109+ resp := w .Result ()
110+ assert .Equal (t , http .StatusOK , resp .StatusCode )
111+ body , err := ioutil .ReadAll (resp .Body )
112+ if ! assert .NoError (t , err ) {
113+ return
114+ }
115+ assert .Equal (t , "{\" foo\" :\" bar\" }" , string (body ))
116+ }
117+
118+ func TestRespondExtensionYAML (t * testing.T ) {
119+ reg := NewDefaultRegistry ()
120+ w := httptest .NewRecorder ()
121+ r := httptest .NewRequest ("GET" , "https://example.com/foo/bar.yml" , nil )
122+ data := map [string ]interface {}{"foo" : "bar" }
123+ status := http .StatusOK
124+ err := Respond (w , r , reg , data , status , "" )
45125 if ! assert .NoError (t , err ) {
46126 return
47127 }
0 commit comments