@@ -26,6 +26,7 @@ import (
26
26
"code.gitea.io/gitea/routers"
27
27
"code.gitea.io/gitea/routers/routes"
28
28
29
+ "github.com/PuerkitoBio/goquery"
29
30
"github.com/Unknwon/com"
30
31
"github.com/stretchr/testify/assert"
31
32
"gopkg.in/macaron.v1"
@@ -260,12 +261,37 @@ func MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *httptest.
260
261
recorder := httptest .NewRecorder ()
261
262
mac .ServeHTTP (recorder , req )
262
263
if expectedStatus != NoExpectedStatus {
263
- assert .EqualValues (t , expectedStatus , recorder .Code ,
264
- "Request: %s %s" , req .Method , req .URL .String ())
264
+ if ! assert .EqualValues (t , expectedStatus , recorder .Code ,
265
+ "Request: %s %s" , req .Method , req .URL .String ()) {
266
+ logUnexpectedResponse (t , recorder )
267
+ }
265
268
}
266
269
return recorder
267
270
}
268
271
272
+ // logUnexpectedResponse logs the contents of an unexpected response.
273
+ func logUnexpectedResponse (t testing.TB , recorder * httptest.ResponseRecorder ) {
274
+ respBytes := recorder .Body .Bytes ()
275
+ if len (respBytes ) == 0 {
276
+ return
277
+ } else if len (respBytes ) < 500 {
278
+ // if body is short, just log the whole thing
279
+ t .Log ("Response:" , string (respBytes ))
280
+ return
281
+ }
282
+
283
+ // log the "flash" error message, if one exists
284
+ // we must create a new buffer, so that we don't "use up" resp.Body
285
+ htmlDoc , err := goquery .NewDocumentFromReader (bytes .NewBuffer (respBytes ))
286
+ if err != nil {
287
+ return // probably a non-HTML response
288
+ }
289
+ errMsg := htmlDoc .Find (".ui.negative.message" ).Text ()
290
+ if len (errMsg ) > 0 {
291
+ t .Log ("A flash error message was found:" , errMsg )
292
+ }
293
+ }
294
+
269
295
func DecodeJSON (t testing.TB , resp * httptest.ResponseRecorder , v interface {}) {
270
296
decoder := json .NewDecoder (resp .Body )
271
297
assert .NoError (t , decoder .Decode (v ))
0 commit comments