Upgrade and add new testing functions

This commit is contained in:
Zeni Kim 2026-09-13 00:09:50 -05:00
parent d12679df65
commit 9330ed8e74
13 changed files with 1867 additions and 130 deletions

View file

@ -157,7 +157,7 @@ func TestGetPathParams(t *testing.T) {
}
a := New()
h := a.makeHTTPRouterHandlerFunc(
Handler(func(c *Context) *Response {
Controller(func(c *Context) *Response {
rsp := fmt.Sprintf("param1: %v | param2: %v", c.GetPathParam("param1"), c.GetPathParam("param2"))
return c.Response.Text(rsp)
}), nil)
@ -178,11 +178,13 @@ func TestGetRequestParams(t *testing.T) {
app.SetBasePath(pwd)
hr := httprouter.New()
gcr := NewRouter()
gcr.Post("/pt", Handler(func(c *Context) *Response {
gcr.Post("/pt", Controller(func(c *Context) *Response {
fmt.Fprintln(c.Response.HttpResponseWriter, c.GetRequestParam("param"))
return nil
}))
gcr.Get("/gt", Handler(func(c *Context) *Response {
gcr.Get("/gt", Controller(func(c *Context) *Response {
fmt.Fprintln(c.Response.HttpResponseWriter, c.GetRequestParam("param"))
return nil
}))
@ -218,11 +220,13 @@ func TestRequestParamsExists(t *testing.T) {
app := New()
hr := httprouter.New()
gcr := NewRouter()
gcr.Post("/pt", Handler(func(c *Context) *Response {
gcr.Post("/pt", Controller(func(c *Context) *Response {
fmt.Fprintln(c.Response.HttpResponseWriter, c.RequestParamExists("param"))
return nil
}))
gcr.Get("/gt", Handler(func(c *Context) *Response {
gcr.Get("/gt", Controller(func(c *Context) *Response {
fmt.Fprintln(c.Response.HttpResponseWriter, c.RequestParamExists("param"))
return nil
}))
@ -259,11 +263,13 @@ func TestGetHeader(t *testing.T) {
app := New()
hr := httprouter.New()
gcr := NewRouter()
gcr.Post("/pt", Handler(func(c *Context) *Response {
gcr.Post("/pt", Controller(func(c *Context) *Response {
fmt.Fprintln(c.Response.HttpResponseWriter, c.GetHeader("headerkey"))
return nil
}))
gcr.Get("/gt", Handler(func(c *Context) *Response {
gcr.Get("/gt", Controller(func(c *Context) *Response {
fmt.Fprintln(c.Response.HttpResponseWriter, c.GetHeader("headerkey"))
return nil
}))
@ -309,8 +315,13 @@ func TestGetUploadedFile(t *testing.T) {
app := New()
hr := httprouter.New()
gcr := NewRouter()
gcr.Post("/pt", Handler(func(c *Context) *Response {
uploadedFile := c.GetUploadedFile("myfile")
gcr.Post("/pt", Controller(func(c *Context) *Response {
uploadedFile, err := c.GetUploadedFile("myfile")
if err != nil {
fmt.Fprintln(c.Response.HttpResponseWriter, "error: "+err.Error())
return nil
}
rs := fmt.Sprintf("file name: %v | size: %v", uploadedFile.Name, uploadedFile.Size)
fmt.Fprintln(c.Response.HttpResponseWriter, rs)
return nil
@ -534,6 +545,89 @@ func TestGetBaseDirPath(t *testing.T) {
}
}
func TestGetUserAgent(t *testing.T) {
r := httptest.NewRequest(GET, LOCALHOST, nil)
r.Header.Set("User-Agent", "goffee-test-agent")
c := makeCTX(t)
c.Request.httpRequest = r
if got := c.GetUserAgent(); got != "goffee-test-agent" {
t.Errorf("expected 'goffee-test-agent', got %q", got)
}
}
func TestGetRequesBodyMap(t *testing.T) {
r := httptest.NewRequest(POST, LOCALHOST, strings.NewReader(`{"name":"alice","age":"30"}`))
c := makeCTX(t)
c.Request.httpRequest = r
m := c.GetRequesBodyMap()
if m["name"] != "alice" {
t.Errorf("expected name 'alice', got %v", m["name"])
}
}
func TestGetRequesBodyStruct(t *testing.T) {
type payload struct {
Name string `json:"name"`
}
r := httptest.NewRequest(POST, LOCALHOST, strings.NewReader(`{"name":"bob"}`))
c := makeCTX(t)
c.Request.httpRequest = r
var p payload
if err := c.GetRequesBodyStruct(&p); err != nil {
t.Fatalf("failed binding body struct: %v", err)
}
if p.Name != "bob" {
t.Errorf("expected name 'bob', got %q", p.Name)
}
}
func TestGetRequesBodyStructNonPointer(t *testing.T) {
type payload struct {
Name string `json:"name"`
}
r := httptest.NewRequest(POST, LOCALHOST, strings.NewReader(`{"name":"bob"}`))
c := makeCTX(t)
c.Request.httpRequest = r
var p payload
err := c.GetRequesBodyStruct(p)
if err == nil {
t.Errorf("expected error when dest is not a pointer")
}
}
func TestMapToJson(t *testing.T) {
c := makeCTX(t)
got := c.MapToJson(map[string]interface{}{"a": 1})
if got != `{"a":1}` {
t.Errorf("expected {\"a\":1}, got %q", got)
}
}
func TestMapToJsonPanicsOnNonMap(t *testing.T) {
c := makeCTX(t)
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic for non-map input")
}
}()
c.MapToJson("not a map")
}
func TestGetRequesForm(t *testing.T) {
r := httptest.NewRequest(http.MethodPost, LOCALHOST, strings.NewReader("param=value"))
r.Header.Set(CONTENT_TYPE, "application/x-www-form-urlencoded")
c := makeCTX(t)
c.Request.httpRequest = r
got := c.GetRequesForm("param")
vals, ok := got.([]string)
if !ok {
t.Fatalf("expected []string form values, got %T", got)
}
if len(vals) != 1 || vals[0] != "value" {
t.Errorf("expected form value 'value', got %v", vals)
}
}
func makeCTXLogTestCTX(t *testing.T, w http.ResponseWriter, r *http.Request, tmpFilePath string) *Context {
t.Helper()
return &Context{