Skip to content

Commit 6dd76ba

Browse files
committed
Throw away big responses
1 parent a9177cd commit 6dd76ba

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

integrations/git_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ func testGit(t *testing.T, u *url.URL) {
9595
assert.Equal(t, littleSize, resp.Body.Len())
9696

9797
req = NewRequest(t, "GET", path.Join("/user2/repo-tmp-17/raw/branch/master/", big))
98-
resp = session.MakeRequest(t, req, http.StatusOK)
99-
assert.Equal(t, bigSize, resp.Body.Len())
98+
nilResp := session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
99+
assert.Equal(t, bigSize, nilResp.Length)
100100

101101
req = NewRequest(t, "GET", path.Join("/user2/repo-tmp-17/raw/branch/master/", littleLFS))
102102
resp = session.MakeRequest(t, req, http.StatusOK)
@@ -114,20 +114,20 @@ func testGit(t *testing.T, u *url.URL) {
114114

115115
// Request media paths
116116
req := NewRequest(t, "GET", path.Join("/user2/repo-tmp-17/media/branch/master/", little))
117-
resp := session.MakeRequest(t, req, http.StatusOK)
118-
assert.Equal(t, littleSize, resp.Body.Len())
117+
resp := session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
118+
assert.Equal(t, littleSize, resp.Length)
119119

120120
req = NewRequest(t, "GET", path.Join("/user2/repo-tmp-17/media/branch/master/", big))
121-
resp = session.MakeRequest(t, req, http.StatusOK)
122-
assert.Equal(t, bigSize, resp.Body.Len())
121+
resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
122+
assert.Equal(t, bigSize, resp.Length)
123123

124124
req = NewRequest(t, "GET", path.Join("/user2/repo-tmp-17/media/branch/master/", littleLFS))
125-
resp = session.MakeRequest(t, req, http.StatusOK)
126-
assert.Equal(t, littleSize, resp.Body.Len())
125+
resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
126+
assert.Equal(t, littleSize, resp.Length)
127127

128128
req = NewRequest(t, "GET", path.Join("/user2/repo-tmp-17/media/branch/master/", bigLFS))
129-
resp = session.MakeRequest(t, req, http.StatusOK)
130-
assert.Equal(t, bigSize, resp.Body.Len())
129+
resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
130+
assert.Equal(t, bigSize, resp.Length)
131131
})
132132

133133
})

integrations/integration_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,23 @@ import (
3535

3636
var mac *macaron.Macaron
3737

38+
type NilResponseRecorder struct {
39+
httptest.ResponseRecorder
40+
Length int
41+
}
42+
43+
func (n *NilResponseRecorder) Write(b []byte) (int, error) {
44+
n.Length = n.Length + len(b)
45+
return len(b), nil
46+
}
47+
48+
// NewRecorder returns an initialized ResponseRecorder.
49+
func NewNilResponseRecorder() *NilResponseRecorder {
50+
return &NilResponseRecorder{
51+
ResponseRecorder: *httptest.NewRecorder(),
52+
}
53+
}
54+
3855
func TestMain(m *testing.M) {
3956
initIntegrationTest()
4057
mac = routes.NewMacaron()
@@ -192,6 +209,22 @@ func (s *TestSession) MakeRequest(t testing.TB, req *http.Request, expectedStatu
192209
return resp
193210
}
194211

212+
func (s *TestSession) MakeRequestNilResponseRecorder(t testing.TB, req *http.Request, expectedStatus int) *NilResponseRecorder {
213+
baseURL, err := url.Parse(setting.AppURL)
214+
assert.NoError(t, err)
215+
for _, c := range s.jar.Cookies(baseURL) {
216+
req.AddCookie(c)
217+
}
218+
resp := MakeRequestNilResponseRecorder(t, req, expectedStatus)
219+
220+
ch := http.Header{}
221+
ch.Add("Cookie", strings.Join(resp.HeaderMap["Set-Cookie"], ";"))
222+
cr := http.Request{Header: ch}
223+
s.jar.SetCookies(baseURL, cr.Cookies())
224+
225+
return resp
226+
}
227+
195228
const userPassword = "password"
196229

197230
var loginSessionCache = make(map[string]*TestSession, 10)
@@ -305,6 +338,18 @@ func MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *httptest.
305338
return recorder
306339
}
307340

341+
func MakeRequestNilResponseRecorder(t testing.TB, req *http.Request, expectedStatus int) *NilResponseRecorder {
342+
recorder := NewNilResponseRecorder()
343+
mac.ServeHTTP(recorder, req)
344+
if expectedStatus != NoExpectedStatus {
345+
if !assert.EqualValues(t, expectedStatus, recorder.Code,
346+
"Request: %s %s", req.Method, req.URL.String()) {
347+
logUnexpectedResponse(t, &recorder.ResponseRecorder)
348+
}
349+
}
350+
return recorder
351+
}
352+
308353
// logUnexpectedResponse logs the contents of an unexpected response.
309354
func logUnexpectedResponse(t testing.TB, recorder *httptest.ResponseRecorder) {
310355
respBytes := recorder.Body.Bytes()

0 commit comments

Comments
 (0)