Micro & pluggable web framework for Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

533 lines
11KB

  1. // Copyright 2015 The Tango Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tango
  5. import (
  6. "bytes"
  7. "encoding/xml"
  8. "errors"
  9. "net/http"
  10. "net/http/httptest"
  11. "strings"
  12. "testing"
  13. )
  14. type MyReturn struct {
  15. }
  16. func (m MyReturn) Get() string {
  17. return "string return"
  18. }
  19. func (m MyReturn) Post() []byte {
  20. return []byte("bytes return")
  21. }
  22. func (m MyReturn) Put() error {
  23. return errors.New("error return")
  24. }
  25. func TestReturn(t *testing.T) {
  26. buff := bytes.NewBufferString("")
  27. recorder := httptest.NewRecorder()
  28. recorder.Body = buff
  29. o := Classic()
  30. o.Any("/", new(MyReturn))
  31. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  32. if err != nil {
  33. t.Error(err)
  34. }
  35. o.ServeHTTP(recorder, req)
  36. expect(t, recorder.Code, http.StatusOK)
  37. refute(t, len(buff.String()), 0)
  38. expect(t, buff.String(), "string return")
  39. buff.Reset()
  40. req, err = http.NewRequest("POST", "http://localhost:8000/", nil)
  41. if err != nil {
  42. t.Error(err)
  43. }
  44. o.ServeHTTP(recorder, req)
  45. expect(t, recorder.Code, http.StatusOK)
  46. refute(t, len(buff.String()), 0)
  47. expect(t, buff.String(), "bytes return")
  48. }
  49. func TestReturnPut(t *testing.T) {
  50. buff := bytes.NewBufferString("")
  51. recorder := httptest.NewRecorder()
  52. recorder.Body = buff
  53. o := Classic()
  54. o.Any("/", new(MyReturn))
  55. req, err := http.NewRequest("PUT", "http://localhost:8000/", nil)
  56. if err != nil {
  57. t.Error(err)
  58. }
  59. o.ServeHTTP(recorder, req)
  60. expect(t, recorder.Code, http.StatusInternalServerError)
  61. refute(t, len(buff.String()), 0)
  62. expect(t, buff.String(), "error return")
  63. }
  64. type MyReturn2 struct {
  65. }
  66. func (m MyReturn2) Any() string {
  67. return "string return"
  68. }
  69. func TestReturn2(t *testing.T) {
  70. buff := bytes.NewBufferString("")
  71. recorder := httptest.NewRecorder()
  72. recorder.Body = buff
  73. o := Classic()
  74. o.Any("/", new(MyReturn2))
  75. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  76. if err != nil {
  77. t.Error(err)
  78. }
  79. o.ServeHTTP(recorder, req)
  80. expect(t, recorder.Code, http.StatusOK)
  81. refute(t, len(buff.String()), 0)
  82. expect(t, buff.String(), "string return")
  83. buff.Reset()
  84. req, err = http.NewRequest("POST", "http://localhost:8000/", nil)
  85. if err != nil {
  86. t.Error(err)
  87. }
  88. o.ServeHTTP(recorder, req)
  89. expect(t, recorder.Code, http.StatusOK)
  90. refute(t, len(buff.String()), 0)
  91. expect(t, buff.String(), "string return")
  92. }
  93. type MyReturn3 struct {
  94. JSON
  95. }
  96. func (m MyReturn3) Any() interface{} {
  97. return map[string]interface{}{
  98. "a": "b",
  99. }
  100. }
  101. func TestReturn3(t *testing.T) {
  102. buff := bytes.NewBufferString("")
  103. recorder := httptest.NewRecorder()
  104. recorder.Body = buff
  105. o := Classic()
  106. o.Any("/", new(MyReturn3))
  107. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  108. if err != nil {
  109. t.Error(err)
  110. }
  111. o.ServeHTTP(recorder, req)
  112. expect(t, recorder.Code, http.StatusOK)
  113. refute(t, len(buff.String()), 0)
  114. expect(t, strings.TrimSpace(buff.String()), `{"a":"b"}`)
  115. buff.Reset()
  116. req, err = http.NewRequest("POST", "http://localhost:8000/", nil)
  117. if err != nil {
  118. t.Error(err)
  119. }
  120. o.ServeHTTP(recorder, req)
  121. expect(t, recorder.Code, http.StatusOK)
  122. refute(t, len(buff.String()), 0)
  123. expect(t, strings.TrimSpace(buff.String()), `{"a":"b"}`)
  124. }
  125. type MyReturn4 struct {
  126. JSON
  127. }
  128. func (m MyReturn4) Any() interface{} {
  129. return map[string]interface{}{
  130. "a": "b",
  131. }
  132. }
  133. func TestReturn4(t *testing.T) {
  134. buff := bytes.NewBufferString("")
  135. recorder := httptest.NewRecorder()
  136. recorder.Body = buff
  137. o := Classic()
  138. o.Group("/1", func(g *Group) {
  139. g.Any("/", new(MyReturn4))
  140. })
  141. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  142. if err != nil {
  143. t.Error(err)
  144. }
  145. o.ServeHTTP(recorder, req)
  146. expect(t, recorder.Code, http.StatusOK)
  147. refute(t, len(buff.String()), 0)
  148. expect(t, strings.TrimSpace(buff.String()), `{"a":"b"}`)
  149. buff.Reset()
  150. req, err = http.NewRequest("POST", "http://localhost:8000/1", nil)
  151. if err != nil {
  152. t.Error(err)
  153. }
  154. o.ServeHTTP(recorder, req)
  155. expect(t, recorder.Code, http.StatusOK)
  156. refute(t, len(buff.String()), 0)
  157. expect(t, strings.TrimSpace(buff.String()), `{"a":"b"}`)
  158. }
  159. type JSONReturn struct {
  160. JSON
  161. }
  162. func (JSONReturn) Get() interface{} {
  163. return map[string]interface{}{
  164. "test1": 1,
  165. "test2": "2",
  166. "test3": true,
  167. }
  168. }
  169. func TestReturnJson1(t *testing.T) {
  170. buff := bytes.NewBufferString("")
  171. recorder := httptest.NewRecorder()
  172. recorder.Body = buff
  173. o := Classic()
  174. o.Get("/", new(JSONReturn))
  175. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  176. if err != nil {
  177. t.Error(err)
  178. }
  179. o.ServeHTTP(recorder, req)
  180. expect(t, recorder.Code, http.StatusOK)
  181. refute(t, len(buff.String()), 0)
  182. expect(t, strings.TrimSpace(buff.String()), `{"test1":1,"test2":"2","test3":true}`)
  183. }
  184. type JSONErrReturn struct {
  185. JSON
  186. }
  187. func (JSONErrReturn) Get() error {
  188. return errors.New("error")
  189. }
  190. func TestReturnJsonError(t *testing.T) {
  191. buff := bytes.NewBufferString("")
  192. recorder := httptest.NewRecorder()
  193. recorder.Body = buff
  194. o := Classic()
  195. o.Get("/", new(JSONErrReturn))
  196. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  197. if err != nil {
  198. t.Error(err)
  199. }
  200. o.ServeHTTP(recorder, req)
  201. expect(t, recorder.Code, http.StatusOK)
  202. refute(t, len(buff.String()), 0)
  203. expect(t, strings.TrimSpace(buff.String()), `{"err":"error"}`)
  204. }
  205. type JSONErrReturn2 struct {
  206. JSON
  207. }
  208. func (JSONErrReturn2) Get() error {
  209. return Abort(http.StatusInternalServerError, "error")
  210. }
  211. func TestReturnJsonError2(t *testing.T) {
  212. buff := bytes.NewBufferString("")
  213. recorder := httptest.NewRecorder()
  214. recorder.Body = buff
  215. o := Classic()
  216. o.Get("/", new(JSONErrReturn2))
  217. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  218. if err != nil {
  219. t.Error(err)
  220. }
  221. o.ServeHTTP(recorder, req)
  222. expect(t, recorder.Code, http.StatusInternalServerError)
  223. refute(t, len(buff.String()), 0)
  224. expect(t, strings.TrimSpace(buff.String()), `{"err":"error"}`)
  225. }
  226. type JSONErrReturn3 struct {
  227. JSON
  228. }
  229. type MyError struct {
  230. }
  231. func (m *MyError) Error() string {
  232. return "error"
  233. }
  234. func (m *MyError) ErrorCode() int {
  235. return 1
  236. }
  237. func (JSONErrReturn3) Get() error {
  238. return &MyError{}
  239. }
  240. func TestReturnJsonError3(t *testing.T) {
  241. buff := bytes.NewBufferString("")
  242. recorder := httptest.NewRecorder()
  243. recorder.Body = buff
  244. o := Classic()
  245. o.Get("/", new(JSONErrReturn3))
  246. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  247. if err != nil {
  248. t.Error(err)
  249. }
  250. o.ServeHTTP(recorder, req)
  251. expect(t, recorder.Code, http.StatusOK)
  252. refute(t, len(buff.String()), 0)
  253. expect(t, strings.TrimSpace(buff.String()), `{"err":"error","err_code":1}`)
  254. }
  255. type JSONReturn1 struct {
  256. JSON
  257. }
  258. func (JSONReturn1) Get() string {
  259. return "return"
  260. }
  261. func TestReturnJson2(t *testing.T) {
  262. buff := bytes.NewBufferString("")
  263. recorder := httptest.NewRecorder()
  264. recorder.Body = buff
  265. o := Classic()
  266. o.Get("/", new(JSONReturn1))
  267. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  268. if err != nil {
  269. t.Error(err)
  270. }
  271. o.ServeHTTP(recorder, req)
  272. expect(t, recorder.Code, http.StatusOK)
  273. refute(t, len(buff.String()), 0)
  274. expect(t, strings.TrimSpace(buff.String()), `{"content":"return"}`)
  275. }
  276. type JSONReturn2 struct {
  277. JSON
  278. }
  279. func (JSONReturn2) Get() []byte {
  280. return []byte("return")
  281. }
  282. func TestReturnJson3(t *testing.T) {
  283. buff := bytes.NewBufferString("")
  284. recorder := httptest.NewRecorder()
  285. recorder.Body = buff
  286. o := Classic()
  287. o.Get("/", new(JSONReturn2))
  288. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  289. if err != nil {
  290. t.Error(err)
  291. }
  292. o.ServeHTTP(recorder, req)
  293. expect(t, recorder.Code, http.StatusOK)
  294. refute(t, len(buff.String()), 0)
  295. expect(t, strings.TrimSpace(buff.String()), `{"content":"return"}`)
  296. }
  297. type JSONReturn3 struct {
  298. JSON
  299. }
  300. func (JSONReturn3) Get() (int, interface{}) {
  301. if true {
  302. return 201, map[string]string{
  303. "say": "Hello tango!",
  304. }
  305. }
  306. return 500, errors.New("something error")
  307. }
  308. func TestReturnJson4(t *testing.T) {
  309. buff := bytes.NewBufferString("")
  310. recorder := httptest.NewRecorder()
  311. recorder.Body = buff
  312. o := Classic()
  313. o.Get("/", new(JSONReturn3))
  314. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  315. if err != nil {
  316. t.Error(err)
  317. }
  318. o.ServeHTTP(recorder, req)
  319. expect(t, recorder.Code, 201)
  320. refute(t, len(buff.String()), 0)
  321. expect(t, strings.TrimSpace(buff.String()), `{"say":"Hello tango!"}`)
  322. }
  323. type XMLReturn struct {
  324. XML
  325. }
  326. type Address struct {
  327. City, State string
  328. }
  329. type Person struct {
  330. XMLName xml.Name `xml:"person"`
  331. ID int `xml:"id,attr"`
  332. FirstName string `xml:"name>first"`
  333. LastName string `xml:"name>last"`
  334. Age int `xml:"age"`
  335. Height float32 `xml:"height,omitempty"`
  336. Married bool
  337. Address
  338. Comment string `xml:",comment"`
  339. }
  340. func (XMLReturn) Get() interface{} {
  341. v := &Person{ID: 13, FirstName: "John", LastName: "Doe", Age: 42}
  342. v.Comment = " Need more details. "
  343. v.Address = Address{"Hanga Roa", "Easter Island"}
  344. return v
  345. }
  346. func TestReturnXML(t *testing.T) {
  347. buff := bytes.NewBufferString("")
  348. recorder := httptest.NewRecorder()
  349. recorder.Body = buff
  350. o := Classic()
  351. o.Get("/", new(XMLReturn))
  352. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  353. if err != nil {
  354. t.Error(err)
  355. }
  356. o.ServeHTTP(recorder, req)
  357. expect(t, recorder.Code, http.StatusOK)
  358. refute(t, len(buff.String()), 0)
  359. 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>`)
  360. }
  361. type XMLErrReturn struct {
  362. XML
  363. }
  364. func (XMLErrReturn) Get() error {
  365. return errors.New("error")
  366. }
  367. func TestReturnXmlError(t *testing.T) {
  368. buff := bytes.NewBufferString("")
  369. recorder := httptest.NewRecorder()
  370. recorder.Body = buff
  371. o := Classic()
  372. o.Get("/", new(XMLErrReturn))
  373. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  374. if err != nil {
  375. t.Error(err)
  376. }
  377. o.ServeHTTP(recorder, req)
  378. expect(t, recorder.Code, http.StatusOK)
  379. refute(t, len(buff.String()), 0)
  380. expect(t, strings.TrimSpace(buff.String()), `<err><content>error</content></err>`)
  381. }
  382. type JSONReturn7 struct {
  383. JSON
  384. }
  385. func (JSONReturn7) Get() (int, interface{}) {
  386. return 201, "sss"
  387. }
  388. func TestReturn7(t *testing.T) {
  389. buff := bytes.NewBufferString("")
  390. recorder := httptest.NewRecorder()
  391. recorder.Body = buff
  392. o := Classic()
  393. o.Get("/", new(JSONReturn7))
  394. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  395. if err != nil {
  396. t.Error(err)
  397. }
  398. o.ServeHTTP(recorder, req)
  399. expect(t, recorder.Code, 201)
  400. refute(t, len(buff.String()), 0)
  401. expect(t, strings.TrimSpace(buff.String()), `{"content":"sss"}`)
  402. }
  403. type Return8 struct {
  404. }
  405. func (Return8) Get() (int, interface{}) {
  406. return 403, "xxx"
  407. }
  408. func TestReturn8(t *testing.T) {
  409. buff := bytes.NewBufferString("")
  410. recorder := httptest.NewRecorder()
  411. recorder.Body = buff
  412. o := Classic()
  413. o.Get("/", new(Return8))
  414. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  415. if err != nil {
  416. t.Error(err)
  417. }
  418. o.ServeHTTP(recorder, req)
  419. expect(t, recorder.Code, 403)
  420. refute(t, len(buff.String()), 0)
  421. expect(t, strings.TrimSpace(buff.String()), `xxx`)
  422. }