mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 18:07:01 +00:00
mvc: versioning: add 'Deprecated' feature as well
Former-commit-id: c233bae47aa765a7e1cd9ab7000acd14614a78ae
This commit is contained in:
@@ -1,10 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/mvc"
|
||||
)
|
||||
|
||||
// Optional deprecated X-API-XXX headers for version 1.
|
||||
var opts = mvc.DeprecationOptions{
|
||||
WarnMessage: "deprecated, see <this link>",
|
||||
DeprecationDate: time.Now().UTC(),
|
||||
DeprecationInfo: "a bigger version is available, see <this link> for more information",
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
|
||||
@@ -18,9 +27,10 @@ func newApp() *iris.Application {
|
||||
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(v1Controller), mvc.Version("1"), mvc.Deprecated(opts)) // 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))
|
||||
}
|
||||
|
||||
|
||||
@@ -18,9 +18,17 @@ func TestVersionedController(t *testing.T) {
|
||||
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")
|
||||
|
||||
// Test Deprecated (v1)
|
||||
ex := e.GET("/data").WithHeader(versioning.AcceptVersionHeaderKey, "1.0").Expect()
|
||||
ex.Status(iris.StatusOK).Body().Equal("data (v1.x)")
|
||||
ex.Header("X-API-Warn").Equal(opts.WarnMessage)
|
||||
expectedDateStr := opts.DeprecationDate.Format(app.ConfigurationReadOnly().GetTimeFormat())
|
||||
ex.Header("X-API-Deprecation-Date").Equal(expectedDateStr)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user