<commit_before>package hhth_test

import (
	"bytes"
	"fmt"
	"net/http"
	"net/url"
	"testing"

	"github.com/kyokomi/hhth"
)

func TestHogeHandler(t *testing.T) {
	hhtHelper := hhth.New(http.DefaultServeMux)

	resp := hhtHelper.Get("/hoge",
		hhth.TestCaseStatusCode(http.StatusOK),
		hhth.TestCaseContentType("text/plain; charset=utf-8"),
		hhth.TestCaseContentLength(len("hogehoge")),
	)
	if resp.Error() != nil {
		t.Errorf("error %s", resp.Error())
	}
	fmt.Println(resp.String())
}

func TestError(t *testing.T) {
	hhtHelper := hhth.New(http.DefaultServeMux)

	respError1 := hhtHelper.Get("/hoge",
		hhth.TestCaseStatusCode(http.StatusFound), // error
		hhth.TestCaseContentType("text/plain; charset=utf-8"),
		hhth.TestCaseContentLength(len("hogehoge")),
	)
	if respError1.Error() == nil {
		t.Error("error not error")
	}

	respError2 := hhtHelper.Get("/hoge",
		hhth.TestCaseStatusCode(http.StatusOK),
		hhth.TestCaseContentType("application/json; charset=UTF-8"), // error
		hhth.TestCaseContentLength(len("hogehoge")),
	)
	if respError2.Error() == nil {
		t.Error("error not error")
	}

	respError3 := hhtHelper.Get("/hoge",
		hhth.TestCaseStatusCode(http.StatusOK),
		hhth.TestCaseContentType("text/plain; charset=utf-8"),
		hhth.TestCaseContentLength(len("hogehogeaaaaa")), // error
	)
	if respError3.Error() == nil {
		t.Error("error not error")
	}
}

func TestHogeJSONHandler(t *testing.T) {
	hhtHelper := hhth.New(http.DefaultServeMux)

	resp := hhtHelper.Get("/hoge.json",
		hhth.TestCaseStatusCode(http.StatusOK),
		hhth.TestCaseContentType("application/json; charset=UTF-8"),
	)

	if resp.Error() != nil {
		t.Errorf("error %s", resp.Error())
	}

	var respJson map[string]interface{}
	if err := resp.JSON(&respJson); err != nil {
		t.Errorf("error %s", err)
	}

	fmt.Println(resp)
}

func TestHogeHeaderHandler(t *testing.T) {
	hhtHelper := hhth.New(http.DefaultServeMux)

	hhtHelper.SetHeader("X-App-Hoge", "hoge-header")

	resp := hhtHelper.Get("/header",
		hhth.TestCaseStatusCode(http.StatusOK),
		hhth.TestCaseContentType("text/plain; charset=utf-8"),
	)
	if resp.Error() != nil {
		t.Errorf("error %s", resp.Error())
	}
	fmt.Println(resp.String())
}

func TestPostHandler(t *testing.T) {
	hhtHelper := hhth.New(http.DefaultServeMux)

	formData := url.Values{}
	formData.Set("name", "hoge")
	formData.Set("age", "19")

	resp := hhtHelper.Post("/post", "application/x-www-form-urlencoded",
		bytes.NewBufferString(formData.Encode()),
		hhth.TestCaseStatusCode(http.StatusOK),
		hhth.TestCaseContentType("text/plain; charset=utf-8"),
	)
	if resp.Error() != nil {
		t.Errorf("error %s", resp.Error())
	}
	fmt.Println(resp.String())
}

func TestGetFormHandler(t *testing.T) {
	hhtHelper := hhth.New(http.DefaultServeMux)

	hhtHelper.SetForm("name", "hoge")

	resp := hhtHelper.Get("/get-form",
		hhth.TestCaseStatusCode(http.StatusOK),
		hhth.TestCaseContentType("text/plain; charset=utf-8"),
	)
	if resp.Error() != nil {
		t.Errorf("error %s", resp.Error())
	}
	fmt.Println(resp.String())
}

func TestNewRequestError(t *testing.T) {
	hhtHelper := hhth.New(http.DefaultServeMux)

	resp := hhtHelper.Get("%||~=&&%%%&&T''=0I)=)((&'") // error
	if resp.Error() == nil {
		t.Error("error not nil")
	}

	var parseJSON map[string]string
	if err := resp.JSON(&parseJSON); err == nil { // error
		t.Error("error not nil")
	}
	if resp.String() != "" { // error
		t.Error("error not nil")
	}

	if res, err := resp.Result(); res != nil || err == nil {
		t.Error("error not nil")
	}
}

func TestJSONParseError(t *testing.T) {
	hhtHelper := hhth.New(http.DefaultServeMux)

	resp := hhtHelper.Get("error.json")
	if resp.Error() != nil {
		t.Error("error %s", resp.Error())
	}

	var parseJSON map[string]string
	if err := resp.JSON(&parseJSON); err == nil { // error
		t.Error("error not nil")
	}
}

func init() {
	http.HandleFunc("/hoge", hogeHandler)
	http.HandleFunc("/hoge.json", hogeJSONHandler)
	http.HandleFunc("/error.json", errorJSONHandler)
	http.HandleFunc("/header", headerHandler)
	http.HandleFunc("/post", postHandler)<commit_msg>fix govet<commit_after>package hhth_test

import (
	"bytes"
	"fmt"
	"net/http"
	"net/url"
	"testing"

	"github.com/kyokomi/hhth"
)

func TestHogeHandler(t *testing.T) {
	hhtHelper := hhth.New(http.DefaultServeMux)

	resp := hhtHelper.Get("/hoge",
		hhth.TestCaseStatusCode(http.StatusOK),
		hhth.TestCaseContentType("text/plain; charset=utf-8"),
		hhth.TestCaseContentLength(len("hogehoge")),
	)
	if resp.Error() != nil {
		t.Errorf("error %s", resp.Error())
	}
	fmt.Println(resp.String())
}

func TestError(t *testing.T) {
	hhtHelper := hhth.New(http.DefaultServeMux)

	respError1 := hhtHelper.Get("/hoge",
		hhth.TestCaseStatusCode(http.StatusFound), // error
		hhth.TestCaseContentType("text/plain; charset=utf-8"),
		hhth.TestCaseContentLength(len("hogehoge")),
	)
	if respError1.Error() == nil {
		t.Error("error not error")
	}

	respError2 := hhtHelper.Get("/hoge",
		hhth.TestCaseStatusCode(http.StatusOK),
		hhth.TestCaseContentType("application/json; charset=UTF-8"), // error
		hhth.TestCaseContentLength(len("hogehoge")),
	)
	if respError2.Error() == nil {
		t.Error("error not error")
	}

	respError3 := hhtHelper.Get("/hoge",
		hhth.TestCaseStatusCode(http.StatusOK),
		hhth.TestCaseContentType("text/plain; charset=utf-8"),
		hhth.TestCaseContentLength(len("hogehogeaaaaa")), // error
	)
	if respError3.Error() == nil {
		t.Error("error not error")
	}
}

func TestHogeJSONHandler(t *testing.T) {
	hhtHelper := hhth.New(http.DefaultServeMux)

	resp := hhtHelper.Get("/hoge.json",
		hhth.TestCaseStatusCode(http.StatusOK),
		hhth.TestCaseContentType("application/json; charset=UTF-8"),
	)

	if resp.Error() != nil {
		t.Errorf("error %s", resp.Error())
	}

	var respJson map[string]interface{}
	if err := resp.JSON(&respJson); err != nil {
		t.Errorf("error %s", err)
	}

	fmt.Println(resp)
}

func TestHogeHeaderHandler(t *testing.T) {
	hhtHelper := hhth.New(http.DefaultServeMux)

	hhtHelper.SetHeader("X-App-Hoge", "hoge-header")

	resp := hhtHelper.Get("/header",
		hhth.TestCaseStatusCode(http.StatusOK),
		hhth.TestCaseContentType("text/plain; charset=utf-8"),
	)
	if resp.Error() != nil {
		t.Errorf("error %s", resp.Error())
	}
	fmt.Println(resp.String())
}

func TestPostHandler(t *testing.T) {
	hhtHelper := hhth.New(http.DefaultServeMux)

	formData := url.Values{}
	formData.Set("name", "hoge")
	formData.Set("age", "19")

	resp := hhtHelper.Post("/post", "application/x-www-form-urlencoded",
		bytes.NewBufferString(formData.Encode()),
		hhth.TestCaseStatusCode(http.StatusOK),
		hhth.TestCaseContentType("text/plain; charset=utf-8"),
	)
	if resp.Error() != nil {
		t.Errorf("error %s", resp.Error())
	}
	fmt.Println(resp.String())
}

func TestGetFormHandler(t *testing.T) {
	hhtHelper := hhth.New(http.DefaultServeMux)

	hhtHelper.SetForm("name", "hoge")

	resp := hhtHelper.Get("/get-form",
		hhth.TestCaseStatusCode(http.StatusOK),
		hhth.TestCaseContentType("text/plain; charset=utf-8"),
	)
	if resp.Error() != nil {
		t.Errorf("error %s", resp.Error())
	}
	fmt.Println(resp.String())
}

func TestNewRequestError(t *testing.T) {
	hhtHelper := hhth.New(http.DefaultServeMux)

	resp := hhtHelper.Get("%||~=&&%%%&&T''=0I)=)((&'") // error
	if resp.Error() == nil {
		t.Error("error not nil")
	}

	var parseJSON map[string]string
	if err := resp.JSON(&parseJSON); err == nil { // error
		t.Error("error not nil")
	}
	if resp.String() != "" { // error
		t.Error("error not nil")
	}

	if res, err := resp.Result(); res != nil || err == nil {
		t.Error("error not nil")
	}
}

func TestJSONParseError(t *testing.T) {
	hhtHelper := hhth.New(http.DefaultServeMux)

	resp := hhtHelper.Get("error.json")
	if resp.Error() != nil {
		t.Errorf("error %s", resp.Error())
	}

	var parseJSON map[string]string
	if err := resp.JSON(&parseJSON); err == nil { // error
		t.Error("error not nil")
	}
}

func init() {
	http.HandleFunc("/hoge", hogeHandler)
	http.HandleFunc("/hoge.json", hogeJSONHandler)
	http.HandleFunc("/error.json", errorJSONHandler)
	http.HandleFunc("/header", headerHandler)
	http.HandleFunc("/post", postHandler)
{'fraction_non_alphanumeric': 0.18750773036487323, 'fraction_numerical': 0.0042053184910327765, 'mean_word_length': 4.71045197740113, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3161038', 'n_tokens_mistral': 3201, 'n_tokens_neox': 3057, 'n_words': 484}