<commit_before>package panichandler

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

const PANICMSG = "panicmsg"
const BODYMSG = "panicmsg"

type pannicker struct{}

func setupTestServer() (*httptest.Server, *bytes.Buffer) {

	logw := bytes.NewBuffer(nil)
	testlog := log.New(logw, "", 0)
	delegate := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		panic(PANICMSG)
	})
	panicker := Decorate(
		nil, testlog, delegate)

	return httptest.NewServer(panicker), logw
}

func TestPanicHandler(t *testing.T) {
	path := "/foo/bar"
	ts, logw := setupTestServer()
	defer ts.Close()
	req, err := http.NewRequest("POST", ts.URL+path, bytes.NewReader([]byte(BODYMSG)))
	panicIf(err)
	res, err := http.DefaultClient.Do(req)
	panicIf(err)

	fmt.Println(res.Body)
	g := logw.String()
	fmt.Println(g)

	expect1 := regexp.MustCompile(PANICMSG)
	if !expect1.MatchString(g) {
		t.Errorf("test 1: got '%s', want '%s'", g, expect1)
	}

	expect2 := regexp.MustCompile(BODYMSG)
	if !expect2.MatchString(g) {
		t.Errorf("test 2: got '%s', want '%s'", g, expect2)
	}
}

func panicIf(err error) {
	if err != nil {
		panic(err)
	}
}<commit_msg>No need for setup function<commit_after>package panichandler

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

const PANICMSG = "panicmsg"
const BODYMSG = "panicmsg"

type pannicker struct{}

func TestPanicHandler(t *testing.T) {
	logw := bytes.NewBuffer(nil)
	testlog := log.New(logw, "", 0)
	delegate := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		panic(PANICMSG)
	})
	panicker := Decorate(
		nil, testlog, delegate)

	ts := httptest.NewServer(panicker)
	defer ts.Close()

	path := "/foo/bar"
	req, err := http.NewRequest("POST", ts.URL+path, bytes.NewReader([]byte(BODYMSG)))
	panicIf(err)
	_, err = http.DefaultClient.Do(req)
	panicIf(err)
	g := logw.String()

	expect1 := regexp.MustCompile(PANICMSG)
	if !expect1.MatchString(g) {
		t.Errorf("test 1: got '%s', want '%s'", g, expect1)
	}

	expect2 := regexp.MustCompile(BODYMSG)
	if !expect2.MatchString(g) {
		t.Errorf("test 2: got '%s', want '%s'", g, expect2)
	}
}

func panicIf(err error) {
	if err != nil {
		panic(err)
	}
}
{'fraction_non_alphanumeric': 0.1729119638826185, 'fraction_numerical': 0.008126410835214447, 'mean_word_length': 4.553884711779449, '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': '3384258', 'n_tokens_mistral': 936, 'n_tokens_neox': 899, 'n_words': 186}