1
2
3
4
5 package gin
6
7 import (
8 "net/http"
9 "net/http/httptest"
10 "testing"
11
12 "github.com/stretchr/testify/assert"
13 )
14
15
16
17
18
19
20 var (
21 _ ResponseWriter = &responseWriter{}
22 _ http.ResponseWriter = &responseWriter{}
23 _ http.ResponseWriter = ResponseWriter(&responseWriter{})
24 _ http.Hijacker = ResponseWriter(&responseWriter{})
25 _ http.Flusher = ResponseWriter(&responseWriter{})
26 _ http.CloseNotifier = ResponseWriter(&responseWriter{})
27 )
28
29 func init() {
30 SetMode(TestMode)
31 }
32
33 func TestResponseWriterUnwrap(t *testing.T) {
34 testWriter := httptest.NewRecorder()
35 writer := &responseWriter{ResponseWriter: testWriter}
36 assert.Same(t, testWriter, writer.Unwrap())
37 }
38
39 func TestResponseWriterReset(t *testing.T) {
40 testWriter := httptest.NewRecorder()
41 writer := &responseWriter{}
42 var w ResponseWriter = writer
43
44 writer.reset(testWriter)
45 assert.Equal(t, -1, writer.size)
46 assert.Equal(t, http.StatusOK, writer.status)
47 assert.Equal(t, testWriter, writer.ResponseWriter)
48 assert.Equal(t, -1, w.Size())
49 assert.Equal(t, http.StatusOK, w.Status())
50 assert.False(t, w.Written())
51 }
52
53 func TestResponseWriterWriteHeader(t *testing.T) {
54 testWriter := httptest.NewRecorder()
55 writer := &responseWriter{}
56 writer.reset(testWriter)
57 w := ResponseWriter(writer)
58
59 w.WriteHeader(http.StatusMultipleChoices)
60 assert.False(t, w.Written())
61 assert.Equal(t, http.StatusMultipleChoices, w.Status())
62 assert.NotEqual(t, http.StatusMultipleChoices, testWriter.Code)
63
64 w.WriteHeader(-1)
65 assert.Equal(t, http.StatusMultipleChoices, w.Status())
66 }
67
68 func TestResponseWriterWriteHeadersNow(t *testing.T) {
69 testWriter := httptest.NewRecorder()
70 writer := &responseWriter{}
71 writer.reset(testWriter)
72 w := ResponseWriter(writer)
73
74 w.WriteHeader(http.StatusMultipleChoices)
75 w.WriteHeaderNow()
76
77 assert.True(t, w.Written())
78 assert.Equal(t, 0, w.Size())
79 assert.Equal(t, http.StatusMultipleChoices, testWriter.Code)
80
81 writer.size = 10
82 w.WriteHeaderNow()
83 assert.Equal(t, 10, w.Size())
84 }
85
86 func TestResponseWriterWrite(t *testing.T) {
87 testWriter := httptest.NewRecorder()
88 writer := &responseWriter{}
89 writer.reset(testWriter)
90 w := ResponseWriter(writer)
91
92 n, err := w.Write([]byte("hola"))
93 assert.Equal(t, 4, n)
94 assert.Equal(t, 4, w.Size())
95 assert.Equal(t, http.StatusOK, w.Status())
96 assert.Equal(t, http.StatusOK, testWriter.Code)
97 assert.Equal(t, "hola", testWriter.Body.String())
98 assert.NoError(t, err)
99
100 n, err = w.Write([]byte(" adios"))
101 assert.Equal(t, 6, n)
102 assert.Equal(t, 10, w.Size())
103 assert.Equal(t, "hola adios", testWriter.Body.String())
104 assert.NoError(t, err)
105 }
106
107 func TestResponseWriterHijack(t *testing.T) {
108 testWriter := httptest.NewRecorder()
109 writer := &responseWriter{}
110 writer.reset(testWriter)
111 w := ResponseWriter(writer)
112
113 assert.Panics(t, func() {
114 _, _, err := w.Hijack()
115 assert.NoError(t, err)
116 })
117 assert.True(t, w.Written())
118
119 assert.Panics(t, func() {
120 w.CloseNotify()
121 })
122
123 w.Flush()
124 }
125
126 func TestResponseWriterFlush(t *testing.T) {
127 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
128 writer := &responseWriter{}
129 writer.reset(w)
130
131 writer.WriteHeader(http.StatusInternalServerError)
132 writer.Flush()
133 }))
134 defer testServer.Close()
135
136
137 resp, err := http.Get(testServer.URL)
138 assert.NoError(t, err)
139 assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
140 }
141
142 func TestResponseWriterStatusCode(t *testing.T) {
143 testWriter := httptest.NewRecorder()
144 writer := &responseWriter{}
145 writer.reset(testWriter)
146 w := ResponseWriter(writer)
147
148 w.WriteHeader(http.StatusOK)
149 w.WriteHeaderNow()
150
151 assert.Equal(t, http.StatusOK, w.Status())
152 assert.True(t, w.Written())
153
154 w.WriteHeader(http.StatusUnauthorized)
155
156
157 assert.Equal(t, http.StatusOK, w.Status())
158 }
159
View as plain text