2018-11-10 02:49:32 +01:00
|
|
|
package versioning_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/kataras/iris"
|
|
|
|
"github.com/kataras/iris/httptest"
|
|
|
|
"github.com/kataras/iris/versioning"
|
|
|
|
)
|
|
|
|
|
|
|
|
func notFoundHandler(ctx iris.Context) {
|
|
|
|
ctx.NotFound()
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
v10Response = "v1.0 handler"
|
|
|
|
v2Response = "v2.x handler"
|
|
|
|
)
|
|
|
|
|
|
|
|
func sendHandler(contents string) iris.Handler {
|
|
|
|
return func(ctx iris.Context) {
|
|
|
|
ctx.WriteString(contents)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-11 01:18:19 +01:00
|
|
|
func TestIf(t *testing.T) {
|
|
|
|
if expected, got := true, versioning.If("1.0", ">=1"); expected != got {
|
|
|
|
t.Fatalf("expected %s to be %s", "1.0", ">= 1")
|
|
|
|
}
|
|
|
|
if expected, got := true, versioning.If("1.2.3", "> 1.2"); expected != got {
|
|
|
|
t.Fatalf("expected %s to be %s", "1.2.3", "> 1.2")
|
|
|
|
}
|
|
|
|
}
|
2018-11-10 22:29:24 +01:00
|
|
|
func TestNewMatcher(t *testing.T) {
|
2018-11-10 02:49:32 +01:00
|
|
|
app := iris.New()
|
|
|
|
|
|
|
|
userAPI := app.Party("/api/user")
|
2018-11-10 22:29:24 +01:00
|
|
|
userAPI.Get("/", versioning.NewMatcher(versioning.Map{
|
2018-11-10 02:49:32 +01:00
|
|
|
"1.0": sendHandler(v10Response),
|
|
|
|
">= 2, < 3": sendHandler(v2Response),
|
|
|
|
versioning.NotFound: notFoundHandler,
|
|
|
|
}))
|
|
|
|
|
2018-11-11 01:18:19 +01:00
|
|
|
// middleware as usual.
|
|
|
|
myMiddleware := func(ctx iris.Context) {
|
|
|
|
ctx.Header("X-Custom", "something")
|
|
|
|
ctx.Next()
|
|
|
|
}
|
|
|
|
myVersions := versioning.Map{
|
|
|
|
"1.0": sendHandler(v10Response),
|
|
|
|
}
|
|
|
|
|
|
|
|
userAPI.Get("/with_middleware", myMiddleware, versioning.NewMatcher(myVersions))
|
|
|
|
|
2018-11-10 02:49:32 +01:00
|
|
|
e := httptest.New(t, app)
|
|
|
|
|
|
|
|
e.GET("/api/user").WithHeader(versioning.AcceptVersionHeaderKey, "1").Expect().
|
|
|
|
Status(iris.StatusOK).Body().Equal(v10Response)
|
|
|
|
e.GET("/api/user").WithHeader(versioning.AcceptVersionHeaderKey, "2.0").Expect().
|
|
|
|
Status(iris.StatusOK).Body().Equal(v2Response)
|
|
|
|
e.GET("/api/user").WithHeader(versioning.AcceptVersionHeaderKey, "2.1").Expect().
|
|
|
|
Status(iris.StatusOK).Body().Equal(v2Response)
|
|
|
|
e.GET("/api/user").WithHeader(versioning.AcceptVersionHeaderKey, "2.9.9").Expect().
|
|
|
|
Status(iris.StatusOK).Body().Equal(v2Response)
|
|
|
|
|
2018-11-11 01:18:19 +01:00
|
|
|
// middleware as usual.
|
|
|
|
ex := e.GET("/api/user/with_middleware").WithHeader(versioning.AcceptVersionHeaderKey, "1.0").Expect()
|
|
|
|
ex.Status(iris.StatusOK).Body().Equal(v10Response)
|
|
|
|
ex.Header("X-Custom").Equal("something")
|
|
|
|
|
2018-11-10 02:49:32 +01:00
|
|
|
e.GET("/api/user").WithHeader(versioning.AcceptVersionHeaderKey, "3.0").Expect().
|
|
|
|
Status(iris.StatusNotFound).Body().Equal("Not Found")
|
|
|
|
}
|
2018-11-10 22:29:24 +01:00
|
|
|
|
|
|
|
func TestNewGroup(t *testing.T) {
|
|
|
|
app := iris.New()
|
|
|
|
|
|
|
|
userAPI := app.Party("/api/user")
|
|
|
|
// [... static serving, middlewares and etc goes here].
|
|
|
|
|
|
|
|
userAPIV10 := versioning.NewGroup("1.0").Deprecated(versioning.DefaultDeprecationOptions)
|
|
|
|
userAPIV10.Get("/", sendHandler(v10Response))
|
|
|
|
|
|
|
|
userAPIV2 := versioning.NewGroup(">= 2, < 3")
|
|
|
|
userAPIV2.Get("/", sendHandler(v2Response))
|
|
|
|
userAPIV2.Post("/", sendHandler(v2Response))
|
|
|
|
userAPIV2.Put("/other", sendHandler(v2Response))
|
|
|
|
|
|
|
|
// versioning.Concat(userAPIV10, userAPIV2)
|
|
|
|
// NotFound(func(ctx iris.Context) {
|
|
|
|
// ctx.StatusCode(iris.StatusNotFound)
|
|
|
|
// ctx.Writef("unknown version %s", versioning.GetVersion(ctx))
|
|
|
|
// }).
|
|
|
|
// For(userAPI)
|
|
|
|
// This is legal too:
|
|
|
|
// For(app.PartyFunc("/api/user", func(r iris.Party) {
|
|
|
|
// // [... static serving, middlewares and etc goes here].
|
|
|
|
// }))
|
|
|
|
|
|
|
|
versioning.RegisterGroups(userAPI, userAPIV10, userAPIV2)
|
|
|
|
|
|
|
|
e := httptest.New(t, app)
|
|
|
|
|
|
|
|
ex := e.GET("/api/user").WithHeader(versioning.AcceptVersionHeaderKey, "1").Expect()
|
|
|
|
ex.Status(iris.StatusOK).Body().Equal(v10Response)
|
|
|
|
ex.Header("X-API-Warn").Equal(versioning.DefaultDeprecationOptions.WarnMessage)
|
|
|
|
|
|
|
|
e.GET("/api/user").WithHeader(versioning.AcceptVersionHeaderKey, "2.0").Expect().
|
|
|
|
Status(iris.StatusOK).Body().Equal(v2Response)
|
|
|
|
e.GET("/api/user").WithHeader(versioning.AcceptVersionHeaderKey, "2.1").Expect().
|
|
|
|
Status(iris.StatusOK).Body().Equal(v2Response)
|
|
|
|
e.GET("/api/user").WithHeader(versioning.AcceptVersionHeaderKey, "2.9.9").Expect().
|
|
|
|
Status(iris.StatusOK).Body().Equal(v2Response)
|
|
|
|
e.POST("/api/user").WithHeader(versioning.AcceptVersionHeaderKey, "2").Expect().
|
|
|
|
Status(iris.StatusOK).Body().Equal(v2Response)
|
|
|
|
e.PUT("/api/user/other").WithHeader(versioning.AcceptVersionHeaderKey, "2.9").Expect().
|
|
|
|
Status(iris.StatusOK).Body().Equal(v2Response)
|
|
|
|
|
|
|
|
e.GET("/api/user").WithHeader(versioning.AcceptVersionHeaderKey, "3.0").Expect().
|
|
|
|
Status(iris.StatusNotImplemented).Body().Equal("version not found")
|
|
|
|
}
|