core/middlewares_test.go

66 lines
1.5 KiB
Go
Raw Normal View History

2024-09-12 18:13:16 -04:00
// Copyright 2021 Harran Ali <harran.m@gmail.com>. All rights reserved.
// 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"
"reflect"
"testing"
)
func TestNewMiddlewares(t *testing.T) {
mw := NewMiddlewares()
if fmt.Sprintf("%T", mw) != "*core.Middlewares" {
t.Errorf("failed testing new middleware")
}
}
func TestResloveMiddleWares(t *testing.T) {
NewMiddlewares()
mw := ResolveMiddlewares()
if fmt.Sprintf("%T", mw) != "*core.Middlewares" {
t.Errorf("failed resolve middlewares")
}
}
func TestAttach(t *testing.T) {
mw := NewMiddlewares()
tmw := Middleware(func(c *Context) {
c.GetLogger().Info("Testing!")
})
mw.Attach(tmw)
mws := mw.getByIndex(0)
if reflect.ValueOf(tmw).Pointer() != reflect.ValueOf(mws).Pointer() {
t.Errorf("Failed testing attach middleware")
}
}
func TestGetMiddleWares(t *testing.T) {
mw := NewMiddlewares()
t1 := Middleware(func(c *Context) {
c.GetLogger().Info("testing1!")
})
t2 := Middleware(func(c *Context) {
c.GetLogger().Info("testing2!")
})
mw.Attach(t1)
mw.Attach(t2)
if len(mw.GetMiddlewares()) != 2 {
t.Errorf("failed testing get middlewares")
}
}
func TestMiddlewareGetByIndex(t *testing.T) {
mw := NewMiddlewares()
t1 := Middleware(func(c *Context) {
c.GetLogger().Info("testing!")
})
mw.Attach(t1)
if reflect.ValueOf(mw.getByIndex(0)).Pointer() != reflect.ValueOf(t1).Pointer() {
t.Errorf("failed testing get by index")
}
}