return_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package tango
  2. import (
  3. "bytes"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. "errors"
  8. "encoding/xml"
  9. )
  10. type MyReturn struct {
  11. }
  12. func (m MyReturn) Get() string {
  13. return "string return"
  14. }
  15. func (m MyReturn) Post() []byte {
  16. return []byte("bytes return")
  17. }
  18. func (m MyReturn) Put() error {
  19. return errors.New("error return")
  20. }
  21. func TestReturn(t *testing.T) {
  22. buff := bytes.NewBufferString("")
  23. recorder := httptest.NewRecorder()
  24. recorder.Body = buff
  25. o := Classic()
  26. o.Any("/", new(MyReturn))
  27. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  28. if err != nil {
  29. t.Error(err)
  30. }
  31. o.ServeHTTP(recorder, req)
  32. expect(t, recorder.Code, http.StatusOK)
  33. refute(t, len(buff.String()), 0)
  34. expect(t, buff.String(), "string return")
  35. buff.Reset()
  36. req, err = http.NewRequest("POST", "http://localhost:8000/", nil)
  37. if err != nil {
  38. t.Error(err)
  39. }
  40. o.ServeHTTP(recorder, req)
  41. expect(t, recorder.Code, http.StatusOK)
  42. refute(t, len(buff.String()), 0)
  43. expect(t, buff.String(), "bytes return")
  44. }
  45. func TestReturnPut(t *testing.T) {
  46. buff := bytes.NewBufferString("")
  47. recorder := httptest.NewRecorder()
  48. recorder.Body = buff
  49. o := Classic()
  50. o.Any("/", new(MyReturn))
  51. req, err := http.NewRequest("PUT", "http://localhost:8000/", nil)
  52. if err != nil {
  53. t.Error(err)
  54. }
  55. o.ServeHTTP(recorder, req)
  56. expect(t, recorder.Code, http.StatusInternalServerError)
  57. refute(t, len(buff.String()), 0)
  58. expect(t, buff.String(), "error return")
  59. }
  60. type JsonReturn struct {
  61. Json
  62. }
  63. func (JsonReturn) Get() interface{} {
  64. return map[string]interface{}{
  65. "test1": 1,
  66. "test2": "2",
  67. "test3": true,
  68. }
  69. }
  70. func TestReturnJson(t *testing.T) {
  71. buff := bytes.NewBufferString("")
  72. recorder := httptest.NewRecorder()
  73. recorder.Body = buff
  74. o := Classic()
  75. o.Get("/", new(JsonReturn))
  76. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  77. if err != nil {
  78. t.Error(err)
  79. }
  80. o.ServeHTTP(recorder, req)
  81. expect(t, recorder.Code, http.StatusOK)
  82. refute(t, len(buff.String()), 0)
  83. expect(t, buff.String(), `{"test1":1,"test2":"2","test3":true}`)
  84. }
  85. type XmlReturn struct {
  86. Xml
  87. }
  88. type Address struct {
  89. City, State string
  90. }
  91. type Person struct {
  92. XMLName xml.Name `xml:"person"`
  93. Id int `xml:"id,attr"`
  94. FirstName string `xml:"name>first"`
  95. LastName string `xml:"name>last"`
  96. Age int `xml:"age"`
  97. Height float32 `xml:"height,omitempty"`
  98. Married bool
  99. Address
  100. Comment string `xml:",comment"`
  101. }
  102. func (XmlReturn) Get() interface{} {
  103. v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
  104. v.Comment = " Need more details. "
  105. v.Address = Address{"Hanga Roa", "Easter Island"}
  106. return v
  107. }
  108. func TestReturnXml(t *testing.T) {
  109. buff := bytes.NewBufferString("")
  110. recorder := httptest.NewRecorder()
  111. recorder.Body = buff
  112. o := Classic()
  113. o.Get("/", new(XmlReturn))
  114. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  115. if err != nil {
  116. t.Error(err)
  117. }
  118. o.ServeHTTP(recorder, req)
  119. expect(t, recorder.Code, http.StatusOK)
  120. refute(t, len(buff.String()), 0)
  121. 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>`)
  122. }