1 Star 0 Fork 10

dongfang/flamego

forked from Flamego/flamego 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
handler_test.go 1.26 KB
一键复制 编辑 原始数据 按行查看 历史
// Copyright 2021 Flamego. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package flamego
import (
"net/http"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/flamego/flamego/inject"
)
var _ inject.FastInvoker = (*testHandlerFastInvoker)(nil)
type testHandlerFastInvoker func() string
func (invoke testHandlerFastInvoker) Invoke([]interface{}) ([]reflect.Value, error) {
return []reflect.Value{reflect.ValueOf(invoke())}, nil
}
func TestValidateAndWrapHandler(t *testing.T) {
t.Run("not a callable function", func(t *testing.T) {
defer func() {
assert.Contains(t, recover(), "handler must be a callable function")
}()
validateAndWrapHandler("string", nil)
})
handlers := []Handler{
func(Context) {},
func(http.ResponseWriter, *http.Request) {},
http.HandlerFunc(nil),
func() string { return "" },
}
for _, h := range handlers {
t.Run("handlers", func(t *testing.T) {
assert.False(t, inject.IsFastInvoker(h))
h = validateAndWrapHandler(h,
func(handler Handler) Handler {
v, ok := h.(func() string)
if ok {
return testHandlerFastInvoker(v)
}
return h
},
)
assert.True(t, inject.IsFastInvoker(h))
})
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/401429542/flamego.git
[email protected]:401429542/flamego.git
401429542
flamego
flamego
main

搜索帮助