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

@ -50,7 +50,8 @@ func TestMakeHTTPHandlerFunc(t *testing.T) {
app.SetLogsDriver(&logger.LogFileDriver{
FilePath: filepath.Join(t.TempDir(), uuid.NewString()),
})
hdlr := Handler(func(c *Context) *Response {
app.Bootstrap()
hdlr := Controller(func(c *Context) *Response {
f, _ := os.Create(tmpFile)
f.WriteString("DFT2V56H")
c.Response.SetHeader("header-key", "header-val")
@ -76,7 +77,8 @@ func TestMakeHTTPHandlerFuncVerifyJson(t *testing.T) {
app.SetLogsDriver(&logger.LogFileDriver{
FilePath: filepath.Join(t.TempDir(), uuid.NewString()),
})
hdlr := Handler(func(c *Context) *Response {
app.Bootstrap()
hdlr := Controller(func(c *Context) *Response {
f, _ := os.Create(tmpFile)
f.WriteString("DFT2V56H")
c.Response.SetHeader("header-key", "header-val")
@ -130,18 +132,19 @@ func TestNotFoundHandler(t *testing.T) {
}
func TestUseMiddleware(t *testing.T) {
func TestUseHook(t *testing.T) {
app := createNewApp(t)
UseMiddleware(Middleware(func(c *Context) { c.GetLogger().Info("Testing!") }))
if len(app.middlewares.GetMiddlewares()) != 1 {
t.Errorf("failed testing use middleware")
app.Bootstrap()
UseHook(Hook(func(c *Context) { c.GetLogger().Info("Testing!") }))
if len(ResolveHooks().GetHooks()) != 1 {
t.Errorf("failed testing use hook")
}
}
func TestChainReset(t *testing.T) {
c := &chain{}
c.nodes = append(c.nodes, Middleware(func(c *Context) { c.GetLogger().Info("Testing1!") }))
c.nodes = append(c.nodes, Middleware(func(c *Context) { c.GetLogger().Info("Testing2!") }))
c.nodes = append(c.nodes, Hook(func(c *Context) { c.GetLogger().Info("Testing1!") }))
c.nodes = append(c.nodes, Hook(func(c *Context) { c.GetLogger().Info("Testing2!") }))
c.reset()
if len(c.nodes) != 0 {
@ -151,11 +154,11 @@ func TestChainReset(t *testing.T) {
func TestNext(t *testing.T) {
app := createNewApp(t)
app.t = 0
app.Bootstrap()
tfPath := filepath.Join(t.TempDir(), uuid.NewString())
var hs []interface{}
hs = append(hs, Middleware(func(c *Context) { c.Next() }))
hs = append(hs, Handler(func(c *Context) *Response {
hs = append(hs, Hook(func(c *Context) { c.Next() }))
hs = append(hs, Controller(func(c *Context) *Response {
f, _ := os.Create(tfPath)
f.WriteString("DFT2V56H")
return nil
@ -165,7 +168,7 @@ func TestNext(t *testing.T) {
app.chain.execute(makeCTX(t))
cnt, _ := os.ReadFile(tfPath)
if string(cnt) != "DFT2V56H" {
// t.Errorf("failed testing next")
t.Errorf("failed testing next")
}
}
@ -173,14 +176,14 @@ func TestChainGetByIndex(t *testing.T) {
c := &chain{}
tf := filepath.Join(t.TempDir(), uuid.NewString())
var hs []interface{}
hs = append(hs, Middleware(func(c *Context) { c.GetLogger().Info("testing!") }))
hs = append(hs, Middleware(func(c *Context) {
hs = append(hs, Hook(func(c *Context) { c.GetLogger().Info("testing!") }))
hs = append(hs, Hook(func(c *Context) {
f, _ := os.Create(tf)
f.WriteString("DFT2V56H")
}))
c.nodes = hs
pf := c.getByIndex(1)
f, ok := pf.(Middleware)
f, ok := pf.(Hook)
if ok {
f(makeCTX(t))
}
@ -192,10 +195,11 @@ func TestChainGetByIndex(t *testing.T) {
func TestPrepareChain(t *testing.T) {
app := createNewApp(t)
UseMiddleware(Middleware(func(c *Context) { c.GetLogger().Info("Testing!") }))
app.Bootstrap()
UseHook(Hook(func(c *Context) { c.GetLogger().Info("Testing!") }))
var hs []interface{}
hs = append(hs, Middleware(func(c *Context) { c.GetLogger().Info("testing1!") }))
hs = append(hs, Middleware(func(c *Context) { c.GetLogger().Info("testing2!") }))
hs = append(hs, Hook(func(c *Context) { c.GetLogger().Info("testing1!") }))
hs = append(hs, Hook(func(c *Context) { c.GetLogger().Info("testing2!") }))
app.prepareChain(hs)
if len(app.chain.nodes) != 3 {
t.Errorf("failed preparing chain")
@ -207,7 +211,7 @@ func TestChainExecute(t *testing.T) {
f1Path := filepath.Join(tmpDir, uuid.NewString())
c := &chain{}
c.nodes = []interface{}{
Handler(func(c *Context) *Response {
Controller(func(c *Context) *Response {
tf, _ := os.Create(f1Path)
defer tf.Close()
tf.WriteString("DFT2V56H")
@ -239,19 +243,23 @@ func makeCTX(t *testing.T) *Context {
}
}
func TestcombHndlers(t *testing.T) {
func TestCombHandlers(t *testing.T) {
app := createNewApp(t)
t1 := Handler(func(c *Context) *Response { c.GetLogger().Info("Testing1!"); return nil })
t2 := Middleware(func(c *Context) { c.GetLogger().Info("Testing2!") })
t1 := Controller(func(c *Context) *Response { c.GetLogger().Info("Testing1!"); return nil })
t2 := Hook(func(c *Context) { c.GetLogger().Info("Testing2!") })
mw := []Middleware{t2}
mw := []Hook{t2}
comb := app.combHandlers(t1, mw)
if reflect.ValueOf(t1).Pointer() != reflect.ValueOf(comb[0]).Pointer() {
t.Errorf("failed testing reverse handlers")
// combHandlers builds the slice as [hooks..., controller]
if len(comb) != 2 {
t.Errorf("failed testing comb handlers: unexpected length %d", len(comb))
}
if reflect.ValueOf(t2).Pointer() != reflect.ValueOf(comb[0]).Pointer() {
t.Errorf("failed testing comb handlers: hook should come first")
}
if reflect.ValueOf(t2).Pointer() != reflect.ValueOf(comb[1]).Pointer() {
t.Errorf("failed testing reverse handlers")
if reflect.ValueOf(t1).Pointer() != reflect.ValueOf(comb[1]).Pointer() {
t.Errorf("failed testing comb handlers: controller should come last")
}
}
@ -259,31 +267,31 @@ func TestRegisterGetRoute(t *testing.T) {
app := New()
hr := httprouter.New()
gcr := NewRouter()
gcr.Get("/", Handler(func(c *Context) *Response {
gcr.Get("/", Controller(func(c *Context) *Response {
fmt.Fprintln(c.Response.HttpResponseWriter, c.GetRequestParam("param"))
return nil
}))
gcr.Post("/", Handler(func(c *Context) *Response {
gcr.Post("/", Controller(func(c *Context) *Response {
fmt.Fprintln(c.Response.HttpResponseWriter, c.GetRequestParam("param"))
return nil
}))
gcr.Delete("/", Handler(func(c *Context) *Response {
gcr.Delete("/", Controller(func(c *Context) *Response {
fmt.Fprintln(c.Response.HttpResponseWriter, c.GetRequestParam("param"))
return nil
}))
gcr.Patch("/", Handler(func(c *Context) *Response {
gcr.Patch("/", Controller(func(c *Context) *Response {
fmt.Fprintln(c.Response.HttpResponseWriter, c.GetRequestParam("param"))
return nil
}))
gcr.Put("/", Handler(func(c *Context) *Response {
gcr.Put("/", Controller(func(c *Context) *Response {
fmt.Fprintln(c.Response.HttpResponseWriter, c.GetRequestParam("param"))
return nil
}))
gcr.Options("/", Handler(func(c *Context) *Response {
gcr.Options("/", Controller(func(c *Context) *Response {
fmt.Fprintln(c.Response.HttpResponseWriter, c.GetRequestParam("param"))
return nil
}))
gcr.Head("/", Handler(func(c *Context) *Response {
gcr.Head("/", Controller(func(c *Context) *Response {
fmt.Fprintln(c.Response.HttpResponseWriter, c.GetRequestParam("param"))
return nil
}))
@ -313,7 +321,7 @@ func TestRegisterPostRoute(t *testing.T) {
app := New()
hr := httprouter.New()
gcr := NewRouter()
gcr.Post("/", Handler(func(c *Context) *Response {
gcr.Post("/", Controller(func(c *Context) *Response {
fmt.Fprintln(c.Response.HttpResponseWriter, c.GetRequestParam("param"))
return nil
}))
@ -343,7 +351,7 @@ func TestRegisterDeleteRoute(t *testing.T) {
app := New()
hr := httprouter.New()
gcr := NewRouter()
gcr.Delete("/", Handler(func(c *Context) *Response {
gcr.Delete("/", Controller(func(c *Context) *Response {
fmt.Fprintln(c.Response.HttpResponseWriter, c.GetRequestParam("param"))
return nil
}))
@ -373,7 +381,7 @@ func TestRegisterPatchRoute(t *testing.T) {
app := New()
hr := httprouter.New()
gcr := NewRouter()
gcr.Patch("/", Handler(func(c *Context) *Response {
gcr.Patch("/", Controller(func(c *Context) *Response {
fmt.Fprintln(c.Response.HttpResponseWriter, c.GetRequestParam("param"))
return nil
}))
@ -403,7 +411,7 @@ func TestRegisterPutRoute(t *testing.T) {
app := New()
hr := httprouter.New()
gcr := NewRouter()
gcr.Put("/", Handler(func(c *Context) *Response {
gcr.Put("/", Controller(func(c *Context) *Response {
fmt.Fprintln(c.Response.HttpResponseWriter, c.GetRequestParam("param"))
return nil
}))
@ -433,7 +441,7 @@ func TestRegisterOptionsRoute(t *testing.T) {
app := New()
hr := httprouter.New()
gcr := NewRouter()
gcr.Options("/", Handler(func(c *Context) *Response {
gcr.Options("/", Controller(func(c *Context) *Response {
fmt.Fprintln(c.Response.HttpResponseWriter, c.GetRequestParam("param"))
return nil
}))
@ -464,7 +472,7 @@ func TestRegisterHeadRoute(t *testing.T) {
hr := httprouter.New()
gcr := NewRouter()
tfp := filepath.Join(t.TempDir(), uuid.NewString())
gcr.Head("/", Handler(func(c *Context) *Response {
gcr.Head("/", Controller(func(c *Context) *Response {
param := c.GetRequestParam("param")
p, _ := param.(string)
f, err := os.OpenFile(p, os.O_CREATE|os.O_RDWR, 777)