2018-11-10 02:49:32 +01:00
|
|
|
package versioning_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
|
|
|
"github.com/kataras/iris/v12/httptest"
|
|
|
|
"github.com/kataras/iris/v12/versioning"
|
2018-11-10 02:49:32 +01:00
|
|
|
)
|
|
|
|
|
2021-01-07 03:14:41 +01:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-10 02:49:32 +01:00
|
|
|
func TestGetVersion(t *testing.T) {
|
|
|
|
app := iris.New()
|
|
|
|
|
|
|
|
writeVesion := func(ctx iris.Context) {
|
|
|
|
ctx.WriteString(versioning.GetVersion(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Get("/", writeVesion)
|
|
|
|
app.Get("/manual", func(ctx iris.Context) {
|
2020-06-20 08:39:42 +02:00
|
|
|
versioning.SetVersion(ctx, "11.0.5")
|
2018-11-10 02:49:32 +01:00
|
|
|
ctx.Next()
|
|
|
|
}, writeVesion)
|
|
|
|
|
|
|
|
e := httptest.New(t, app)
|
|
|
|
|
2021-01-07 03:14:41 +01:00
|
|
|
e.GET("/").WithHeader(versioning.AcceptVersionHeaderKey, "1.0.0").Expect().
|
2023-07-08 01:08:18 +02:00
|
|
|
Status(iris.StatusOK).Body().IsEqual("1.0.0")
|
2021-01-07 03:14:41 +01:00
|
|
|
e.GET("/").WithHeader(versioning.AcceptHeaderKey, "application/vnd.api+json; version=2.1.0").Expect().
|
2023-07-08 01:08:18 +02:00
|
|
|
Status(iris.StatusOK).Body().IsEqual("2.1.0")
|
2021-01-07 03:14:41 +01:00
|
|
|
e.GET("/").WithHeader(versioning.AcceptHeaderKey, "application/vnd.api+json; version=2.1.0 ;other=dsa").Expect().
|
2023-07-08 01:08:18 +02:00
|
|
|
Status(iris.StatusOK).Body().IsEqual("2.1.0")
|
2021-01-07 03:14:41 +01:00
|
|
|
e.GET("/").WithHeader(versioning.AcceptHeaderKey, "version=2.1.0").Expect().
|
2023-07-08 01:08:18 +02:00
|
|
|
Status(iris.StatusOK).Body().IsEqual("2.1.0")
|
2021-01-07 03:14:41 +01:00
|
|
|
e.GET("/").WithHeader(versioning.AcceptHeaderKey, "version=1.0.0").Expect().
|
2023-07-08 01:08:18 +02:00
|
|
|
Status(iris.StatusOK).Body().IsEqual("1.0.0")
|
2018-11-10 02:49:32 +01:00
|
|
|
|
|
|
|
// unknown versions.
|
|
|
|
e.GET("/").WithHeader(versioning.AcceptVersionHeaderKey, "").Expect().
|
2023-07-08 01:08:18 +02:00
|
|
|
Status(iris.StatusOK).Body().IsEqual("")
|
2018-11-10 02:49:32 +01:00
|
|
|
e.GET("/").WithHeader(versioning.AcceptHeaderKey, "application/vnd.api+json; version=").Expect().
|
2023-07-08 01:08:18 +02:00
|
|
|
Status(iris.StatusOK).Body().IsEqual("")
|
2018-11-10 02:49:32 +01:00
|
|
|
e.GET("/").WithHeader(versioning.AcceptHeaderKey, "application/vnd.api+json; version= ;other=dsa").Expect().
|
2023-07-08 01:08:18 +02:00
|
|
|
Status(iris.StatusOK).Body().IsEqual("")
|
2018-11-10 02:49:32 +01:00
|
|
|
e.GET("/").WithHeader(versioning.AcceptHeaderKey, "version=").Expect().
|
2023-07-08 01:08:18 +02:00
|
|
|
Status(iris.StatusOK).Body().IsEqual("")
|
2018-11-10 02:49:32 +01:00
|
|
|
|
2023-07-08 01:08:18 +02:00
|
|
|
e.GET("/manual").Expect().Status(iris.StatusOK).Body().IsEqual("11.0.5")
|
2018-11-10 02:49:32 +01:00
|
|
|
}
|
2021-01-06 00:52:39 +01:00
|
|
|
|
|
|
|
func TestVersionAliases(t *testing.T) {
|
|
|
|
app := iris.New()
|
|
|
|
|
|
|
|
api := app.Party("/api")
|
|
|
|
api.Use(versioning.Aliases(map[string]string{
|
2021-01-07 03:14:41 +01:00
|
|
|
versioning.Empty: "1.0.0",
|
|
|
|
"stage": "2.0.0",
|
2021-01-06 00:52:39 +01:00
|
|
|
}))
|
|
|
|
|
|
|
|
writeVesion := func(ctx iris.Context) {
|
|
|
|
ctx.WriteString(versioning.GetVersion(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
// A group without registration order.
|
2021-01-07 03:14:41 +01:00
|
|
|
v3 := versioning.NewGroup(api, ">= 3.0.0 < 4.0.0")
|
2021-01-06 00:52:39 +01:00
|
|
|
v3.Get("/", writeVesion)
|
|
|
|
|
2021-01-07 03:14:41 +01:00
|
|
|
v1 := versioning.NewGroup(api, ">= 1.0.0 < 2.0.0")
|
2021-01-06 00:52:39 +01:00
|
|
|
v1.Get("/", writeVesion)
|
|
|
|
|
2021-01-07 03:14:41 +01:00
|
|
|
v2 := versioning.NewGroup(api, ">= 2.0.0 < 3.0.0")
|
2021-01-06 00:52:39 +01:00
|
|
|
v2.Get("/", writeVesion)
|
|
|
|
|
|
|
|
api.Get("/manual", func(ctx iris.Context) {
|
|
|
|
versioning.SetVersion(ctx, "12.0.0")
|
|
|
|
ctx.Next()
|
|
|
|
}, writeVesion)
|
|
|
|
|
|
|
|
e := httptest.New(t, app)
|
|
|
|
|
|
|
|
// Make sure the SetVersion still works.
|
2023-07-08 01:08:18 +02:00
|
|
|
e.GET("/api/manual").Expect().Status(iris.StatusOK).Body().IsEqual("12.0.0")
|
2021-01-06 00:52:39 +01:00
|
|
|
|
|
|
|
// Test Empty default.
|
|
|
|
e.GET("/api").WithHeader(versioning.AcceptVersionHeaderKey, "").Expect().
|
2023-07-08 01:08:18 +02:00
|
|
|
Status(iris.StatusOK).Body().IsEqual("1.0.0")
|
2021-01-06 00:52:39 +01:00
|
|
|
// Test NotFound error, aliases are not responsible for that.
|
2021-01-07 03:14:41 +01:00
|
|
|
e.GET("/api").WithHeader(versioning.AcceptVersionHeaderKey, "4.0.0").Expect().
|
2023-07-08 01:08:18 +02:00
|
|
|
Status(iris.StatusNotImplemented).Body().IsEqual("version not found")
|
2021-01-06 00:52:39 +01:00
|
|
|
// Test "stage" alias.
|
|
|
|
e.GET("/api").WithHeader(versioning.AcceptVersionHeaderKey, "stage").Expect().
|
2023-07-08 01:08:18 +02:00
|
|
|
Status(iris.StatusOK).Body().IsEqual("2.0.0")
|
2021-01-06 00:52:39 +01:00
|
|
|
// Test version 2.
|
2021-01-07 03:14:41 +01:00
|
|
|
e.GET("/api").WithHeader(versioning.AcceptVersionHeaderKey, "2.0.0").Expect().
|
2023-07-08 01:08:18 +02:00
|
|
|
Status(iris.StatusOK).Body().IsEqual("2.0.0")
|
2021-01-06 00:52:39 +01:00
|
|
|
// Test version 3 (registered first).
|
2021-01-07 03:14:41 +01:00
|
|
|
e.GET("/api").WithHeader(versioning.AcceptVersionHeaderKey, "3.1.0").Expect().
|
2023-07-08 01:08:18 +02:00
|
|
|
Status(iris.StatusOK).Body().IsEqual("3.1.0")
|
2021-01-06 00:52:39 +01:00
|
|
|
}
|