1
0
Fork 0
forked from goffee/core
core/router_test.go

126 lines
2.7 KiB
Go
Raw Permalink Normal View History

2024-09-12 18:13:16 -04:00
// 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()
2024-09-15 20:00:38 -04:00
handler := Controller(func(c *Context) *Response {
2024-09-12 18:13:16 -04:00
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()
2024-09-15 20:00:38 -04:00
handler := Controller(func(c *Context) *Response {
2024-09-12 18:13:16 -04:00
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()
2024-09-15 20:00:38 -04:00
handler := Controller(func(c *Context) *Response {
2024-09-12 18:13:16 -04:00
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()
2024-09-15 20:00:38 -04:00
handler := Controller(func(c *Context) *Response {
2024-09-12 18:13:16 -04:00
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()
2024-09-15 20:00:38 -04:00
handler := Controller(func(c *Context) *Response {
2024-09-12 18:13:16 -04:00
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()
2024-09-15 20:00:38 -04:00
handler := Controller(func(c *Context) *Response {
2024-09-12 18:13:16 -04:00
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()
2024-09-15 20:00:38 -04:00
r.Get("/", Controller(func(c *Context) *Response {
2024-09-12 18:13:16 -04:00
c.GetLogger().Info(TEST_STR)
return nil
}))
2024-09-15 20:00:38 -04:00
r.Post("/", Controller(func(c *Context) *Response {
2024-09-12 18:13:16 -04:00
c.GetLogger().Info(TEST_STR)
return nil
}))
if len(r.GetRoutes()) != 2 {
t.Errorf("failed getting added routes")
}
}