1
0
mirror of https://github.com/kataras/iris.git synced 2026-03-06 16:35:57 +00:00

add Context.SetVersion helper

Former-commit-id: 605d6c1e78f73b8f2c89bd2dc7ee23f21551d47b
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-05-06 07:17:24 +03:00
parent 24665990ce
commit d19672115b
7 changed files with 48 additions and 18 deletions

View File

@@ -21,10 +21,12 @@ const (
// ctx.Values().Set(versioning.Key, ctx.URLParamDefault("version", "1"))
// ctx.Next()
// }
Key = "iris.api.version" // for use inside the ctx.Values(), not visible by the user.
// NotFound is the key that can be used inside a `Map` or inside `ctx.Values().Set(versioning.Key, versioning.NotFound)`
//
// DEPRECATED: May 06 2020: Use `ctx.SetVersion(ctx.URLParamDefault("version", "1"))` instead.
Key = "iris.api.version"
// NotFound is the key that can be used inside a `Map` or inside `ctx.SetVersion(versioning.NotFound)`
// to tell that a version wasn't found, therefore the not found handler should handle the request instead.
NotFound = Key + ".notfound"
NotFound = "iris.api.version.notfound"
)
// NotFoundHandler is the default version not found handler that
@@ -55,13 +57,14 @@ var NotFoundHandler = func(ctx context.Context) {
// However, the end developer can also set a custom version for a handler via a middleware by using the context's store key
// for versions (see `Key` for further details on that).
func GetVersion(ctx context.Context) string {
// firstly by context store, if manually set-ed by a middleware.
if version := ctx.Values().GetString(Key); version != "" {
// firstly by context store, if manually set by a middleware.
version := ctx.Values().GetString(ctx.Application().ConfigurationReadOnly().GetVersionContextKey())
if version != "" {
return version
}
// secondly by the "Accept-Version" header.
if version := ctx.GetHeader(AcceptVersionHeaderKey); version != "" {
if version = ctx.GetHeader(AcceptVersionHeaderKey); version != "" {
return version
}
@@ -72,7 +75,7 @@ func GetVersion(ctx context.Context) string {
rem := acceptValue[idx:]
startVersion := strings.Index(rem, "=")
if startVersion == -1 || len(rem) < startVersion+1 {
return NotFound
return ""
}
rem = rem[startVersion+1:]
@@ -85,11 +88,11 @@ func GetVersion(ctx context.Context) string {
end = len(rem)
}
if version := rem[:end]; version != "" {
if version = rem[:end]; version != "" {
return version
}
}
}
return NotFound
return ""
}

View File

@@ -17,7 +17,7 @@ func TestGetVersion(t *testing.T) {
app.Get("/", writeVesion)
app.Get("/manual", func(ctx iris.Context) {
ctx.Values().Set(versioning.Key, "11.0.5")
ctx.SetVersion("11.0.5")
ctx.Next()
}, writeVesion)
@@ -36,13 +36,13 @@ func TestGetVersion(t *testing.T) {
// unknown versions.
e.GET("/").WithHeader(versioning.AcceptVersionHeaderKey, "").Expect().
Status(iris.StatusOK).Body().Equal(versioning.NotFound)
Status(iris.StatusOK).Body().Equal("")
e.GET("/").WithHeader(versioning.AcceptHeaderKey, "application/vnd.api+json; version=").Expect().
Status(iris.StatusOK).Body().Equal(versioning.NotFound)
Status(iris.StatusOK).Body().Equal("")
e.GET("/").WithHeader(versioning.AcceptHeaderKey, "application/vnd.api+json; version= ;other=dsa").Expect().
Status(iris.StatusOK).Body().Equal(versioning.NotFound)
Status(iris.StatusOK).Body().Equal("")
e.GET("/").WithHeader(versioning.AcceptHeaderKey, "version=").Expect().
Status(iris.StatusOK).Body().Equal(versioning.NotFound)
Status(iris.StatusOK).Body().Equal("")
e.GET("/manual").Expect().Status(iris.StatusOK).Body().Equal("11.0.5")
}

View File

@@ -43,7 +43,7 @@ func NewMatcher(versions Map) context.Handler {
return func(ctx context.Context) {
versionString := GetVersion(ctx)
if versionString == NotFound {
if versionString == "" || versionString == NotFound {
notFoundHandler(ctx)
return
}
@@ -63,7 +63,7 @@ func NewMatcher(versions Map) context.Handler {
}
// pass the not matched version so the not found handler can have knowedge about it.
// ctx.Values().Set(Key, versionString)
// ctx.SetVersion(versionString)
// or let a manual cal of GetVersion(ctx) do that instead.
notFoundHandler(ctx)
}