aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/_nuts/github.com/go-martini/martini/recovery_test.go
blob: 17e2e011310f3474cf00465066dccc47d21e61f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package martini

import (
	"bytes"
	"log"
	"net/http"
	"net/http/httptest"
	"testing"
)

func Test_Recovery(t *testing.T) {
	buff := bytes.NewBufferString("")
	recorder := httptest.NewRecorder()

	setENV(Dev)
	m := New()
	// replace log for testing
	m.Map(log.New(buff, "[martini] ", 0))
	m.Use(func(res http.ResponseWriter, req *http.Request) {
		res.Header().Set("Content-Type", "unpredictable")
	})
	m.Use(Recovery())
	m.Use(func(res http.ResponseWriter, req *http.Request) {
		panic("here is a panic!")
	})
	m.ServeHTTP(recorder, (*http.Request)(nil))
	expect(t, recorder.Code, http.StatusInternalServerError)
	expect(t, recorder.HeaderMap.Get("Content-Type"), "text/html")
	refute(t, recorder.Body.Len(), 0)
	refute(t, len(buff.String()), 0)
}

func Test_Recovery_ResponseWriter(t *testing.T) {
	recorder := httptest.NewRecorder()
	recorder2 := httptest.NewRecorder()

	setENV(Dev)
	m := New()
	m.Use(Recovery())
	m.Use(func(c Context) {
		c.MapTo(recorder2, (*http.ResponseWriter)(nil))
		panic("here is a panic!")
	})
	m.ServeHTTP(recorder, (*http.Request)(nil))

	expect(t, recorder2.Code, http.StatusInternalServerError)
	expect(t, recorder2.HeaderMap.Get("Content-Type"), "text/html")
	refute(t, recorder2.Body.Len(), 0)
}