middleware/http/recovery_test.go

package http_test

import (
	stdhttp "net/http"
	"net/http/httptest"
	"strings"
	"testing"

	mw "mercemay.top/src/lambdalog/middleware/http"
)

func TestRecovery_CatchesPanic(t *testing.T) {
	l := &stubLogger{}
	h := mw.Recovery(l, mw.RecoveryOptions{})(mw.PanicAsError("boom"))
	rec := httptest.NewRecorder()
	h.ServeHTTP(rec, httptest.NewRequest("GET", "/", nil))
	if rec.Code != 500 {
		t.Fatalf("code %d", rec.Code)
	}
	if !strings.Contains(rec.Body.String(), "internal") {
		t.Fatalf("body %q", rec.Body.String())
	}
	if len(l.msgs) == 0 {
		t.Fatal("no log messages")
	}
}

func TestRecovery_CustomStatusAndBody(t *testing.T) {
	l := &stubLogger{}
	h := mw.Recovery(l, mw.RecoveryOptions{Status: 503, Body: "gone"})(mw.PanicAsError("boom"))
	rec := httptest.NewRecorder()
	h.ServeHTTP(rec, httptest.NewRequest("GET", "/", nil))
	if rec.Code != 503 {
		t.Fatalf("code %d", rec.Code)
	}
	if rec.Body.String() != "gone" {
		t.Fatalf("body %q", rec.Body.String())
	}
}

func TestRecovery_NoPanicIsTransparent(t *testing.T) {
	l := &stubLogger{}
	h := mw.Recovery(l, mw.RecoveryOptions{})(stdhttp.HandlerFunc(func(w stdhttp.ResponseWriter, r *stdhttp.Request) {
		w.WriteHeader(204)
	}))
	rec := httptest.NewRecorder()
	h.ServeHTTP(rec, httptest.NewRequest("GET", "/", nil))
	if rec.Code != 204 {
		t.Fatalf("code %d", rec.Code)
	}
	if len(l.msgs) != 0 {
		t.Fatalf("unexpected log messages: %v", l.msgs)
	}
}

func TestRecovery_IncludeStack(t *testing.T) {
	l := &stubLogger{}
	h := mw.Recovery(l, mw.RecoveryOptions{IncludeStack: true})(mw.PanicAsError("boom"))
	h.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("GET", "/", nil))
	if len(l.msgs) == 0 {
		t.Fatal("expected a log message")
	}
}

func TestRecoveredError_Error(t *testing.T) {
	err := &mw.RecoveredError{Value: "x"}
	if !strings.Contains(err.Error(), "recovered panic") {
		t.Fatalf("error: %v", err)
	}
}