core/router_test.go
2024-09-16 16:01:44 -05:00

125 lines
2.7 KiB
Go

// Copyright (c) 2024 Zeni Kim <zenik@smarteching.com>
// Use of this source code is governed by MIT-style
// license that can be found in the LICENSE file.
package core
import (
"fmt"
"testing"
)
func TestNewRouter(t *testing.T) {
r := NewRouter()
if fmt.Sprintf("%T", r) != "*core.Router" {
t.Error("failed asserting initiation of new router")
}
}
func TestResolveRouter(t *testing.T) {
r := ResolveRouter()
if fmt.Sprintf("%T", r) != "*core.Router" {
t.Error("failed resolve router variable")
}
}
func TestGetRequest(t *testing.T) {
r := NewRouter()
handler := Controller(func(c *Context) *Response {
c.GetLogger().Info(TEST_STR)
return nil
})
r.Get("/", handler)
route := r.GetRoutes()[0]
if route.Method != "get" || route.Path != "/" {
t.Errorf("failed adding route with get http method")
}
}
func TestPostRequest(t *testing.T) {
r := NewRouter()
handler := Controller(func(c *Context) *Response {
c.GetLogger().Info(TEST_STR)
return nil
})
r.Post("/", handler)
route := r.GetRoutes()[0]
if route.Method != "post" || route.Path != "/" {
t.Errorf("failed adding route with post http method")
}
}
func TestDeleteRequest(t *testing.T) {
r := NewRouter()
handler := Controller(func(c *Context) *Response {
c.GetLogger().Info(TEST_STR)
return nil
})
r.Delete("/", handler)
route := r.GetRoutes()[0]
if route.Method != "delete" || route.Path != "/" {
t.Errorf("failed adding route with delete http method")
}
}
func TestPutRequest(t *testing.T) {
r := NewRouter()
handler := Controller(func(c *Context) *Response {
c.GetLogger().Info(TEST_STR)
return nil
})
r.Put("/", handler)
route := r.GetRoutes()[0]
if route.Method != "put" || route.Path != "/" {
t.Errorf("failed adding route with put http method")
}
}
func TestOptionsRequest(t *testing.T) {
r := NewRouter()
handler := Controller(func(c *Context) *Response {
c.GetLogger().Info(TEST_STR)
return nil
})
r.Options("/", handler)
route := r.GetRoutes()[0]
if route.Method != "options" || route.Path != "/" {
t.Errorf("failed adding route with options http method")
}
}
func TestHeadRequest(t *testing.T) {
r := NewRouter()
handler := Controller(func(c *Context) *Response {
c.GetLogger().Info(TEST_STR)
return nil
})
r.Head("/", handler)
route := r.GetRoutes()[0]
if route.Method != "head" || route.Path != "/" {
t.Errorf("failed adding route with head http method")
}
}
func TestAddMultipleRoutes(t *testing.T) {
r := NewRouter()
r.Get("/", Controller(func(c *Context) *Response {
c.GetLogger().Info(TEST_STR)
return nil
}))
r.Post("/", Controller(func(c *Context) *Response {
c.GetLogger().Info(TEST_STR)
return nil
}))
if len(r.GetRoutes()) != 2 {
t.Errorf("failed getting added routes")
}
}