<commit_before>package bookkeeping

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"reflect"
	"strconv"
	"strings"
	"testing"

	"github.com/davecgh/go-spew/spew"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func testHeaders(t *testing.T, originalHeader http.Header, headersToCopy []string, expectedKeyValues []interface{}) {
	var (
		assert  = assert.New(t)
		require = require.New(t)

		request = &http.Request{
			Header: originalHeader,
		}

		rf = RequestHeaders(headersToCopy...)
	)

	require.NotNil(rf)
	returnedKeyValuePair := rf(request)
	assert.Equal(expectedKeyValues, returnedKeyValuePair)
}

func TestBookkeepingHeaders(t *testing.T) {
	testData := []struct {
		originalHeader   http.Header
		headersToCopy    []string
		expectedResponse []interface{}
	}{
		{
			http.Header{"X-Test-1": []string{"foo"}, "X-Test-2": []string{"foo", "bar"}, "X-Test-3": []string{}},
			nil,
			[]interface{}{},
		},
		{
			http.Header{"X-Test-1": []string{"foo"}, "X-Test-2": []string{"foo", "bar"}, "X-Test-3": []string{}},
			[]string{"X-Does-Not-Exist"},
			[]interface{}{},
		},
		{
			http.Header{"X-Test-1": []string{"foo"}, "X-Test-2": []string{"foo", "bar"}, "X-Test-3": []string{}},
			[]string{"X-Does-Not-Exist", "X-Test-1"},
			[]interface{}{"X-Test-1", []string{"foo"}},
		},
		{
			http.Header{"X-Test-1": []string{"foo"}, "X-Test-2": []string{"foo", "bar"}, "X-Test-3": []string{}},
			[]string{"X-Does-Not-Exist", "x-test-1"},
			[]interface{}{"X-Test-1", []string{"foo"}},
		},
		{
			http.Header{"X-Test-1": []string{"foo"}, "X-Test-2": []string{"foo", "bar"}, "X-Test-3": []string{}},
			[]string{"X-Test-1"},
			[]interface{}{"X-Test-1", []string{"foo"}},
		},
		{
			http.Header{"X-Test-1": []string{"foo"}, "X-Test-2": []string{"foo", "bar"}, "X-Test-3": []string{}},
			[]string{"X-Test-3", "X-Test-1"},
			[]interface{}{"X-Test-1", []string{"foo"}},
		},
		{
			http.Header{"X-Test-1": []string{"foo"}, "X-Test-2": []string{"foo", "bar"}, "X-Test-3": []string{}},
			[]string{"x-TeST-3", "X-tESt-1"},
			[]interface{}{"X-Test-1", []string{"foo"}},
		},
		{
			http.Header{"X-Test-1": []string{"foo"}, "X-Test-2": []string{"foo", "bar"}, "X-Test-3": []string{}},
			[]string{"X-Test-3", "X-Test-1", "X-Test-2"},
			[]interface{}{"X-Test-1", []string{"foo"}, "X-Test-2", []string{"foo", "bar"}},
		},
		{
			http.Header{"X-Test-1": []string{"foo"}, "X-Test-2": []string{"foo", "bar"}, "X-Test-3": []string{}},
			[]string{"X-TEST-3", "x-TEsT-1", "x-TesT-2"},
			[]interface{}{"X-Test-1", []string{"foo"}, "X-Test-2", []string{"foo", "bar"}},
		},
	}

	for i, record := range testData {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			t.Logf("%#v", record)
			testHeaders(t, record.originalHeader, record.headersToCopy, record.expectedResponse)
		})
	}
}

func testReturnHeadersWithPrefix(t *testing.T, request *http.Request, headerPrefixToCopy []string, expectedKV []interface{}) {
	var (
		require = require.New(t)
		rf      = RequestHeadersWithPrefix(headerPrefixToCopy...)
	)

	require.NotNil(rf)
	kv := rf(request)

	expectedkvMap := make(map[int]interface{})
	for i, v := range expectedKV {
		expectedkvMap[i] = v
	}

	kvMap := make(map[int]interface{})
	for i, v := range kv {
		kvMap[i] = v
	}

	// for i, v := range expectedKV {
	if ok := reflect.DeepEqual(expectedKV, kv); !ok {
		t.Errorf("Expecting: %v\n but got: %v\n", spew.Sdump(expectedkvMap), spew.Sdump(kvMap))
	}
}

func TestReturnHeadersWithPrefix(t *testing.T) {
	testData := []struct {
		request    *http.Request
		prefixs    []string
		expectedKV []interface{}
	}{
		{
			nil,
			nil,
			[]interface{}{},
		},<commit_msg>bookkeeping: request response bug<commit_after>package bookkeeping

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"reflect"
	"strconv"
	"strings"
	"testing"

	"github.com/davecgh/go-spew/spew"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func testHeaders(t *testing.T, originalHeader http.Header, headersToCopy []string, expectedKeyValues []interface{}) {
	var (
		assert  = assert.New(t)
		require = require.New(t)

		request = &http.Request{
			Header: originalHeader,
		}

		rf = RequestHeaders(headersToCopy...)
	)

	require.NotNil(rf)
	returnedKeyValuePair := rf(request)
	assert.Equal(expectedKeyValues, returnedKeyValuePair)
}

func TestBookkeepingHeaders(t *testing.T) {
	testData := []struct {
		originalHeader   http.Header
		headersToCopy    []string
		expectedResponse []interface{}
	}{
		{
			http.Header{"X-Test-1": []string{"foo"}, "X-Test-2": []string{"foo", "bar"}, "X-Test-3": []string{}},
			nil,
			[]interface{}{},
		},
		{
			http.Header{"X-Test-1": []string{"foo"}, "X-Test-2": []string{"foo", "bar"}, "X-Test-3": []string{}},
			[]string{"X-Does-Not-Exist"},
			[]interface{}{},
		},
		{
			http.Header{"X-Test-1": []string{"foo"}, "X-Test-2": []string{"foo", "bar"}, "X-Test-3": []string{}},
			[]string{"X-Does-Not-Exist", "X-Test-1"},
			[]interface{}{"X-Test-1", []string{"foo"}},
		},
		{
			http.Header{"X-Test-1": []string{"foo"}, "X-Test-2": []string{"foo", "bar"}, "X-Test-3": []string{}},
			[]string{"X-Does-Not-Exist", "x-test-1"},
			[]interface{}{"X-Test-1", []string{"foo"}},
		},
		{
			http.Header{"X-Test-1": []string{"foo"}, "X-Test-2": []string{"foo", "bar"}, "X-Test-3": []string{}},
			[]string{"X-Test-1"},
			[]interface{}{"X-Test-1", []string{"foo"}},
		},
		{
			http.Header{"X-Test-1": []string{"foo"}, "X-Test-2": []string{"foo", "bar"}, "X-Test-3": []string{}},
			[]string{"X-Test-3", "X-Test-1"},
			[]interface{}{"X-Test-1", []string{"foo"}},
		},
		{
			http.Header{"X-Test-1": []string{"foo"}, "X-Test-2": []string{"foo", "bar"}, "X-Test-3": []string{}},
			[]string{"x-TeST-3", "X-tESt-1"},
			[]interface{}{"X-Test-1", []string{"foo"}},
		},
		{
			http.Header{"X-Test-1": []string{"foo"}, "X-Test-2": []string{"foo", "bar"}, "X-Test-3": []string{}},
			[]string{"X-Test-3", "X-Test-1", "X-Test-2"},
			[]interface{}{"X-Test-1", []string{"foo"}, "X-Test-2", []string{"foo", "bar"}},
		},
		{
			http.Header{"X-Test-1": []string{"foo"}, "X-Test-2": []string{"foo", "bar"}, "X-Test-3": []string{}},
			[]string{"X-TEST-3", "x-TEsT-1", "x-TesT-2"},
			[]interface{}{"X-Test-1", []string{"foo"}, "X-Test-2", []string{"foo", "bar"}},
		},
	}

	for i, record := range testData {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			t.Logf("%#v", record)
			testHeaders(t, record.originalHeader, record.headersToCopy, record.expectedResponse)
		})
	}
}

func testReturnHeadersWithPrefix(t *testing.T, request *http.Request, headerPrefixToCopy []string, expectedKV []interface{}) {
	var (
		require = require.New(t)
		rf      = RequestHeadersWithPrefix(headerPrefixToCopy...)
	)

	require.NotNil(rf)
	kv := rf(request)

	f := func(i []interface{}, c chan<- map[string]interface{}) {
		m := make(map[string]interface{})
		for _, v := range i {
			m["test"] = v
		}

		c <- m
	}

	c1, c2 := make(chan map[string]interface{}), make(chan map[string]interface{})
	go f(kv, c1)
	go f(expectedKV, c2)

	kvMap, expectedkvMap := <-c1, <-c2

	if ok := reflect.DeepEqual(expectedKV, kv); !ok {
		t.Errorf("\nExpecting: %v\n but got: %v\n", spew.Sdump(expectedkvMap), spew.Sdump(kvMap))
	}
}

func TestReturnHeadersWithPrefix(t *testing.T) {
	testData := []struct {
		request    *http.Request
		prefixs    []string
		expectedKV []interface{}
	}{
		{
			nil,
			nil,
			[]interface{}{},
		},
{'fraction_non_alphanumeric': 0.26229287350127983, 'fraction_numerical': 0.014010507880910683, 'mean_word_length': 4.144837144837145, 'pattern_counts': {'":': 54, '<': 7, '<?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': '5545493', 'n_tokens_mistral': 3176, 'n_tokens_neox': 2942, 'n_words': 476}