response_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package restful
  2. import (
  3. "errors"
  4. "net/http"
  5. "net/http/httptest"
  6. "strings"
  7. "testing"
  8. )
  9. func TestWriteHeader(t *testing.T) {
  10. httpWriter := httptest.NewRecorder()
  11. resp := Response{httpWriter, "*/*", []string{"*/*"}, 0, 0, true, nil}
  12. resp.WriteHeader(123)
  13. if resp.StatusCode() != 123 {
  14. t.Errorf("Unexpected status code:%d", resp.StatusCode())
  15. }
  16. }
  17. func TestNoWriteHeader(t *testing.T) {
  18. httpWriter := httptest.NewRecorder()
  19. resp := Response{httpWriter, "*/*", []string{"*/*"}, 0, 0, true, nil}
  20. if resp.StatusCode() != http.StatusOK {
  21. t.Errorf("Unexpected status code:%d", resp.StatusCode())
  22. }
  23. }
  24. type food struct {
  25. Kind string
  26. }
  27. // go test -v -test.run TestMeasureContentLengthXml ...restful
  28. func TestMeasureContentLengthXml(t *testing.T) {
  29. httpWriter := httptest.NewRecorder()
  30. resp := Response{httpWriter, "*/*", []string{"*/*"}, 0, 0, true, nil}
  31. resp.WriteAsXml(food{"apple"})
  32. if resp.ContentLength() != 76 {
  33. t.Errorf("Incorrect measured length:%d", resp.ContentLength())
  34. }
  35. }
  36. // go test -v -test.run TestMeasureContentLengthJson ...restful
  37. func TestMeasureContentLengthJson(t *testing.T) {
  38. httpWriter := httptest.NewRecorder()
  39. resp := Response{httpWriter, "*/*", []string{"*/*"}, 0, 0, true, nil}
  40. resp.WriteAsJson(food{"apple"})
  41. if resp.ContentLength() != 22 {
  42. t.Errorf("Incorrect measured length:%d", resp.ContentLength())
  43. }
  44. }
  45. // go test -v -test.run TestMeasureContentLengthJsonNotPretty ...restful
  46. func TestMeasureContentLengthJsonNotPretty(t *testing.T) {
  47. httpWriter := httptest.NewRecorder()
  48. resp := Response{httpWriter, "*/*", []string{"*/*"}, 0, 0, false, nil}
  49. resp.WriteAsJson(food{"apple"})
  50. if resp.ContentLength() != 17 { // 16+1 using the Encoder directly yields another /n
  51. t.Errorf("Incorrect measured length:%d", resp.ContentLength())
  52. }
  53. }
  54. // go test -v -test.run TestMeasureContentLengthWriteErrorString ...restful
  55. func TestMeasureContentLengthWriteErrorString(t *testing.T) {
  56. httpWriter := httptest.NewRecorder()
  57. resp := Response{httpWriter, "*/*", []string{"*/*"}, 0, 0, true, nil}
  58. resp.WriteErrorString(404, "Invalid")
  59. if resp.ContentLength() != len("Invalid") {
  60. t.Errorf("Incorrect measured length:%d", resp.ContentLength())
  61. }
  62. }
  63. // go test -v -test.run TestStatusIsPassedToResponse ...restful
  64. func TestStatusIsPassedToResponse(t *testing.T) {
  65. for _, each := range []struct {
  66. write, read int
  67. }{
  68. {write: 204, read: 204},
  69. {write: 304, read: 304},
  70. {write: 200, read: 200},
  71. {write: 400, read: 400},
  72. } {
  73. httpWriter := httptest.NewRecorder()
  74. resp := Response{httpWriter, "*/*", []string{"*/*"}, 0, 0, true, nil}
  75. resp.WriteHeader(each.write)
  76. if got, want := httpWriter.Code, each.read; got != want {
  77. t.Errorf("got %v want %v", got, want)
  78. }
  79. }
  80. }
  81. // go test -v -test.run TestStatusCreatedAndContentTypeJson_Issue54 ...restful
  82. func TestStatusCreatedAndContentTypeJson_Issue54(t *testing.T) {
  83. httpWriter := httptest.NewRecorder()
  84. resp := Response{httpWriter, "application/json", []string{"application/json"}, 0, 0, true, nil}
  85. resp.WriteHeader(201)
  86. resp.WriteAsJson(food{"Juicy"})
  87. if httpWriter.HeaderMap.Get("Content-Type") != "application/json" {
  88. t.Errorf("Expected content type json but got:%s", httpWriter.HeaderMap.Get("Content-Type"))
  89. }
  90. if httpWriter.Code != 201 {
  91. t.Errorf("Expected status 201 but got:%d", httpWriter.Code)
  92. }
  93. }
  94. type errorOnWriteRecorder struct {
  95. *httptest.ResponseRecorder
  96. }
  97. func (e errorOnWriteRecorder) Write(bytes []byte) (int, error) {
  98. return 0, errors.New("fail")
  99. }
  100. // go test -v -test.run TestLastWriteErrorCaught ...restful
  101. func TestLastWriteErrorCaught(t *testing.T) {
  102. httpWriter := errorOnWriteRecorder{httptest.NewRecorder()}
  103. resp := Response{httpWriter, "application/json", []string{"application/json"}, 0, 0, true, nil}
  104. err := resp.WriteAsJson(food{"Juicy"})
  105. if err.Error() != "fail" {
  106. t.Errorf("Unexpected error message:%v", err)
  107. }
  108. }
  109. // go test -v -test.run TestAcceptStarStar_Issue83 ...restful
  110. func TestAcceptStarStar_Issue83(t *testing.T) {
  111. httpWriter := httptest.NewRecorder()
  112. // Accept Produces
  113. resp := Response{httpWriter, "application/bogus,*/*;q=0.8", []string{"application/json"}, 0, 0, true, nil}
  114. resp.WriteEntity(food{"Juicy"})
  115. ct := httpWriter.Header().Get("Content-Type")
  116. if "application/json" != ct {
  117. t.Errorf("Unexpected content type:%s", ct)
  118. }
  119. }
  120. // go test -v -test.run TestAcceptSkipStarStar_Issue83 ...restful
  121. func TestAcceptSkipStarStar_Issue83(t *testing.T) {
  122. httpWriter := httptest.NewRecorder()
  123. // Accept Produces
  124. resp := Response{httpWriter, " application/xml ,*/* ; q=0.8", []string{"application/json", "application/xml"}, 0, 0, true, nil}
  125. resp.WriteEntity(food{"Juicy"})
  126. ct := httpWriter.Header().Get("Content-Type")
  127. if "application/xml" != ct {
  128. t.Errorf("Unexpected content type:%s", ct)
  129. }
  130. }
  131. // go test -v -test.run TestAcceptXmlBeforeStarStar_Issue83 ...restful
  132. func TestAcceptXmlBeforeStarStar_Issue83(t *testing.T) {
  133. httpWriter := httptest.NewRecorder()
  134. // Accept Produces
  135. resp := Response{httpWriter, "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", []string{"application/json"}, 0, 0, true, nil}
  136. resp.WriteEntity(food{"Juicy"})
  137. ct := httpWriter.Header().Get("Content-Type")
  138. if "application/json" != ct {
  139. t.Errorf("Unexpected content type:%s", ct)
  140. }
  141. }
  142. // go test -v -test.run TestWriteHeaderNoContent_Issue124 ...restful
  143. func TestWriteHeaderNoContent_Issue124(t *testing.T) {
  144. httpWriter := httptest.NewRecorder()
  145. resp := Response{httpWriter, "text/plain", []string{"text/plain"}, 0, 0, true, nil}
  146. resp.WriteHeader(http.StatusNoContent)
  147. if httpWriter.Code != http.StatusNoContent {
  148. t.Errorf("got %d want %d", httpWriter.Code, http.StatusNoContent)
  149. }
  150. }
  151. // go test -v -test.run TestStatusCreatedAndContentTypeJson_Issue163 ...restful
  152. func TestStatusCreatedAndContentTypeJson_Issue163(t *testing.T) {
  153. httpWriter := httptest.NewRecorder()
  154. resp := Response{httpWriter, "application/json", []string{"application/json"}, 0, 0, true, nil}
  155. resp.WriteHeader(http.StatusNotModified)
  156. if httpWriter.Code != http.StatusNotModified {
  157. t.Errorf("Got %d want %d", httpWriter.Code, http.StatusNotModified)
  158. }
  159. }
  160. func TestWriteHeaderAndEntity_Issue235(t *testing.T) {
  161. httpWriter := httptest.NewRecorder()
  162. resp := Response{httpWriter, "application/json", []string{"application/json"}, 0, 0, true, nil}
  163. var pong = struct {
  164. Foo string `json:"foo"`
  165. }{Foo: "123"}
  166. resp.WriteHeaderAndEntity(404, pong)
  167. if httpWriter.Code != http.StatusNotFound {
  168. t.Errorf("got %d want %d", httpWriter.Code, http.StatusNoContent)
  169. }
  170. if got, want := httpWriter.Header().Get("Content-Type"), "application/json"; got != want {
  171. t.Errorf("got %v want %v", got, want)
  172. }
  173. if !strings.HasPrefix(httpWriter.Body.String(), "{") {
  174. t.Errorf("expected pong struct in json:%s", httpWriter.Body.String())
  175. }
  176. }
  177. func TestWriteEntityNoAcceptMatchWithProduces(t *testing.T) {
  178. httpWriter := httptest.NewRecorder()
  179. resp := Response{httpWriter, "application/bogus", []string{"application/json"}, 0, 0, true, nil}
  180. resp.WriteEntity("done")
  181. if httpWriter.Code != http.StatusOK {
  182. t.Errorf("got %d want %d", httpWriter.Code, http.StatusOK)
  183. }
  184. }
  185. func TestWriteEntityNoAcceptMatchNoProduces(t *testing.T) {
  186. httpWriter := httptest.NewRecorder()
  187. resp := Response{httpWriter, "application/bogus", []string{}, 0, 0, true, nil}
  188. resp.WriteEntity("done")
  189. if httpWriter.Code != http.StatusNotAcceptable {
  190. t.Errorf("got %d want %d", httpWriter.Code, http.StatusNotAcceptable)
  191. }
  192. }