func TestDeleteHogeHandler(t *testing.T) { hhtHelper := hhth.New(http.DefaultServeMux) hhtHelper.SetTestCase( hhth.TestCaseStatusCode(http.StatusNoContent), ) resp := hhtHelper.Delete("/hoge") if resp.Error() != nil { t.Errorf("error %s", resp.Error()) } fmt.Println(resp.String()) }
func TestCustomTestCase(t *testing.T) { hhtHelper := hhth.New(http.DefaultServeMux) hhtHelper.SetTestCase( hhth.TestCaseStatusCode(http.StatusOK), ) hhtHelper.AddTestCase(hhth.TestCaseContentType("text/plain; charset=utf-8")) resp := hhtHelper.Get("/hoge") if resp.Error() != nil { t.Errorf("error %s", resp.Error()) } fmt.Println(resp.String()) }
func TestErrorStatusCode(t *testing.T) { hhtHelper := hhth.New(http.DefaultServeMux) hhtHelper.SetTestCase( hhth.TestCaseStatusCode(http.StatusFound), // error hhth.TestCaseContentType("text/plain; charset=utf-8"), hhth.TestCaseContentLength(len("hogehoge")), ) respError1 := hhtHelper.Get("/hoge") if respError1.Error() == nil { t.Error("error not error") } else { t.Logf("OK %s", respError1.Error()) } }
func TestHeadHogeHandler(t *testing.T) { hhtHelper := hhth.New(http.DefaultServeMux) hhtHelper.SetTestCase( hhth.TestCaseStatusCode(http.StatusOK), hhth.TestCaseContentType("text/plain; charset=utf-8"), hhth.TestCaseContentLength(len("hogehoge")), ) resp := hhtHelper.Head("/hoge") if resp.Error() != nil { t.Errorf("error %s", resp.Error()) } fmt.Println(resp.String()) }
func TestHogeHeaderHandler(t *testing.T) { hhtHelper := hhth.New(http.DefaultServeMux) hhtHelper.SetTestCase( hhth.TestCaseStatusCode(http.StatusOK), hhth.TestCaseContentType("text/plain; charset=utf-8"), ) hhtHelper.SetHeader("X-App-Hoge", "hoge-header") resp := hhtHelper.Get("/header") if resp.Error() != nil { t.Errorf("error %s", resp.Error()) } fmt.Println(resp.String()) }
func TestErrorContentType(t *testing.T) { hhtHelper := hhth.New(http.DefaultServeMux) hhtHelper.SetTestCase( hhth.TestCaseStatusCode(http.StatusOK), hhth.TestCaseContentType("application/json; charset=UTF-8"), // error hhth.TestCaseContentLength(len("hogehoge")), ) respError2 := hhtHelper.Get("/hoge") if respError2.Error() == nil { t.Error("error not error") } else { t.Logf("OK %s", respError2.Error()) } }
func TestGetFormHandler(t *testing.T) { hhtHelper := hhth.New(http.DefaultServeMux) hhtHelper.SetTestCase( hhth.TestCaseStatusCode(http.StatusOK), hhth.TestCaseContentType("text/plain; charset=utf-8"), ) hhtHelper.SetForm("name", "hoge") resp := hhtHelper.Get("/get-form") if resp.Error() != nil { t.Errorf("error %s", resp.Error()) } fmt.Println(resp.String()) }
func TestHogeHandler(t *testing.T) { hhtHelper := hhth.New(http.DefaultServeMux) hhtHelper.SetTestCase( hhth.TestCaseStatusCode(http.StatusOK), hhth.TestCaseContentType("text/plain; charset=utf-8"), ) resp := hhtHelper.Get("/hoge") if resp.Error() != nil { t.Errorf("error %s", resp.Error()) } if resp.String() != "hoge" { t.Errorf("error response body hoge != %s", resp.String()) } }
func TestPutHogeHandler(t *testing.T) { hhtHelper := hhth.New(http.DefaultServeMux) hhtHelper.SetTestCase( hhth.TestCaseStatusCode(http.StatusCreated), ) formData := url.Values{} formData.Set("message", "hello") resp := hhtHelper.Put("/hoge", "application/x-www-form-urlencoded", bytes.NewBufferString(formData.Encode()), ) if resp.Error() != nil { t.Errorf("error %s", resp.Error()) } fmt.Println(resp.String()) }
func TestOptionsHogeHandler(t *testing.T) { hhtHelper := hhth.New(http.DefaultServeMux) hhtHelper.SetTestCase( hhth.TestCaseStatusCode(http.StatusNoContent), hhth.HandlerTestCaseFunc(func(resp hhth.Response) error { r, _ := resp.Result() if r.Header().Get("Allow") != "GET,HEAD,PUT,POST,DELETE" { return fmt.Errorf("allow header error %s", r.Header().Get("Allow")) } return nil }), ) resp := hhtHelper.Options("/hoge") if resp.Error() != nil { t.Errorf("error %s", resp.Error()) } fmt.Println(resp.String()) }
func TestPostHandler(t *testing.T) { hhtHelper := hhth.New(http.DefaultServeMux) hhtHelper.SetTestCase( hhth.TestCaseStatusCode(http.StatusOK), hhth.TestCaseContentType("text/plain; charset=utf-8"), ) formData := url.Values{} formData.Set("name", "hoge") formData.Set("age", "19") resp := hhtHelper.Post("/post", "application/x-www-form-urlencoded", bytes.NewBufferString(formData.Encode()), ) if resp.Error() != nil { t.Errorf("error %s", resp.Error()) } fmt.Println(resp.String()) }
func TestHogeJSONHandler(t *testing.T) { hhtHelper := hhth.New(http.DefaultServeMux) hhtHelper.SetTestCase( hhth.TestCaseStatusCode(http.StatusOK), hhth.TestCaseContentType("application/json; charset=UTF-8"), ) resp := hhtHelper.Get("/hoge.json") 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 TestJSONParse(t *testing.T) { hhtHelper := hhth.New(http.DefaultServeMux) hhtHelper.SetTestCase( hhth.TestCaseStatusCode(http.StatusOK), hhth.TestCaseContentType("application/json; charset=UTF-8"), ) var resp map[string]interface{} if err := hhtHelper.Get("/hoge.json").JSON(&resp); err != nil { t.Errorf("error %s", err) } if resp["name"].(string) != "hoge" { t.Errorf("error json response name != %s", resp["name"]) } if resp["age"].(float64) != 20 { t.Errorf("error json response age != %s", resp["age"]) } }