mirror of
https://github.com/kataras/iris.git
synced 2026-01-21 10:56:01 +00:00
new feature: versioned controllers
Former-commit-id: c797e23c78b1e74bbe9ba56673f3a98f17f5e2f7
This commit is contained in:
@@ -190,6 +190,7 @@
|
||||
* [Regexp](mvc/regexp/main.go)
|
||||
* [Session Controller](mvc/session-controller/main.go)
|
||||
* [Authenticated Controller](mvc/authenticated-controller/main.go)
|
||||
* [Versioned Controller](mvc/versioned-controller/main.go)
|
||||
* [Websocket Controller](mvc/websocket)
|
||||
* [Register Middleware](mvc/middleware)
|
||||
* [gRPC](mvc/grpc-compatible)
|
||||
|
||||
52
_examples/mvc/versioned-controller/main.go
Normal file
52
_examples/mvc/versioned-controller/main.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/mvc"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
|
||||
// See main_test.go for request examples.
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
|
||||
dataRouter := app.Party("/data")
|
||||
{
|
||||
m := mvc.New(dataRouter)
|
||||
m.Handle(new(v1Controller), mvc.Version("1")) // 1 or 1.0, 1.0.0 ...
|
||||
m.Handle(new(v2Controller), mvc.Version("2.3")) // 2.3 or 2.3.0
|
||||
m.Handle(new(v3Controller), mvc.Version(">=3, <4")) // 3, 3.x, 3.x.x ...
|
||||
m.Handle(new(noVersionController))
|
||||
}
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
type v1Controller struct{}
|
||||
|
||||
func (c *v1Controller) Get() string {
|
||||
return "data (v1.x)"
|
||||
}
|
||||
|
||||
type v2Controller struct{}
|
||||
|
||||
func (c *v2Controller) Get() string {
|
||||
return "data (v2.x)"
|
||||
}
|
||||
|
||||
type v3Controller struct{}
|
||||
|
||||
func (c *v3Controller) Get() string {
|
||||
return "data (v3.x)"
|
||||
}
|
||||
|
||||
type noVersionController struct{}
|
||||
|
||||
func (c *noVersionController) Get() string {
|
||||
return "data"
|
||||
}
|
||||
26
_examples/mvc/versioned-controller/main_test.go
Normal file
26
_examples/mvc/versioned-controller/main_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/httptest"
|
||||
"github.com/kataras/iris/v12/versioning"
|
||||
)
|
||||
|
||||
func TestVersionedController(t *testing.T) {
|
||||
app := newApp()
|
||||
|
||||
e := httptest.New(t, app)
|
||||
e.GET("/data").WithHeader(versioning.AcceptVersionHeaderKey, "1").Expect().
|
||||
Status(iris.StatusOK).Body().Equal("data (v1.x)")
|
||||
e.GET("/data").WithHeader(versioning.AcceptVersionHeaderKey, "2.3.0").Expect().
|
||||
Status(iris.StatusOK).Body().Equal("data (v2.x)")
|
||||
e.GET("/data").WithHeader(versioning.AcceptVersionHeaderKey, "3.1").Expect().
|
||||
Status(iris.StatusOK).Body().Equal("data (v3.x)")
|
||||
// Test invalid version or no version at all.
|
||||
e.GET("/data").WithHeader(versioning.AcceptVersionHeaderKey, "4").Expect().
|
||||
Status(iris.StatusOK).Body().Equal("data")
|
||||
e.GET("/data").Expect().
|
||||
Status(iris.StatusOK).Body().Equal("data")
|
||||
}
|
||||
@@ -14,14 +14,14 @@ func TestSessionsEncodeDecode(t *testing.T) {
|
||||
es := e.GET("/set").Expect()
|
||||
es.Status(iris.StatusOK)
|
||||
es.Cookies().NotEmpty()
|
||||
es.Body().Equal("All ok session set to: iris")
|
||||
es.Body().Equal("All ok session set to: iris [isNew=true]")
|
||||
|
||||
e.GET("/get").Expect().Status(iris.StatusOK).Body().Equal("The name on the /set was: iris")
|
||||
// delete and re-get
|
||||
e.GET("/delete").Expect().Status(iris.StatusOK)
|
||||
e.GET("/get").Expect().Status(iris.StatusOK).Body().Equal("The name on the /set was: ")
|
||||
// set, clear and re-get
|
||||
e.GET("/set").Expect().Body().Equal("All ok session set to: iris")
|
||||
e.GET("/set").Expect().Body().Equal("All ok session set to: iris [isNew=false]")
|
||||
e.GET("/clear").Expect().Status(iris.StatusOK)
|
||||
e.GET("/get").Expect().Status(iris.StatusOK).Body().Equal("The name on the /set was: ")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user