mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
add simple tests for _examples/mvc/hello-world and session-controller
Former-commit-id: d88a792ba57cd869d2888f41bca6eb3e5b4f7d49
This commit is contained in:
parent
a25c0557de
commit
b8cafce6b9
|
@ -28,7 +28,9 @@ import (
|
|||
// what suits you best with Iris, low-level handlers: performance
|
||||
// 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()
|
||||
// Optionally, add two built'n handlers
|
||||
// that can recover from any http-relative panics
|
||||
|
@ -38,6 +40,11 @@ func main() {
|
|||
|
||||
// Register a controller based on the root Router, "/".
|
||||
mvc.New(app).Register(new(ExampleController))
|
||||
return app
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
|
||||
// http://localhost:8080
|
||||
// http://localhost:8080/ping
|
||||
|
|
23
_examples/mvc/hello-world/main_test.go
Normal file
23
_examples/mvc/hello-world/main_test.go
Normal 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")
|
||||
}
|
|
@ -7,18 +7,14 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/mvc"
|
||||
"github.com/kataras/iris/sessions"
|
||||
)
|
||||
|
||||
// VisitController handles the root route.
|
||||
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,
|
||||
// its initialization happens at the `BeginRequest`.
|
||||
// its initialization happens by the dependency function that we've added to the `visitApp`.
|
||||
Session *sessions.Session
|
||||
|
||||
// A time.time which is binded from the `app.Controller`,
|
||||
|
@ -26,53 +22,49 @@ type VisitController struct {
|
|||
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
|
||||
// Method: GET
|
||||
// Path: http://localhost:8080
|
||||
func (c *VisitController) Get() string {
|
||||
// get the visits, before calcuate this new one.
|
||||
visits, _ := c.Session.GetIntDefault("visits", 0)
|
||||
|
||||
// increment the visits and store to the session.
|
||||
visits++
|
||||
c.Session.Set("visits", visits)
|
||||
|
||||
// it increments a "visits" value of integer by one,
|
||||
// if the entry with key 'visits' doesn't exist it will create it for you.
|
||||
visits := c.Session.Increment("visits", 1)
|
||||
// write the current, updated visits.
|
||||
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",
|
||||
visits, since)
|
||||
}
|
||||
|
||||
var (
|
||||
manager = sessions.New(sessions.Config{Cookie: "mysession_cookie_name"})
|
||||
func newApp() *iris.Application {
|
||||
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() {
|
||||
app := iris.New()
|
||||
|
||||
// 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())
|
||||
app := newApp()
|
||||
|
||||
// 1. open the browser (no in private mode)
|
||||
// 2. navigate to http://localhost:8080
|
||||
|
|
21
_examples/mvc/session-controller/main_test.go
Normal file
21
_examples/mvc/session-controller/main_test.go
Normal 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")
|
||||
}
|
|
@ -1,14 +1,14 @@
|
|||
package router
|
||||
|
||||
import (
|
||||
"github.com/kataras/golog"
|
||||
"html"
|
||||
"net/http"
|
||||
"sort"
|
||||
"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/netutil"
|
||||
"github.com/kataras/iris/core/router/node"
|
||||
|
|
|
@ -58,8 +58,6 @@ func getNameOf(typ reflect.Type) string {
|
|||
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 {
|
||||
var (
|
||||
val = reflect.ValueOf(controller)
|
||||
|
@ -121,12 +119,7 @@ func (c *ControllerActivator) isReservedMethod(name string) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
// register all available, exported methods to handlers if possible.
|
||||
func (c *ControllerActivator) parseMethods() {
|
||||
n := c.Type.NumMethod()
|
||||
for i := 0; i < n; i++ {
|
||||
m := c.Type.Method(i)
|
||||
|
||||
func (c *ControllerActivator) parseMethod(m reflect.Method) {
|
||||
httpMethod, httpPath, err := parseMethod(m, c.isReservedMethod)
|
||||
if err != nil {
|
||||
if err != errSkip {
|
||||
|
@ -134,11 +127,22 @@ func (c *ControllerActivator) parseMethods() {
|
|||
c.Router.GetReporter().AddErr(err)
|
||||
|
||||
}
|
||||
continue
|
||||
return
|
||||
}
|
||||
|
||||
c.Handle(httpMethod, httpPath, m.Name)
|
||||
}
|
||||
|
||||
// register all available, exported methods to handlers if possible.
|
||||
func (c *ControllerActivator) parseMethods() {
|
||||
n := c.Type.NumMethod()
|
||||
// wg := &sync.WaitGroup{}
|
||||
// wg.Add(n)
|
||||
for i := 0; i < n; i++ {
|
||||
m := c.Type.Method(i)
|
||||
c.parseMethod(m)
|
||||
}
|
||||
// wg.Wait()
|
||||
}
|
||||
|
||||
func (c *ControllerActivator) activate() {
|
||||
|
@ -248,7 +252,7 @@ func buildHandler(m reflect.Method, typ reflect.Type, initRef reflect.Value, str
|
|||
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.
|
||||
if !implementsBase {
|
||||
|
||||
|
|
|
@ -9,6 +9,10 @@ import (
|
|||
"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() {
|
||||
app := iris.New()
|
||||
mvc.New(app.Party("/todo")).Configure(TodoApp)
|
||||
|
|
Loading…
Reference in New Issue
Block a user