<commit_before>package dou

import (
	"encoding/json"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"reflect"
	"strings"
	"testing"
)

type recorder struct {
	calledTime int
}

func newRecorder() *recorder {
	return &recorder{calledTime: 0}
}

type testAPI struct {
	beforeDispatchCalled bool
	afterDispatchCalled  bool
	recoverCalled        bool
}

func (p *testAPI) BeforeDispatch(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, *http.Request) {
	p.beforeDispatchCalled = true
	return w, r
}

func (p *testAPI) AfterDispatch(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, *http.Request) {
	p.afterDispatchCalled = true
	return w, r
}

func (p *testAPI) OnPanic(w http.ResponseWriter, r *http.Request) {
	p.recoverCalled = true
}

// Enable to stub
var testAPIMarshal = func(v interface{}) ([]byte, error) {
	return nil, nil
}

func (p *testAPI) Marshal(v interface{}) ([]byte, error) {
	return testAPIMarshal(v)
}

// Enable to stub
var testAPIUnmarshal = func(data []byte, v interface{}) error {
	return nil
}

func (p *testAPI) Unmarshal(data []byte, v interface{}) error {
	return testAPIUnmarshal(data, v)
}

func (p *testAPI) APIStatus(w http.ResponseWriter, code int) {
}

func TestCallBeforeDispatchAndAfterDispatch(t *testing.T) {
	request, _ := http.NewRequest("GET", "/", nil)
	response := httptest.NewRecorder()

	ta := &testAPI{}

	Register("testapi", ta)
	defer delete(plugins, "testapi")

	a, err := NewAPI("testapi")
	a.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})

	if err != nil {
		panic(err)
	}
<commit_msg>Write test for Deregister<commit_after>package dou

import (
	"encoding/json"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"reflect"
	"strings"
	"testing"
)

type recorder struct {
	calledTime int
}

func newRecorder() *recorder {
	return &recorder{calledTime: 0}
}

type testAPI struct {
	beforeDispatchCalled bool
	afterDispatchCalled  bool
	recoverCalled        bool
}

func (p *testAPI) BeforeDispatch(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, *http.Request) {
	p.beforeDispatchCalled = true
	return w, r
}

func (p *testAPI) AfterDispatch(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, *http.Request) {
	p.afterDispatchCalled = true
	return w, r
}

func (p *testAPI) OnPanic(w http.ResponseWriter, r *http.Request) {
	p.recoverCalled = true
}

// Enable to stub
var testAPIMarshal = func(v interface{}) ([]byte, error) {
	return nil, nil
}

func (p *testAPI) Marshal(v interface{}) ([]byte, error) {
	return testAPIMarshal(v)
}

// Enable to stub
var testAPIUnmarshal = func(data []byte, v interface{}) error {
	return nil
}

func (p *testAPI) Unmarshal(data []byte, v interface{}) error {
	return testAPIUnmarshal(data, v)
}

func (p *testAPI) APIStatus(w http.ResponseWriter, code int) {
}

// - Test

func TestNewAPIWithUnregisteredPluginName(t *testing.T) {
	api, err := NewAPI("unknown")

	if api != nil {
		t.Error("If NewAPI called with unknown plugin name, api should be nil")
	}

	if err == nil {
		t.Error("If NewAPI called with unknown plugin name, api should not be nil")
	}
}

func TestDeregister(t *testing.T) {
	Register("testapi", &testAPI{})
	Deregister("testapi")

	if _, ok := plugins["testapi"]; ok != false {
		t.Error("Deregister should delete registered plugin")
	}
}

func TestCallBeforeDispatchAndAfterDispatch(t *testing.T) {
	request, _ := http.NewRequest("GET", "/", nil)
	response := httptest.NewRecorder()

	ta := &testAPI{}

	Register("testapi", ta)
	defer delete(plugins, "testapi")

	a, err := NewAPI("testapi")
	a.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})

	if err != nil {
		panic(err)
	}

{'fraction_non_alphanumeric': 0.13906207644348062, 'fraction_numerical': 0.0005421523448088912, 'mean_word_length': 4.633587786259542, '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': '5577068', 'n_tokens_mistral': 1320, 'n_tokens_neox': 1298, 'n_words': 345}