<commit_before>package server

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


type domainsResult struct {
	Result []string      `json:"result"`
	Error  error       `json:"error"`
}


func request(s *Server, t *testing.T, method string, domain string, body string) *httptest.ResponseRecorder {
	reqBody := strings.NewReader(body)
	req, err := http.NewRequest(method, "http://counters.io/" + domain, reqBody)
	if err != nil {
		t.Fatalf("%s", err)
	}
	respw := httptest.NewRecorder()
	s.ServeHTTP(respw, req)
	return respw
}

func unmarschal(resp *httptest.ResponseRecorder) domainsResult {
	body, _ := ioutil.ReadAll(resp.Body)
	var r domainsResult
	json.Unmarshal(body, &r)
	return r
}


func TestDomainsInitiallyEmpty(t *testing.T) {
	s := New()
	resp := request(s, t, "GET", "", "{}")
	if resp.Code != 200 {
		t.Fatalf("Invalid Response Code %d - %s", resp.Code, resp.Body.String())	
		return
	}
	result := unmarschal(resp)
	if len(result.Result) != 0 {
		t.Fatalf("Initial resultCount != 0. Got %s", result)	
	}
}

func TestBadRequest(t *testing.T) {
	s := New()
	var resp *httptest.ResponseRecorder
	resp = request(s, t, "GET", "", `{"invalid": "request"}`)
	if resp.Code != 400 {
		t.Fatalf("Invalid Response Code %d - Expected 400", resp.Code)	
		return
	}
	resp = request(s, t, "POST", "marvel", "{}")
	if resp.Code != 400 {
		t.Fatalf("Invalid Response Code %d - Expected 400", resp.Code)	
		return
	}
}

func TestCreateDomain(t *testing.T) {
	s := New()
	resp := request(s, t, "POST", "marvel", `{
		"domain": "marvel",
		"domainType": "mutable",
		"capacity": 100000,
		"values": []
	}`)

	if resp.Code != 200 {
		t.Fatalf("Invalid Response Code %d - %s", resp.Code, resp.Body.String())
		return
	}

	result := unmarschal(resp)
	if len(result.Result) != 1 {
		t.Fatalf("after add resultCount != 1. Got %s", len(result.Result))
	}
}
<commit_msg>cleanup<commit_after>package server

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


type domainsResult struct {
	Result []string    `json:"result"`
	Error  error       `json:"error"`
}


func request(s *Server, t *testing.T, method string, domain string, body string) *httptest.ResponseRecorder {
	reqBody := strings.NewReader(body)
	req, err := http.NewRequest(method, "http://counters.io/" + domain, reqBody)
	if err != nil {
		t.Fatalf("%s", err)
	}
	respw := httptest.NewRecorder()
	s.ServeHTTP(respw, req)
	return respw
}

func unmarschal(resp *httptest.ResponseRecorder) domainsResult {
	body, _ := ioutil.ReadAll(resp.Body)
	var r domainsResult
	json.Unmarshal(body, &r)
	return r
}


func TestDomainsInitiallyEmpty(t *testing.T) {
	s := New()
	resp := request(s, t, "GET", "", "{}")
	if resp.Code != 200 {
		t.Fatalf("Invalid Response Code %d - %s", resp.Code, resp.Body.String())	
		return
	}
	result := unmarschal(resp)
	if len(result.Result) != 0 {
		t.Fatalf("Initial resultCount != 0. Got %s", result)	
	}
}

func TestBadRequest(t *testing.T) {
	s := New()
	var resp *httptest.ResponseRecorder
	resp = request(s, t, "GET", "", `{"invalid": "request"}`)
	if resp.Code != 400 {
		t.Fatalf("Invalid Response Code %d - Expected 400", resp.Code)	
		return
	}
	resp = request(s, t, "POST", "marvel", "{}")
	if resp.Code != 400 {
		t.Fatalf("Invalid Response Code %d - Expected 400", resp.Code)	
		return
	}
}

func TestCreateDomain(t *testing.T) {
	s := New()
	resp := request(s, t, "POST", "marvel", `{
		"domain": "marvel",
		"domainType": "mutable",
		"capacity": 100000,
		"values": []
	}`)

	if resp.Code != 200 {
		t.Fatalf("Invalid Response Code %d - %s", resp.Code, resp.Body.String())
		return
	}

	result := unmarschal(resp)
	if len(result.Result) != 1 {
		t.Fatalf("after add resultCount != 1. Got %d", len(result.Result))
	}
}

{'fraction_non_alphanumeric': 0.1793792740662809, 'fraction_numerical': 0.014729089952656496, 'mean_word_length': 3.9070967741935485, 'pattern_counts': {'":': 10, '<': 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': '7620834', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1448, 'n_words': 348}