add simple tests for _examples/mvc/hello-world and session-controller

Former-commit-id: d88a792ba57cd869d2888f41bca6eb3e5b4f7d49
This commit is contained in:
Gerasimos (Makis) Maropoulos 2017-12-16 21:27:20 +02:00
parent a25c0557de
commit b8cafce6b9
7 changed files with 110 additions and 59 deletions

View File

@ -28,7 +28,9 @@ import (
// what suits you best with Iris, low-level handlers: performance // what suits you best with Iris, low-level handlers: performance
// or high-level controllers: easier to maintain and smaller codebase on large applications. // or high-level controllers: easier to maintain and smaller codebase on large applications.
func main() { // Of course you can put all these to main func, it's just a separate function
// for the main_test.go.
func newApp() *iris.Application {
app := iris.New() app := iris.New()
// Optionally, add two built'n handlers // Optionally, add two built'n handlers
// that can recover from any http-relative panics // that can recover from any http-relative panics
@ -38,6 +40,11 @@ func main() {
// Register a controller based on the root Router, "/". // Register a controller based on the root Router, "/".
mvc.New(app).Register(new(ExampleController)) mvc.New(app).Register(new(ExampleController))
return app
}
func main() {
app := newApp()
// http://localhost:8080 // http://localhost:8080
// http://localhost:8080/ping // http://localhost:8080/ping

View File

@ -0,0 +1,23 @@
package main
import (
"testing"
"github.com/kataras/iris/httptest"
)
func TestMVCHelloWorld(t *testing.T) {
e := httptest.New(t, newApp())
e.GET("/").Expect().Status(httptest.StatusOK).
ContentType("text/html", "utf-8").Body().Equal("<h1>Welcome</h1>")
e.GET("/ping").Expect().Status(httptest.StatusOK).
Body().Equal("pong")
e.GET("/hello").Expect().Status(httptest.StatusOK).
JSON().Object().Value("message").Equal("Hello Iris!")
e.GET("/custom_path").Expect().Status(httptest.StatusOK).
Body().Equal("hello from the custom handler without following the naming guide")
}

View File

@ -7,18 +7,14 @@ import (
"time" "time"
"github.com/kataras/iris" "github.com/kataras/iris"
"github.com/kataras/iris/mvc"
"github.com/kataras/iris/sessions" "github.com/kataras/iris/sessions"
) )
// VisitController handles the root route. // VisitController handles the root route.
type VisitController struct { type VisitController struct {
iris.C
// the sessions manager, we need that to set `Session`.
// It's binded from `app.Controller`.
Manager *sessions.Sessions
// the current request session, // the current request session,
// its initialization happens at the `BeginRequest`. // its initialization happens by the dependency function that we've added to the `visitApp`.
Session *sessions.Session Session *sessions.Session
// A time.time which is binded from the `app.Controller`, // A time.time which is binded from the `app.Controller`,
@ -26,53 +22,49 @@ type VisitController struct {
StartTime time.Time StartTime time.Time
} }
// BeginRequest is executed for each Get, Post, Put requests,
// can be used to share context, common data
// or to cancel the request via `ctx.StopExecution()`.
func (c *VisitController) BeginRequest(ctx iris.Context) {
// always call the embedded `BeginRequest` before everything else.
c.C.BeginRequest(ctx)
if c.Manager == nil {
ctx.Application().Logger().Errorf(`VisitController: sessions manager is nil, you should bind it`)
// dont run the main method handler and any "done" handlers.
ctx.StopExecution()
return
}
// set the `c.Session` we will use that in our Get method.
c.Session = c.Manager.Start(ctx)
}
// Get handles // Get handles
// Method: GET // Method: GET
// Path: http://localhost:8080 // Path: http://localhost:8080
func (c *VisitController) Get() string { func (c *VisitController) Get() string {
// get the visits, before calcuate this new one. // it increments a "visits" value of integer by one,
visits, _ := c.Session.GetIntDefault("visits", 0) // if the entry with key 'visits' doesn't exist it will create it for you.
visits := c.Session.Increment("visits", 1)
// increment the visits and store to the session.
visits++
c.Session.Set("visits", visits)
// write the current, updated visits. // write the current, updated visits.
since := time.Now().Sub(c.StartTime).Seconds() since := time.Now().Sub(c.StartTime).Seconds()
return fmt.Sprintf("%d visit from my current session in %0.1f seconds of server's up-time", return fmt.Sprintf("%d visit from my current session in %0.1f seconds of server's up-time",
visits, since) visits, since)
} }
var ( func newApp() *iris.Application {
manager = sessions.New(sessions.Config{Cookie: "mysession_cookie_name"}) app := iris.New()
) sess := sessions.New(sessions.Config{Cookie: "mysession_cookie_name"})
visitApp := mvc.New(app.Party("/"))
// bind the current *session.Session, which is required, to the `VisitController.Session`
// and the time.Now() to the `VisitController.StartTime`.
visitApp.AddDependencies(
// if dependency is a function which accepts
// a Context and returns a single value
// then the result type of this function is resolved by the controller
// and on each request it will call the function with its Context
// and set the result(the *sessions.Session here) to the controller's field.
//
// If dependencies are registered without field or function's input arguments as
// consumers then those dependencies are being ignored before the server ran,
// so you can bind many dependecies and use them in different controllers.
// func(ctx iris.Context) *sessions.Session {
// return sess.Start(ctx)
// }, -> same as mvc.Session(sess):
mvc.Session(sess),
time.Now(),
)
visitApp.Register(new(VisitController))
return app
}
func main() { func main() {
app := iris.New() app := newApp()
// bind our session manager, which is required, to the `VisitController.Manager`
// and the time.Now() to the `VisitController.StartTime`.
app.Controller("/", new(VisitController),
manager,
time.Now())
// 1. open the browser (no in private mode) // 1. open the browser (no in private mode)
// 2. navigate to http://localhost:8080 // 2. navigate to http://localhost:8080

View File

@ -0,0 +1,21 @@
package main
import (
"testing"
"github.com/kataras/iris/httptest"
)
func TestMVCSession(t *testing.T) {
e := httptest.New(t, newApp(), httptest.URL("http://example.com"))
e1 := e.GET("/").Expect().Status(httptest.StatusOK)
e1.Cookies().NotEmpty()
e1.Body().Contains("1 visit")
e.GET("/").Expect().Status(httptest.StatusOK).
Body().Contains("2 visit")
e.GET("/").Expect().Status(httptest.StatusOK).
Body().Contains("3 visit")
}

View File

@ -1,14 +1,14 @@
package router package router
import ( import (
"github.com/kataras/golog"
"html" "html"
"net/http" "net/http"
"sort" "sort"
"strings" "strings"
"github.com/kataras/iris/context" "github.com/kataras/golog"
"github.com/kataras/iris/context"
"github.com/kataras/iris/core/errors" "github.com/kataras/iris/core/errors"
"github.com/kataras/iris/core/netutil" "github.com/kataras/iris/core/netutil"
"github.com/kataras/iris/core/router/node" "github.com/kataras/iris/core/router/node"

View File

@ -58,8 +58,6 @@ func getNameOf(typ reflect.Type) string {
return fullname return fullname
} }
/// TODO: activate controllers with go routines so the startup time of iris
// can be improved on huge applications.
func newControllerActivator(router router.Party, controller interface{}, d *di.D) *ControllerActivator { func newControllerActivator(router router.Party, controller interface{}, d *di.D) *ControllerActivator {
var ( var (
val = reflect.ValueOf(controller) val = reflect.ValueOf(controller)
@ -121,24 +119,30 @@ func (c *ControllerActivator) isReservedMethod(name string) bool {
return false return false
} }
func (c *ControllerActivator) parseMethod(m reflect.Method) {
httpMethod, httpPath, err := parseMethod(m, c.isReservedMethod)
if err != nil {
if err != errSkip {
err = fmt.Errorf("MVC: fail to parse the route path and HTTP method for '%s.%s': %v", c.FullName, m.Name, err)
c.Router.GetReporter().AddErr(err)
}
return
}
c.Handle(httpMethod, httpPath, m.Name)
}
// register all available, exported methods to handlers if possible. // register all available, exported methods to handlers if possible.
func (c *ControllerActivator) parseMethods() { func (c *ControllerActivator) parseMethods() {
n := c.Type.NumMethod() n := c.Type.NumMethod()
// wg := &sync.WaitGroup{}
// wg.Add(n)
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
m := c.Type.Method(i) m := c.Type.Method(i)
c.parseMethod(m)
httpMethod, httpPath, err := parseMethod(m, c.isReservedMethod)
if err != nil {
if err != errSkip {
err = fmt.Errorf("MVC: fail to parse the route path and HTTP method for '%s.%s': %v", c.FullName, m.Name, err)
c.Router.GetReporter().AddErr(err)
}
continue
}
c.Handle(httpMethod, httpPath, m.Name)
} }
// wg.Wait()
} }
func (c *ControllerActivator) activate() { func (c *ControllerActivator) activate() {
@ -248,7 +252,7 @@ func buildHandler(m reflect.Method, typ reflect.Type, initRef reflect.Value, str
elemTyp = di.IndirectType(typ) elemTyp = di.IndirectType(typ)
) )
// if it doesn't implements the base controller, // if it doesn't implement the base controller,
// it may have struct injector and/or func injector. // it may have struct injector and/or func injector.
if !implementsBase { if !implementsBase {

View File

@ -9,6 +9,10 @@ import (
"github.com/kataras/iris/mvc" "github.com/kataras/iris/mvc"
) )
// TODO: It's not here but this file is what I'll see before the commit in order to delete it:
// Think a way to simplify the router cycle, I did create it to support any type of router
// but as I see nobody wants to override the iris router's behavior(I'm not speaking about wrapper, this will stay of course because it's useful on security-critical middlewares) because it's the best by far.
// Therefore I should reduce some "freedom of change" for the shake of code maintanability in the core/router files: handler.go | router.go and single change on APIBuilder's field.
func main() { func main() {
app := iris.New() app := iris.New()
mvc.New(app.Party("/todo")).Configure(TodoApp) mvc.New(app.Party("/todo")).Configure(TodoApp)