mirror of
https://github.com/kataras/iris.git
synced 2026-01-08 20:41:57 +00:00
fix https://github.com/kataras/iris/issues/1450 and continue on implementing 1449
Former-commit-id: 617f64d061e88f050a443ea1751fa244164656c5
This commit is contained in:
@@ -4,6 +4,7 @@ package hero_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
@@ -14,8 +15,8 @@ import (
|
||||
|
||||
// dynamic func
|
||||
type testUserStruct struct {
|
||||
ID int64
|
||||
Username string
|
||||
ID int64 `json:"id"`
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
func testBinderFunc(ctx iris.Context) testUserStruct {
|
||||
@@ -126,3 +127,38 @@ func TestBindFunctionAsFunctionInputArgument(t *testing.T) {
|
||||
e.POST("/").WithFormField("username", expectedUsername).
|
||||
Expect().Status(iris.StatusOK).Body().Equal(expectedUsername)
|
||||
}
|
||||
|
||||
func TestBindReflectValue(t *testing.T) {
|
||||
// TODO: THINK of simplify this,
|
||||
// as 'hero' and 'mvc' are not depend on the root kataras/iris/v12 package, smart decision back then.
|
||||
// e.g.
|
||||
// app := iris.New()
|
||||
// app.RegisterDependency(...)
|
||||
// app.HandleFunc("GET POST", "/", func(input MyInput) MyOutput {})
|
||||
// instead of:
|
||||
// app := iris.New()
|
||||
// h := hero.New()
|
||||
// h.Register(...) or hero.Register for shared deps across Iris different applications.
|
||||
// handler := h.Handler(func(input MyInput) MyOutput {})
|
||||
// app.HandleMany("GET POST", "/", handler)
|
||||
|
||||
h := New()
|
||||
h.Register(func(ctx iris.Context) reflect.Value {
|
||||
var v interface{}
|
||||
err := ctx.ReadJSON(&v)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return reflect.ValueOf(v)
|
||||
// return reflect.Value{}
|
||||
})
|
||||
postHandler := h.Handler(func(input testUserStruct) string {
|
||||
return input.Username
|
||||
})
|
||||
|
||||
app := iris.New()
|
||||
app.Post("/", postHandler)
|
||||
|
||||
e := httptest.New(t, app)
|
||||
e.POST("/").WithJSON(iris.Map{"username": "makis"}).Expect().Status(httptest.StatusOK).Body().Equal("makis")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user