<commit_before>package negroni

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

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

	rec := NewRecovery()
	rec.Logger = log.New(buff, "[negroni] ", 0)
	rec.ErrorHandlerFunc = func(i interface{}) {
		handlerCalled = true
	}

	n := New()
	// replace log for testing
	n.Use(rec)
	n.UseHandler(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
		panic("here is a panic!")
	}))
	n.ServeHTTP(recorder, (*http.Request)(nil))
	expect(t, recorder.Header().Get("Content-Type"), "text/plain; charset=utf-8")
	expect(t, recorder.Code, http.StatusInternalServerError)
	expect(t, handlerCalled, true)
	refute(t, recorder.Body.Len(), 0)
	refute(t, len(buff.String()), 0)
}

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

	rec := NewRecovery()
	rec.Logger = log.New(bytes.NewBuffer([]byte{}), "[negroni] ", 0)

	n := New()
	n.Use(rec)
	n.UseHandler(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
		res.Header().Set("Content-Type", "application/javascript; charset=utf-8")
		panic("here is a panic!")
	}))
	n.ServeHTTP(recorder, (*http.Request)(nil))
	expect(t, recorder.Header().Get("Content-Type"), "application/javascript; charset=utf-8")
}

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

	rec := NewRecovery()
	rec.Logger = log.New(buff, "[negroni] ", 0)
	rec.ErrorHandlerFunc = func(i interface{}) {
		panic("callback panic")
	}

	n := New()
	n.Use(rec)
	n.UseHandler(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
		panic("here is a panic!")
	}))
	n.ServeHTTP(recorder, (*http.Request)(nil))

	expect(t, strings.Contains(buff.String(), "callback panic"), true)
}

type testOutput struct {
	*bytes.Buffer
}

func newTestOutput() *testOutput {
	buf := bytes.NewBufferString("")
	return &testOutput{buf}
}

func (t *testOutput) FormatPanicError(rw http.ResponseWriter, r *http.Request, infos *PanicInformations) {
	fmt.Fprintf(t, formatInfos(infos))
}

func formatInfos(infos *PanicInformations) string {
	return fmt.Sprintf("%s %s", infos.RequestDescription(), infos.RecoveredElement)
}
func TestRecovery_formatter(t *testing.T) {
	recorder := httptest.NewRecorder()
	formatter := newTestOutput()

	req, _ := http.NewRequest("GET", "http://localhost:3003/somePath?element=true", http.NoBody)
	var element interface{} = "here is a panic!"
	expectedInfos := &PanicInformations{RecoveredElement: element, Request: req}

	rec := NewRecovery()
	rec.SetFormatter(formatter)
	n := New()
	n.Use(rec)
	n.UseHandler(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
		panic(element)
	}))

	n.ServeHTTP(recorder, req)

	expect(t, formatInfos(expectedInfos), formatter.String())
}

func TestRecovery_PanicInformations(t *testing.T) {
	// Request with query
	req, _ := http.NewRequest("GET", "http://localhost:3003/somePath?element=true", http.NoBody)
	var element interface{} = "here is a panic!"
	expectedInfos := &PanicInformations{RecoveredElement: element, Request: req}

	expect(t, expectedInfos.RequestDescription(), "GET /somePath?element=true")

	// Request without Query
	req, _ = http.NewRequest("POST", "http://localhost:3003/somePath", http.NoBody)
	element = "here is a panic!"
	expectedInfos = &PanicInformations{RecoveredElement: element, Request: req}

	expect(t, expectedInfos.RequestDescription(), "POST /somePath")

	// Nil request
	expectedInfos = &PanicInformations{RecoveredElement: element, Request: nil}
	expect(t, expectedInfos.RequestDescription(), nilRequestMessage)

	// Stack<commit_msg>Attempt to fix CI<commit_after>package negroni

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

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

	rec := NewRecovery()
	rec.Logger = log.New(buff, "[negroni] ", 0)
	rec.ErrorHandlerFunc = func(i interface{}) {
		handlerCalled = true
	}

	n := New()
	// replace log for testing
	n.Use(rec)
	n.UseHandler(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
		panic("here is a panic!")
	}))
	n.ServeHTTP(recorder, (*http.Request)(nil))
	expect(t, recorder.Header().Get("Content-Type"), "text/plain; charset=utf-8")
	expect(t, recorder.Code, http.StatusInternalServerError)
	expect(t, handlerCalled, true)
	refute(t, recorder.Body.Len(), 0)
	refute(t, len(buff.String()), 0)
}

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

	rec := NewRecovery()
	rec.Logger = log.New(bytes.NewBuffer([]byte{}), "[negroni] ", 0)

	n := New()
	n.Use(rec)
	n.UseHandler(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
		res.Header().Set("Content-Type", "application/javascript; charset=utf-8")
		panic("here is a panic!")
	}))
	n.ServeHTTP(recorder, (*http.Request)(nil))
	expect(t, recorder.Header().Get("Content-Type"), "application/javascript; charset=utf-8")
}

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

	rec := NewRecovery()
	rec.Logger = log.New(buff, "[negroni] ", 0)
	rec.ErrorHandlerFunc = func(i interface{}) {
		panic("callback panic")
	}

	n := New()
	n.Use(rec)
	n.UseHandler(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
		panic("here is a panic!")
	}))
	n.ServeHTTP(recorder, (*http.Request)(nil))

	expect(t, strings.Contains(buff.String(), "callback panic"), true)
}

type testOutput struct {
	*bytes.Buffer
}

func newTestOutput() *testOutput {
	buf := bytes.NewBufferString("")
	return &testOutput{buf}
}

func (t *testOutput) FormatPanicError(rw http.ResponseWriter, r *http.Request, infos *PanicInformations) {
	fmt.Fprintf(t, formatInfos(infos))
}

func formatInfos(infos *PanicInformations) string {
	return fmt.Sprintf("%s %s", infos.RequestDescription(), infos.RecoveredElement)
}
func TestRecovery_formatter(t *testing.T) {
	recorder := httptest.NewRecorder()
	formatter := newTestOutput()

	req, _ := http.NewRequest("GET", "http://localhost:3003/somePath?element=true", nil)
	var element interface{} = "here is a panic!"
	expectedInfos := &PanicInformations{RecoveredElement: element, Request: req}

	rec := NewRecovery()
	rec.SetFormatter(formatter)
	n := New()
	n.Use(rec)
	n.UseHandler(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
		panic(element)
	}))

	n.ServeHTTP(recorder, req)

	expect(t, formatInfos(expectedInfos), formatter.String())
}

func TestRecovery_PanicInformations(t *testing.T) {
	// Request with query
	req, _ := http.NewRequest("GET", "http://localhost:3003/somePath?element=true", nil)
	var element interface{} = "here is a panic!"
	expectedInfos := &PanicInformations{RecoveredElement: element, Request: req}

	expect(t, expectedInfos.RequestDescription(), "GET /somePath?element=true")

	// Request without Query
	req, _ = http.NewRequest("POST", "http://localhost:3003/somePath", nil)
	element = "here is a panic!"
	expectedInfos = &PanicInformations{RecoveredElement: element, Request: req}

	expect(t, expectedInfos.RequestDescription(), "POST /somePath")

	// Nil request
	expectedInfos = &PanicInformations{RecoveredElement: element, Request: nil}
	expect(t, expectedInfos.RequestDescription(), nilRequestMessage)

	// Stack
{'fraction_non_alphanumeric': 0.1615959434214038, 'fraction_numerical': 0.0053376034160661865, 'mean_word_length': 5.674087266251113, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3660819', 'n_tokens_mistral': 2711, 'n_tokens_neox': 2600, 'n_words': 518}