mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
API versioning improvements
Replace the go-version package with a regex-free alternative semver
the result: versioned apis have almost zero performance cost now
thanks @motogo for your kind donation ❤️ - please check your github notifications
This commit is contained in:
@@ -8,6 +8,15 @@ import (
|
||||
"github.com/kataras/iris/v12/versioning"
|
||||
)
|
||||
|
||||
func TestIf(t *testing.T) {
|
||||
if expected, got := true, versioning.If("1.0.0", ">=1.0.0"); expected != got {
|
||||
t.Fatalf("expected %s to be %s", "1.0.0", ">= 1.0.0")
|
||||
}
|
||||
if expected, got := true, versioning.If("1.2.3", "> 1.2.0"); expected != got {
|
||||
t.Fatalf("expected %s to be %s", "1.2.3", "> 1.2.0")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetVersion(t *testing.T) {
|
||||
app := iris.New()
|
||||
|
||||
@@ -23,16 +32,16 @@ func TestGetVersion(t *testing.T) {
|
||||
|
||||
e := httptest.New(t, app)
|
||||
|
||||
e.GET("/").WithHeader(versioning.AcceptVersionHeaderKey, "1.0").Expect().
|
||||
Status(iris.StatusOK).Body().Equal("1.0")
|
||||
e.GET("/").WithHeader(versioning.AcceptHeaderKey, "application/vnd.api+json; version=2.1").Expect().
|
||||
Status(iris.StatusOK).Body().Equal("2.1")
|
||||
e.GET("/").WithHeader(versioning.AcceptHeaderKey, "application/vnd.api+json; version=2.1 ;other=dsa").Expect().
|
||||
Status(iris.StatusOK).Body().Equal("2.1")
|
||||
e.GET("/").WithHeader(versioning.AcceptHeaderKey, "version=2.1").Expect().
|
||||
Status(iris.StatusOK).Body().Equal("2.1")
|
||||
e.GET("/").WithHeader(versioning.AcceptHeaderKey, "version=1").Expect().
|
||||
Status(iris.StatusOK).Body().Equal("1")
|
||||
e.GET("/").WithHeader(versioning.AcceptVersionHeaderKey, "1.0.0").Expect().
|
||||
Status(iris.StatusOK).Body().Equal("1.0.0")
|
||||
e.GET("/").WithHeader(versioning.AcceptHeaderKey, "application/vnd.api+json; version=2.1.0").Expect().
|
||||
Status(iris.StatusOK).Body().Equal("2.1.0")
|
||||
e.GET("/").WithHeader(versioning.AcceptHeaderKey, "application/vnd.api+json; version=2.1.0 ;other=dsa").Expect().
|
||||
Status(iris.StatusOK).Body().Equal("2.1.0")
|
||||
e.GET("/").WithHeader(versioning.AcceptHeaderKey, "version=2.1.0").Expect().
|
||||
Status(iris.StatusOK).Body().Equal("2.1.0")
|
||||
e.GET("/").WithHeader(versioning.AcceptHeaderKey, "version=1.0.0").Expect().
|
||||
Status(iris.StatusOK).Body().Equal("1.0.0")
|
||||
|
||||
// unknown versions.
|
||||
e.GET("/").WithHeader(versioning.AcceptVersionHeaderKey, "").Expect().
|
||||
@@ -52,8 +61,8 @@ func TestVersionAliases(t *testing.T) {
|
||||
|
||||
api := app.Party("/api")
|
||||
api.Use(versioning.Aliases(map[string]string{
|
||||
versioning.Empty: "1",
|
||||
"stage": "2",
|
||||
versioning.Empty: "1.0.0",
|
||||
"stage": "2.0.0",
|
||||
}))
|
||||
|
||||
writeVesion := func(ctx iris.Context) {
|
||||
@@ -61,13 +70,13 @@ func TestVersionAliases(t *testing.T) {
|
||||
}
|
||||
|
||||
// A group without registration order.
|
||||
v3 := versioning.NewGroup(api, ">= 3, < 4")
|
||||
v3 := versioning.NewGroup(api, ">= 3.0.0 < 4.0.0")
|
||||
v3.Get("/", writeVesion)
|
||||
|
||||
v1 := versioning.NewGroup(api, ">= 1, < 2")
|
||||
v1 := versioning.NewGroup(api, ">= 1.0.0 < 2.0.0")
|
||||
v1.Get("/", writeVesion)
|
||||
|
||||
v2 := versioning.NewGroup(api, ">= 2, < 3")
|
||||
v2 := versioning.NewGroup(api, ">= 2.0.0 < 3.0.0")
|
||||
v2.Get("/", writeVesion)
|
||||
|
||||
api.Get("/manual", func(ctx iris.Context) {
|
||||
@@ -84,15 +93,15 @@ func TestVersionAliases(t *testing.T) {
|
||||
e.GET("/api").WithHeader(versioning.AcceptVersionHeaderKey, "").Expect().
|
||||
Status(iris.StatusOK).Body().Equal("1.0.0")
|
||||
// Test NotFound error, aliases are not responsible for that.
|
||||
e.GET("/api").WithHeader(versioning.AcceptVersionHeaderKey, "4").Expect().
|
||||
e.GET("/api").WithHeader(versioning.AcceptVersionHeaderKey, "4.0.0").Expect().
|
||||
Status(iris.StatusNotImplemented).Body().Equal("version not found")
|
||||
// Test "stage" alias.
|
||||
e.GET("/api").WithHeader(versioning.AcceptVersionHeaderKey, "stage").Expect().
|
||||
Status(iris.StatusOK).Body().Equal("2.0.0")
|
||||
// Test version 2.
|
||||
e.GET("/api").WithHeader(versioning.AcceptVersionHeaderKey, "2").Expect().
|
||||
e.GET("/api").WithHeader(versioning.AcceptVersionHeaderKey, "2.0.0").Expect().
|
||||
Status(iris.StatusOK).Body().Equal("2.0.0")
|
||||
// Test version 3 (registered first).
|
||||
e.GET("/api").WithHeader(versioning.AcceptVersionHeaderKey, "3.1").Expect().
|
||||
e.GET("/api").WithHeader(versioning.AcceptVersionHeaderKey, "3.1.0").Expect().
|
||||
Status(iris.StatusOK).Body().Equal("3.1.0")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user