return_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package tango
  2. import (
  3. "bytes"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. "errors"
  8. "strings"
  9. "encoding/xml"
  10. )
  11. type MyReturn struct {
  12. }
  13. func (m MyReturn) Get() string {
  14. return "string return"
  15. }
  16. func (m MyReturn) Post() []byte {
  17. return []byte("bytes return")
  18. }
  19. func (m MyReturn) Put() error {
  20. return errors.New("error return")
  21. }
  22. func TestReturn(t *testing.T) {
  23. buff := bytes.NewBufferString("")
  24. recorder := httptest.NewRecorder()
  25. recorder.Body = buff
  26. o := Classic()
  27. o.Any("/", new(MyReturn))
  28. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  29. if err != nil {
  30. t.Error(err)
  31. }
  32. o.ServeHTTP(recorder, req)
  33. expect(t, recorder.Code, http.StatusOK)
  34. refute(t, len(buff.String()), 0)
  35. expect(t, buff.String(), "string return")
  36. buff.Reset()
  37. req, err = http.NewRequest("POST", "http://localhost:8000/", nil)
  38. if err != nil {
  39. t.Error(err)
  40. }
  41. o.ServeHTTP(recorder, req)
  42. expect(t, recorder.Code, http.StatusOK)
  43. refute(t, len(buff.String()), 0)
  44. expect(t, buff.String(), "bytes return")
  45. }
  46. func TestReturnPut(t *testing.T) {
  47. buff := bytes.NewBufferString("")
  48. recorder := httptest.NewRecorder()
  49. recorder.Body = buff
  50. o := Classic()
  51. o.Any("/", new(MyReturn))
  52. req, err := http.NewRequest("PUT", "http://localhost:8000/", nil)
  53. if err != nil {
  54. t.Error(err)
  55. }
  56. o.ServeHTTP(recorder, req)
  57. expect(t, recorder.Code, http.StatusInternalServerError)
  58. refute(t, len(buff.String()), 0)
  59. expect(t, buff.String(), "error return")
  60. }
  61. type JsonReturn struct {
  62. Json
  63. }
  64. func (JsonReturn) Get() interface{} {
  65. return map[string]interface{}{
  66. "test1": 1,
  67. "test2": "2",
  68. "test3": true,
  69. }
  70. }
  71. func TestReturnJson1(t *testing.T) {
  72. buff := bytes.NewBufferString("")
  73. recorder := httptest.NewRecorder()
  74. recorder.Body = buff
  75. o := Classic()
  76. o.Get("/", new(JsonReturn))
  77. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  78. if err != nil {
  79. t.Error(err)
  80. }
  81. o.ServeHTTP(recorder, req)
  82. expect(t, recorder.Code, http.StatusOK)
  83. refute(t, len(buff.String()), 0)
  84. expect(t, strings.TrimSpace(buff.String()), `{"test1":1,"test2":"2","test3":true}`)
  85. }
  86. type JsonErrReturn struct {
  87. Json
  88. }
  89. func (JsonErrReturn) Get() error {
  90. return errors.New("error")
  91. }
  92. func TestReturnJsonError(t *testing.T) {
  93. buff := bytes.NewBufferString("")
  94. recorder := httptest.NewRecorder()
  95. recorder.Body = buff
  96. o := Classic()
  97. o.Get("/", new(JsonErrReturn))
  98. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  99. if err != nil {
  100. t.Error(err)
  101. }
  102. o.ServeHTTP(recorder, req)
  103. expect(t, recorder.Code, http.StatusOK)
  104. refute(t, len(buff.String()), 0)
  105. expect(t, strings.TrimSpace(buff.String()), `{"err":"error"}`)
  106. }
  107. type JsonErrReturn2 struct {
  108. Json
  109. }
  110. func (JsonErrReturn2) Get() error {
  111. return Abort(http.StatusInternalServerError, "error")
  112. }
  113. func TestReturnJsonError2(t *testing.T) {
  114. buff := bytes.NewBufferString("")
  115. recorder := httptest.NewRecorder()
  116. recorder.Body = buff
  117. o := Classic()
  118. o.Get("/", new(JsonErrReturn2))
  119. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  120. if err != nil {
  121. t.Error(err)
  122. }
  123. o.ServeHTTP(recorder, req)
  124. expect(t, recorder.Code, http.StatusInternalServerError)
  125. refute(t, len(buff.String()), 0)
  126. expect(t, strings.TrimSpace(buff.String()), `{"err":"error"}`)
  127. }
  128. type JsonReturn1 struct {
  129. Json
  130. }
  131. func (JsonReturn1) Get() string {
  132. return "return"
  133. }
  134. func TestReturnJson2(t *testing.T) {
  135. buff := bytes.NewBufferString("")
  136. recorder := httptest.NewRecorder()
  137. recorder.Body = buff
  138. o := Classic()
  139. o.Get("/", new(JsonReturn1))
  140. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  141. if err != nil {
  142. t.Error(err)
  143. }
  144. o.ServeHTTP(recorder, req)
  145. expect(t, recorder.Code, http.StatusOK)
  146. refute(t, len(buff.String()), 0)
  147. expect(t, strings.TrimSpace(buff.String()), `{"content":"return"}`)
  148. }
  149. type JsonReturn2 struct {
  150. Json
  151. }
  152. func (JsonReturn2) Get() []byte {
  153. return []byte("return")
  154. }
  155. func TestReturnJson3(t *testing.T) {
  156. buff := bytes.NewBufferString("")
  157. recorder := httptest.NewRecorder()
  158. recorder.Body = buff
  159. o := Classic()
  160. o.Get("/", new(JsonReturn2))
  161. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  162. if err != nil {
  163. t.Error(err)
  164. }
  165. o.ServeHTTP(recorder, req)
  166. expect(t, recorder.Code, http.StatusOK)
  167. refute(t, len(buff.String()), 0)
  168. expect(t, strings.TrimSpace(buff.String()), `{"content":"return"}`)
  169. }
  170. type XmlReturn struct {
  171. Xml
  172. }
  173. type Address struct {
  174. City, State string
  175. }
  176. type Person struct {
  177. XMLName xml.Name `xml:"person"`
  178. Id int `xml:"id,attr"`
  179. FirstName string `xml:"name>first"`
  180. LastName string `xml:"name>last"`
  181. Age int `xml:"age"`
  182. Height float32 `xml:"height,omitempty"`
  183. Married bool
  184. Address
  185. Comment string `xml:",comment"`
  186. }
  187. func (XmlReturn) Get() interface{} {
  188. v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
  189. v.Comment = " Need more details. "
  190. v.Address = Address{"Hanga Roa", "Easter Island"}
  191. return v
  192. }
  193. func TestReturnXml(t *testing.T) {
  194. buff := bytes.NewBufferString("")
  195. recorder := httptest.NewRecorder()
  196. recorder.Body = buff
  197. o := Classic()
  198. o.Get("/", new(XmlReturn))
  199. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  200. if err != nil {
  201. t.Error(err)
  202. }
  203. o.ServeHTTP(recorder, req)
  204. expect(t, recorder.Code, http.StatusOK)
  205. refute(t, len(buff.String()), 0)
  206. expect(t, buff.String(), `<person id="13"><name><first>John</first><last>Doe</last></name><age>42</age><Married>false</Married><City>Hanga Roa</City><State>Easter Island</State><!-- Need more details. --></person>`)
  207. }
  208. type XmlErrReturn struct {
  209. Xml
  210. }
  211. func (XmlErrReturn) Get() error {
  212. return errors.New("error")
  213. }
  214. func TestReturnXmlError(t *testing.T) {
  215. buff := bytes.NewBufferString("")
  216. recorder := httptest.NewRecorder()
  217. recorder.Body = buff
  218. o := Classic()
  219. o.Get("/", new(XmlErrReturn))
  220. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  221. if err != nil {
  222. t.Error(err)
  223. }
  224. o.ServeHTTP(recorder, req)
  225. expect(t, recorder.Code, http.StatusOK)
  226. refute(t, len(buff.String()), 0)
  227. expect(t, strings.TrimSpace(buff.String()), `<err><content>error</content></err>`)
  228. }