1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 10:27:06 +00:00

OK, my dream-idea is implemented. TODO: Some examples and doc.go is not updated yet, comments on the mvc/di subpackage, the tutorial/vuejs-todo-mvc is running but not finished yet (it's using browser's localstorage and it should be replaced by the http requests that are registered via iris mvc

Former-commit-id: 0ea7e01ce1d78bcb78b40f3b0f5c03ad7c9abaea
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-12-16 06:38:28 +02:00
parent 55dfd195e0
commit 34664aa311
41 changed files with 437 additions and 408 deletions

66
mvc/param_test.go Normal file
View File

@@ -0,0 +1,66 @@
package mvc
import (
"testing"
"github.com/kataras/iris/context"
)
func TestPathParamsBinder(t *testing.T) {
m := NewEngine()
m.Dependencies.Add(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 := NewEngine()
m.Dependencies.Add(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")
}
}