Skip to content

Commit 682ac11

Browse files
ethantkoeniglunny
authored andcommitted
Log unexpected responses in integration tests (#3138)
* Log flash error message in integration tests * Also log short, non-HTML responses
1 parent defc97a commit 682ac11

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

integrations/integration_test.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"code.gitea.io/gitea/routers"
2727
"code.gitea.io/gitea/routers/routes"
2828

29+
"github.com/PuerkitoBio/goquery"
2930
"github.com/Unknwon/com"
3031
"github.com/stretchr/testify/assert"
3132
"gopkg.in/macaron.v1"
@@ -260,12 +261,37 @@ func MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *httptest.
260261
recorder := httptest.NewRecorder()
261262
mac.ServeHTTP(recorder, req)
262263
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+
}
265268
}
266269
return recorder
267270
}
268271

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+
269295
func DecodeJSON(t testing.TB, resp *httptest.ResponseRecorder, v interface{}) {
270296
decoder := json.NewDecoder(resp.Body)
271297
assert.NoError(t, decoder.Decode(v))

0 commit comments

Comments
 (0)