1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-22 20:37:05 +00:00

new feature: versioned controllers

Former-commit-id: c797e23c78b1e74bbe9ba56673f3a98f17f5e2f7
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-06-19 20:58:24 +03:00
parent 3f98b39632
commit 311b560717
13 changed files with 217 additions and 32 deletions

View 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"
}

View 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")
}