Exemple #1
0
func TestCustomTestCaseFunc(t *testing.T) {
	hhtHelper := hhth.New(http.DefaultServeMux)
	hhtHelper.SetTestCaseFunc(func(resp hhth.Response) error {
		r, _ := resp.Result()
		if r.Header().Get("X-Hoge-Version") != "1.0.0" {
			return fmt.Errorf("error header version %s", r.Header().Get("X-Hoge-Version"))
		}
		return nil
	})
	hhtHelper.AddTestCaseFunc(func(resp hhth.Response) error {
		r, _ := resp.Result()
		if r.Header().Get("Content-Type") != "text/plain; charset=utf-8" {
			return fmt.Errorf("error header Content-Type %s", r.Header().Get("Content-Type"))
		}
		return nil
	})

	resp := hhtHelper.Get("/hoge", func(resp hhth.Response) error {
		r, _ := resp.Result()
		if r.Code != http.StatusOK {
			return fmt.Errorf("error http code %d", r.Code)
		}
		return nil
	})
	if resp.Error() != nil {
		t.Errorf("error %s", resp.Error())
	}
	fmt.Println(resp.String())
}
Exemple #2
0
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())
}
Exemple #3
0
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())
}
Exemple #4
0
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")
	}
}
Exemple #5
0
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())
	}
}
Exemple #6
0
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())
}
Exemple #7
0
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())
}
Exemple #8
0
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())
	}
}
Exemple #9
0
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())
}
Exemple #10
0
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())
	}
}
Exemple #11
0
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())
}
Exemple #12
0
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())
}
Exemple #13
0
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())
}
Exemple #14
0
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)
}
Exemple #15
0
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")
	}
}
Exemple #16
0
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"])
	}
}