Skip to content

Commit

Permalink
Add tests/benchmarks for versioning middleware (#1677)
Browse files Browse the repository at this point in the history
Includes DoMockReply, extends #1614
  • Loading branch information
matiasinsaurralde authored and buger committed May 9, 2018
1 parent 65b9346 commit 9f13654
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 5 deletions.
112 changes: 112 additions & 0 deletions api_definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,115 @@ func testPrepareDefaultVersion() string {
}}
})
}

func TestGetVersionFromRequest(t *testing.T) {
ts := newTykTestServer()
defer ts.Close()

versionInfo := apidef.VersionInfo{}
versionInfo.Paths.WhiteList = []string{"/foo"}
versionInfo.Paths.BlackList = []string{"/bar"}

t.Run("Header location", func(t *testing.T) {
buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.VersionData.NotVersioned = false
spec.VersionDefinition.Location = "header"
spec.VersionDefinition.Key = "X-API-Version"
spec.VersionData.Versions["v1"] = versionInfo
})

ts.Run(t, []test.TestCase{
{Path: "/foo", Code: 200, Headers: map[string]string{"X-API-Version": "v1"}},
{Path: "/bar", Code: 403, Headers: map[string]string{"X-API-Version": "v1"}},
}...)
})

t.Run("URL param location", func(t *testing.T) {
buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.VersionData.NotVersioned = false
spec.VersionDefinition.Location = "url-param"
spec.VersionDefinition.Key = "version"
spec.VersionData.Versions["v2"] = versionInfo
})

ts.Run(t, []test.TestCase{
{Path: "/foo?version=v2", Code: 200},
{Path: "/bar?version=v2", Code: 403},
}...)
})

t.Run("URL location", func(t *testing.T) {
buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.VersionData.NotVersioned = false
spec.VersionDefinition.Location = "url"
spec.VersionData.Versions["v3"] = versionInfo
})

ts.Run(t, []test.TestCase{
{Path: "/v3/foo", Code: 200},
{Path: "/v3/bar", Code: 403},
}...)
})
}

func BenchmarkGetVersionFromRequest(b *testing.B) {
ts := newTykTestServer()
defer ts.Close()

versionInfo := apidef.VersionInfo{}
versionInfo.Paths.WhiteList = []string{"/foo"}
versionInfo.Paths.BlackList = []string{"/bar"}

b.Run("Header location", func(b *testing.B) {
buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.VersionData.NotVersioned = false
spec.VersionDefinition.Location = "header"
spec.VersionDefinition.Key = "X-API-Version"
spec.VersionData.Versions["v1"] = versionInfo
})

for i := 0; i < b.N; i++ {
ts.Run(b, []test.TestCase{
{Path: "/foo", Code: 200, Headers: map[string]string{"X-API-Version": "v1"}},
{Path: "/bar", Code: 403, Headers: map[string]string{"X-API-Version": "v1"}},
}...)
}
})

b.Run("URL param location", func(b *testing.B) {
buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.VersionData.NotVersioned = false
spec.VersionDefinition.Location = "url-param"
spec.VersionDefinition.Key = "version"
spec.VersionData.Versions["v2"] = versionInfo
})

for i := 0; i < b.N; i++ {
ts.Run(b, []test.TestCase{
{Path: "/foo?version=v2", Code: 200},
{Path: "/bar?version=v2", Code: 403},
}...)
}
})

b.Run("URL location", func(b *testing.B) {
buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.VersionData.NotVersioned = false
spec.VersionDefinition.Location = "url"
spec.VersionData.Versions["v3"] = versionInfo
})

for i := 0; i < b.N; i++ {
ts.Run(b, []test.TestCase{
{Path: "/v3/foo", Code: 200},
{Path: "/v3/bar", Code: 403},
}...)
}
})
}
47 changes: 42 additions & 5 deletions mw_version_check_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"net/http"
"testing"

"github.com/TykTechnologies/tyk/apidef"
Expand All @@ -19,17 +20,41 @@ func testPrepareVersioning() (string, string) {
Name: "expired",
Expires: "2006-01-02 15:04",
}
spec.VersionData.Versions["v2"] = apidef.VersionInfo{
Name: "v2",
UseExtendedPaths: true,
ExtendedPaths: apidef.ExtendedPathsSet{
WhiteList: []apidef.EndPointMeta{
{
Path: "/mock",
MethodActions: map[string]apidef.EndpointMethodMeta{
http.MethodGet: {
Action: apidef.Reply,
Code: http.StatusOK,
Data: "testbody",
Headers: map[string]string{"testheader": "testvalue"},
},
},
},
},
Ignored: []apidef.EndPointMeta{
{
Path: "/ignore",
},
},
},
}
})

keyWrongVersion := createSession(func(s *user.SessionState) {
s.AccessRights = map[string]user.AccessDefinition{"test": {
APIID: "test", Versions: []string{"v2"},
APIID: "test", Versions: []string{"v3"},
}}
})

keyKnownVersion := createSession(func(s *user.SessionState) {
s.AccessRights = map[string]user.AccessDefinition{"test": {
APIID: "test", Versions: []string{"v1", "expired"},
APIID: "test", Versions: []string{"v1", "v2", "expired"},
}}
})

Expand All @@ -44,7 +69,7 @@ func TestVersioning(t *testing.T) {

wrongVersionHeaders := map[string]string{
"authorization": keyWrongVersion,
"version": "v2",
"version": "v3",
}

disallowedAccessHeaders := map[string]string{
Expand All @@ -62,11 +87,18 @@ func TestVersioning(t *testing.T) {
"version": "expired",
}

mockVersionHeaders := map[string]string{
"authorization": keyKnownVersion,
"version": "v2",
}

ts.Run(t, []test.TestCase{
{Path: "/", Code: 403, Headers: wrongVersionHeaders, BodyMatch: "This API version does not seem to exist"},
{Path: "/", Code: 403, Headers: disallowedAccessHeaders, BodyMatch: "Access to this API has been disallowed"},
{Path: "/", Code: 200, Headers: knownVersionHeaders},
{Path: "/", Code: 403, Headers: expiredVersionHeaders, BodyMatch: string(VersionExpired)},
{Path: "/mock", Code: 200, Headers: mockVersionHeaders, BodyMatch: "testbody", HeadersMatch: map[string]string{"testheader": "testvalue"}},
{Path: "/ignore", Code: 200, Headers: mockVersionHeaders},
}...)
}

Expand All @@ -77,10 +109,9 @@ func BenchmarkVersioning(b *testing.B) {
defer ts.Close()

keyWrongVersion, keyKnownVersion := testPrepareVersioning()

wrongVersionHeaders := map[string]string{
"authorization": keyWrongVersion,
"version": "v2",
"version": "v3",
}

disallowedAccessHeaders := map[string]string{
Expand All @@ -97,13 +128,19 @@ func BenchmarkVersioning(b *testing.B) {
"authorization": keyKnownVersion,
"version": "expired",
}
mockVersionHeaders := map[string]string{
"authorization": keyKnownVersion,
"version": "v2",
}

for i := 0; i < b.N; i++ {
ts.Run(b, []test.TestCase{
{Path: "/", Code: 403, Headers: wrongVersionHeaders, BodyMatch: "This API version does not seem to exist"},
{Path: "/", Code: 403, Headers: disallowedAccessHeaders, BodyMatch: "Access to this API has been disallowed"},
{Path: "/", Code: 200, Headers: knownVersionHeaders},
{Path: "/", Code: 403, Headers: expiredVersionHeaders, BodyMatch: string(VersionExpired)},
{Path: "/mock", Code: 200, Headers: mockVersionHeaders, BodyMatch: "testbody", HeadersMatch: map[string]string{"testheader": "testvalue"}},
{Path: "/ignore", Code: 200, Headers: mockVersionHeaders},
}...)
}
}

0 comments on commit 9f13654

Please sign in to comment.