<commit_before>package iffy

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/http/httptest"

	"testing"
	"text/template"
)

type Tester struct {
	t      *testing.T
	r      http.Handler
	Calls  []*Call
	values Values
}

type Headers map[string]string

type Call struct {
	Name       string
	Method     string
	QueryStr   string
	Body       string
	headers    Headers
	respObject interface{}
	checkers   []Checker
}

func (c *Call) ResponseObject(respObject interface{}) *Call {
	c.respObject = respObject
	return c
}

func (c *Call) Headers(h Headers) *Call {
	c.headers = h
	return c
}

func (c *Call) Checkers(ch ...Checker) *Call {
	c.checkers = ch
	return c
}

type Checker func(r *http.Response, body string, respObject interface{}) error

// Tester

func NewTester(t *testing.T, r http.Handler, calls ...*Call) *Tester {
	return &Tester{
		t:      t,
		r:      r,
		values: make(Values),
	}
}

func (t *Tester) Reset() {
	t.Calls = []*Call{}
}

func (t *Tester) AddCall(name, method, querystr, body string) *Call {
	c := &Call{
		Name:     name,
		Method:   method,
		QueryStr: querystr,
		Body:     body,
	}
	t.Calls = append(t.Calls, c)
	return c
}

func (t *Tester) Run() {
	for _, c := range t.Calls {
		var body io.Reader
		if c.Body != "" {
			body = bytes.NewBuffer([]byte(t.applyTemplate(c.Body)))
		}
		req, err := http.NewRequest(c.Method, t.applyTemplate(c.QueryStr), body)
		if err != nil {
			t.t.Error(err)
			continue
		}
		if c.Body != "" {
			req.Header.Set("content-type", "application/json")
		}
		if c.headers != nil {
			for k, v := range c.headers {
				req.Header.Set(t.applyTemplate(k), t.applyTemplate(v))
			}
		}
		w := httptest.NewRecorder()
		t.r.ServeHTTP(w, req)
		resp := w.Result()
		var respBody string<commit_msg>As per Go documentation, body is always non-nil (#33)<commit_after>package iffy

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/http/httptest"

	"testing"
	"text/template"
)

type Tester struct {
	t      *testing.T
	r      http.Handler
	Calls  []*Call
	values Values
}

type Headers map[string]string

type Call struct {
	Name       string
	Method     string
	QueryStr   string
	Body       string
	headers    Headers
	respObject interface{}
	checkers   []Checker
}

func (c *Call) ResponseObject(respObject interface{}) *Call {
	c.respObject = respObject
	return c
}

func (c *Call) Headers(h Headers) *Call {
	c.headers = h
	return c
}

func (c *Call) Checkers(ch ...Checker) *Call {
	c.checkers = ch
	return c
}

type Checker func(r *http.Response, body string, respObject interface{}) error

// Tester

func NewTester(t *testing.T, r http.Handler, calls ...*Call) *Tester {
	return &Tester{
		t:      t,
		r:      r,
		values: make(Values),
	}
}

func (t *Tester) Reset() {
	t.Calls = []*Call{}
}

func (t *Tester) AddCall(name, method, querystr, body string) *Call {
	c := &Call{
		Name:     name,
		Method:   method,
		QueryStr: querystr,
		Body:     body,
	}
	t.Calls = append(t.Calls, c)
	return c
}

func (t *Tester) Run() {
	for _, c := range t.Calls {
		body := bytes.NewBufferString(t.applyTemplate(c.Body))
		req, err := http.NewRequest(c.Method, t.applyTemplate(c.QueryStr), body)
		if err != nil {
			t.t.Error(err)
			continue
		}
		if c.Body != "" {
			req.Header.Set("content-type", "application/json")
		}
		if c.headers != nil {
			for k, v := range c.headers {
				req.Header.Set(t.applyTemplate(k), t.applyTemplate(v))
			}
		}
		w := httptest.NewRecorder()
		t.r.ServeHTTP(w, req)
		resp := w.Result()
		var respBody string
{'fraction_non_alphanumeric': 0.1476923076923077, 'fraction_numerical': 0.0005594405594405594, 'mean_word_length': 3.036117381489842, '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': '288087', 'n_tokens_mistral': 1471, 'n_tokens_neox': 1393, 'n_words': 357}