1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-19 10:57:05 +00:00

implement a simple path param binder

Former-commit-id: 2edc7f115332b7afe42d6b0b1b7b6edd4a44a121
This commit is contained in:
kataras
2017-11-25 14:04:35 +02:00
parent 2448a60e04
commit 9d63e3194f
5 changed files with 177 additions and 8 deletions

66
mvc2/path_param_test.go Normal file
View File

@@ -0,0 +1,66 @@
package mvc2
import (
"testing"
"github.com/kataras/iris/context"
)
func TestPathParamsBinder(t *testing.T) {
m := New()
m.In(PathParamsBinder)
got := ""
h := m.Handler(func(params PathParams) {
got = params.Get("firstname") + params.Get("lastname")
})
ctx := context.NewContext(nil)
ctx.Params().Set("firstname", "Gerasimos")
ctx.Params().Set("lastname", "Maropoulos")
h(ctx)
expected := "GerasimosMaropoulos"
if got != expected {
t.Fatalf("expected the params 'firstname' + 'lastname' to be '%s' but got '%s'", expected, got)
}
}
func TestPathParamBinder(t *testing.T) {
m := New()
m.In(PathParamBinder("username"))
got := ""
executed := false
h := m.Handler(func(username PathParam) {
// this should not be fired at all if "username" param wasn't found at all.
// although router is responsible for that but the `ParamBinder` makes that check as well because
// the end-developer may put a param as input argument on her/his function but
// on its route's path didn't describe the path parameter,
// the handler fires a warning and stops the execution for the invalid handler to protect the user.
executed = true
got = username.String()
})
expectedUsername := "kataras"
ctx := context.NewContext(nil)
ctx.Params().Set("username", expectedUsername)
h(ctx)
if got != expectedUsername {
t.Fatalf("expected the param 'username' to be '%s' but got '%s'", expectedUsername, got)
}
// test the non executed if param not found.
executed = false
got = ""
ctx2 := context.NewContext(nil)
h(ctx2)
if got != "" {
t.Fatalf("expected the param 'username' to be entirely empty but got '%s'", got)
}
if executed {
t.Fatalf("expected the handler to not be executed")
}
}