response_writer_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package gin
  5. import (
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. // TODO
  12. // func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  13. // func (w *responseWriter) CloseNotify() <-chan bool {
  14. // func (w *responseWriter) Flush() {
  15. var _ ResponseWriter = &responseWriter{}
  16. var _ http.ResponseWriter = &responseWriter{}
  17. var _ http.ResponseWriter = ResponseWriter(&responseWriter{})
  18. var _ http.Hijacker = ResponseWriter(&responseWriter{})
  19. var _ http.Flusher = ResponseWriter(&responseWriter{})
  20. var _ http.CloseNotifier = ResponseWriter(&responseWriter{})
  21. func init() {
  22. SetMode(TestMode)
  23. }
  24. func TestResponseWriterReset(t *testing.T) {
  25. testWriter := httptest.NewRecorder()
  26. writer := &responseWriter{}
  27. var w ResponseWriter = writer
  28. writer.reset(testWriter)
  29. assert.Equal(t, -1, writer.size)
  30. assert.Equal(t, http.StatusOK, writer.status)
  31. assert.Equal(t, testWriter, writer.ResponseWriter)
  32. assert.Equal(t, -1, w.Size())
  33. assert.Equal(t, http.StatusOK, w.Status())
  34. assert.False(t, w.Written())
  35. }
  36. func TestResponseWriterWriteHeader(t *testing.T) {
  37. testWriter := httptest.NewRecorder()
  38. writer := &responseWriter{}
  39. writer.reset(testWriter)
  40. w := ResponseWriter(writer)
  41. w.WriteHeader(http.StatusMultipleChoices)
  42. assert.False(t, w.Written())
  43. assert.Equal(t, http.StatusMultipleChoices, w.Status())
  44. assert.NotEqual(t, http.StatusMultipleChoices, testWriter.Code)
  45. w.WriteHeader(-1)
  46. assert.Equal(t, http.StatusMultipleChoices, w.Status())
  47. }
  48. func TestResponseWriterWriteHeadersNow(t *testing.T) {
  49. testWriter := httptest.NewRecorder()
  50. writer := &responseWriter{}
  51. writer.reset(testWriter)
  52. w := ResponseWriter(writer)
  53. w.WriteHeader(http.StatusMultipleChoices)
  54. w.WriteHeaderNow()
  55. assert.True(t, w.Written())
  56. assert.Equal(t, 0, w.Size())
  57. assert.Equal(t, http.StatusMultipleChoices, testWriter.Code)
  58. writer.size = 10
  59. w.WriteHeaderNow()
  60. assert.Equal(t, 10, w.Size())
  61. }
  62. func TestResponseWriterWrite(t *testing.T) {
  63. testWriter := httptest.NewRecorder()
  64. writer := &responseWriter{}
  65. writer.reset(testWriter)
  66. w := ResponseWriter(writer)
  67. n, err := w.Write([]byte("hola"))
  68. assert.Equal(t, 4, n)
  69. assert.Equal(t, 4, w.Size())
  70. assert.Equal(t, http.StatusOK, w.Status())
  71. assert.Equal(t, http.StatusOK, testWriter.Code)
  72. assert.Equal(t, "hola", testWriter.Body.String())
  73. assert.NoError(t, err)
  74. n, err = w.Write([]byte(" adios"))
  75. assert.Equal(t, 6, n)
  76. assert.Equal(t, 10, w.Size())
  77. assert.Equal(t, "hola adios", testWriter.Body.String())
  78. assert.NoError(t, err)
  79. }
  80. func TestResponseWriterHijack(t *testing.T) {
  81. testWriter := httptest.NewRecorder()
  82. writer := &responseWriter{}
  83. writer.reset(testWriter)
  84. w := ResponseWriter(writer)
  85. assert.Panics(t, func() {
  86. _, _, err := w.Hijack()
  87. assert.NoError(t, err)
  88. })
  89. assert.True(t, w.Written())
  90. assert.Panics(t, func() {
  91. w.CloseNotify()
  92. })
  93. w.Flush()
  94. }
  95. func TestResponseWriterFlush(t *testing.T) {
  96. testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  97. writer := &responseWriter{}
  98. writer.reset(w)
  99. writer.WriteHeader(http.StatusInternalServerError)
  100. writer.Flush()
  101. }))
  102. defer testServer.Close()
  103. // should return 500
  104. resp, err := http.Get(testServer.URL)
  105. assert.NoError(t, err)
  106. assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
  107. }