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

add versioning/README.md

Former-commit-id: 7ea92fadc982038533675996704b6bf89e149aae
This commit is contained in:
Gerasimos (Makis) Maropoulos
2018-11-11 02:18:19 +02:00
parent fc9e5b3c05
commit 70610af6fd
3 changed files with 131 additions and 23 deletions

View File

@@ -23,6 +23,14 @@ func sendHandler(contents string) iris.Handler {
}
}
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")
}
}
func TestNewMatcher(t *testing.T) {
app := iris.New()
@@ -33,6 +41,17 @@ func TestNewMatcher(t *testing.T) {
versioning.NotFound: notFoundHandler,
}))
// 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))
e := httptest.New(t, app)
e.GET("/api/user").WithHeader(versioning.AcceptVersionHeaderKey, "1").Expect().
@@ -44,32 +63,17 @@ func TestNewMatcher(t *testing.T) {
e.GET("/api/user").WithHeader(versioning.AcceptVersionHeaderKey, "2.9.9").Expect().
Status(iris.StatusOK).Body().Equal(v2Response)
// 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")
e.GET("/api/user").WithHeader(versioning.AcceptVersionHeaderKey, "3.0").Expect().
Status(iris.StatusNotFound).Body().Equal("Not Found")
}
func TestNewGroup(t *testing.T) {
app := iris.New()
// userAPI := app.Party("/api/user")
// userAPIV10 := versioning.NewGroup("1.0", userAPI)
// userAPIV10.Get("/", sendHandler(v10Response))
// userAPIV2 := versioning.NewGroup(">= 2, < 3", userAPI)
// userAPIV2.Get("/", sendHandler(v2Response))
// ---
// userAPI := app.Party("/api/user")
// userVAPI := versioning.NewGroup(userAPI)
// userAPIV10 := userVAPI.Version("1.0")
// userAPIV10.Get("/", sendHandler(v10Response))
// userAPIV10 := userVAPI.Version("2.0")
// userAPIV10.Get("/", sendHandler(v10Response))
// userVAPI.NotFound(...)
// userVAPI.Build()
// --
userAPI := app.Party("/api/user")
// [... static serving, middlewares and etc goes here].