Skip to content

Commit

Permalink
TT-13507, added section A of spike test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-tyk committed Nov 18, 2024
1 parent 17020f8 commit 771d1e5
Showing 1 changed file with 291 additions and 49 deletions.
340 changes: 291 additions & 49 deletions gateway/api_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,280 @@ func TestConfigureAuthAndOrgStores(t *testing.T) {
}
}

// section A - start
func TestAPIsHavingShorterSubstringListenPathButLongerCustomDomain(t *testing.T) { //the case that triggered the critical from TT-12873
ts := StartTest(nil)
t.Cleanup(ts.Close)

localClient := test.NewClientLocal()

mockServerA := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"match":"serverA"}`))

Check failure on line 617 in gateway/api_loader_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `w.Write` is not checked (errcheck)
}))
defer mockServerA.Close()

mockServerB := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"match":"serverB"}`))

Check failure on line 623 in gateway/api_loader_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `w.Write` is not checked (errcheck)
}))
defer mockServerB.Close()

t.Run("all httpserver options true", func(t *testing.T) {
globalConf := ts.Gw.GetConfig()
globalConf.EnableCustomDomains = true

globalConf.HttpServerOptions.EnableStrictRoutes = true
globalConf.HttpServerOptions.EnablePathSuffixMatching = true
globalConf.HttpServerOptions.EnablePathPrefixMatching = true

ts.Gw.SetConfig(globalConf)
defer ts.ResetTestConfig()

ts.Gw.BuildAndLoadAPI(
func(spec *APISpec) {
spec.APIID = "api-a"
spec.Proxy.ListenPath = "/test-classic"
spec.Proxy.TargetURL = mockServerA.URL
spec.Domain = "{subdomain:tyktest.io|abc.def.ghi}"
spec.Proxy.DisableStripSlash = true
spec.Proxy.StripListenPath = true
},
func(spec *APISpec) {
spec.APIID = "api-b"
spec.Proxy.ListenPath = "/test-classic-extended"
spec.Proxy.TargetURL = mockServerB.URL
spec.Domain = "{subdomain:tyktest.io}"
spec.Proxy.DisableStripSlash = true
spec.Proxy.StripListenPath = true
},
)

_, _ = ts.Run(t, []test.TestCase{
{Client: localClient, Code: 200, Path: "/test-classic", Domain: "tyktest.io", BodyMatch: `{"match":"serverA"}`},
{Client: localClient, Code: 200, Path: "/test-classic-extended", Domain: "tyktest.io", BodyMatch: `{"match":"serverB"}`},
}...)
})

t.Run("strict routes false", func(t *testing.T) {
globalConf := ts.Gw.GetConfig()
globalConf.EnableCustomDomains = true

globalConf.HttpServerOptions.EnableStrictRoutes = false
globalConf.HttpServerOptions.EnablePathSuffixMatching = true
globalConf.HttpServerOptions.EnablePathPrefixMatching = true

ts.Gw.SetConfig(globalConf)
defer ts.ResetTestConfig()

ts.Gw.BuildAndLoadAPI(
func(spec *APISpec) {
spec.APIID = "api-a"
spec.Proxy.ListenPath = "/test-classic"
spec.Proxy.TargetURL = mockServerA.URL
spec.Domain = "{subdomain:tyktest.io|abc.def.ghi}"
spec.Proxy.DisableStripSlash = true
spec.Proxy.StripListenPath = true
},
func(spec *APISpec) {
spec.APIID = "api-b"
spec.Proxy.ListenPath = "/test-classic-extended"
spec.Proxy.TargetURL = mockServerB.URL
spec.Domain = "{subdomain:tyktest.io}"
spec.Proxy.DisableStripSlash = true
spec.Proxy.StripListenPath = true
},
)

_, _ = ts.Run(t, []test.TestCase{
{Client: localClient, Code: 200, Path: "/test-classic", Domain: "tyktest.io", BodyMatch: `{"match":"serverA"}`},
{Client: localClient, Code: 200, Path: "/test-classic-extended", Domain: "tyktest.io", BodyMatch: `{"match":"serverB"}`},
}...)
})

t.Run(" suffix match false", func(t *testing.T) {
globalConf := ts.Gw.GetConfig()
globalConf.EnableCustomDomains = true

globalConf.HttpServerOptions.EnableStrictRoutes = true
globalConf.HttpServerOptions.EnablePathSuffixMatching = false
globalConf.HttpServerOptions.EnablePathPrefixMatching = true

ts.Gw.SetConfig(globalConf)
defer ts.ResetTestConfig()

ts.Gw.BuildAndLoadAPI(
func(spec *APISpec) {
spec.APIID = "api-a"
spec.Proxy.ListenPath = "/test-classic"
spec.Proxy.TargetURL = mockServerA.URL
spec.Domain = "{subdomain:tyktest.io|abc.def.ghi}"
spec.Proxy.DisableStripSlash = true
spec.Proxy.StripListenPath = true
},
func(spec *APISpec) {
spec.APIID = "api-b"
spec.Proxy.ListenPath = "/test-classic-extended"
spec.Proxy.TargetURL = mockServerB.URL
spec.Domain = "{subdomain:tyktest.io}"
spec.Proxy.DisableStripSlash = true
spec.Proxy.StripListenPath = true
},
)

_, _ = ts.Run(t, []test.TestCase{
{Client: localClient, Code: 200, Path: "/test-classic", Domain: "tyktest.io", BodyMatch: `{"match":"serverA"}`},
{Client: localClient, Code: 200, Path: "/test-classic-extended", Domain: "tyktest.io", BodyMatch: `{"match":"serverB"}`},
}...)
})

t.Run(" prefix match false", func(t *testing.T) {
globalConf := ts.Gw.GetConfig()
globalConf.EnableCustomDomains = true

globalConf.HttpServerOptions.EnableStrictRoutes = true
globalConf.HttpServerOptions.EnablePathSuffixMatching = true
globalConf.HttpServerOptions.EnablePathPrefixMatching = false

ts.Gw.SetConfig(globalConf)
defer ts.ResetTestConfig()

ts.Gw.BuildAndLoadAPI(
func(spec *APISpec) {
spec.APIID = "api-a"
spec.Proxy.ListenPath = "/test-classic"
spec.Proxy.TargetURL = mockServerA.URL
spec.Domain = "{subdomain:tyktest.io|abc.def.ghi}"
spec.Proxy.DisableStripSlash = true
spec.Proxy.StripListenPath = true
},
func(spec *APISpec) {
spec.APIID = "api-b"
spec.Proxy.ListenPath = "/test-classic-extended"
spec.Proxy.TargetURL = mockServerB.URL
spec.Domain = "{subdomain:tyktest.io}"
spec.Proxy.DisableStripSlash = true
spec.Proxy.StripListenPath = true
},
)

_, _ = ts.Run(t, []test.TestCase{
{Client: localClient, Code: 200, Path: "/test-classic", Domain: "tyktest.io", BodyMatch: `{"match":"serverA"}`},
{Client: localClient, Code: 200, Path: "/test-classic-extended", Domain: "tyktest.io", BodyMatch: `{"match":"serverB"}`},
}...)
})

t.Run(" prefix and suffix match false", func(t *testing.T) {
globalConf := ts.Gw.GetConfig()
globalConf.EnableCustomDomains = true

globalConf.HttpServerOptions.EnableStrictRoutes = true
globalConf.HttpServerOptions.EnablePathSuffixMatching = false
globalConf.HttpServerOptions.EnablePathPrefixMatching = false

ts.Gw.SetConfig(globalConf)
defer ts.ResetTestConfig()

ts.Gw.BuildAndLoadAPI(
func(spec *APISpec) {
spec.APIID = "api-a"
spec.Proxy.ListenPath = "/test-classic"
spec.Proxy.TargetURL = mockServerA.URL
spec.Domain = "{subdomain:tyktest.io|abc.def.ghi}"
spec.Proxy.DisableStripSlash = true
spec.Proxy.StripListenPath = true
},
func(spec *APISpec) {
spec.APIID = "api-b"
spec.Proxy.ListenPath = "/test-classic-extended"
spec.Proxy.TargetURL = mockServerB.URL
spec.Domain = "{subdomain:tyktest.io}"
spec.Proxy.DisableStripSlash = true
spec.Proxy.StripListenPath = true
},
)

_, _ = ts.Run(t, []test.TestCase{
{Client: localClient, Code: 200, Path: "/test-classic", Domain: "tyktest.io", BodyMatch: `{"match":"serverA"}`},
{Client: localClient, Code: 200, Path: "/test-classic-extended", Domain: "tyktest.io", BodyMatch: `{"match":"serverB"}`},
}...)
})

t.Run(" strict and suffix false", func(t *testing.T) {
globalConf := ts.Gw.GetConfig()
globalConf.EnableCustomDomains = true

globalConf.HttpServerOptions.EnableStrictRoutes = false
globalConf.HttpServerOptions.EnablePathSuffixMatching = false
globalConf.HttpServerOptions.EnablePathPrefixMatching = true

ts.Gw.SetConfig(globalConf)
defer ts.ResetTestConfig()

ts.Gw.BuildAndLoadAPI(
func(spec *APISpec) {
spec.APIID = "api-a"
spec.Proxy.ListenPath = "/test-classic"
spec.Proxy.TargetURL = mockServerA.URL
spec.Domain = "{subdomain:tyktest.io|abc.def.ghi}"
spec.Proxy.DisableStripSlash = true
spec.Proxy.StripListenPath = true
},
func(spec *APISpec) {
spec.APIID = "api-b"
spec.Proxy.ListenPath = "/test-classic-extended"
spec.Proxy.TargetURL = mockServerB.URL
spec.Domain = "{subdomain:tyktest.io}"
spec.Proxy.DisableStripSlash = true
spec.Proxy.StripListenPath = true
},
)

_, _ = ts.Run(t, []test.TestCase{
{Client: localClient, Code: 200, Path: "/test-classic", Domain: "tyktest.io", BodyMatch: `{"match":"serverA"}`},
{Client: localClient, Code: 200, Path: "/test-classic-extended", Domain: "tyktest.io", BodyMatch: `{"match":"serverB"}`},
}...)
})

t.Run(" strict and prefix false", func(t *testing.T) {
globalConf := ts.Gw.GetConfig()
globalConf.EnableCustomDomains = true

globalConf.HttpServerOptions.EnableStrictRoutes = false
globalConf.HttpServerOptions.EnablePathSuffixMatching = true
globalConf.HttpServerOptions.EnablePathPrefixMatching = false

ts.Gw.SetConfig(globalConf)
defer ts.ResetTestConfig()

ts.Gw.BuildAndLoadAPI(
func(spec *APISpec) {
spec.APIID = "api-a"
spec.Proxy.ListenPath = "/test-classic"
spec.Proxy.TargetURL = mockServerA.URL
spec.Domain = "{subdomain:tyktest.io|abc.def.ghi}"
spec.Proxy.DisableStripSlash = true
spec.Proxy.StripListenPath = true
},
func(spec *APISpec) {
spec.APIID = "api-b"
spec.Proxy.ListenPath = "/test-classic-extended"
spec.Proxy.TargetURL = mockServerB.URL
spec.Domain = "{subdomain:tyktest.io}"
spec.Proxy.DisableStripSlash = true
spec.Proxy.StripListenPath = true
},
)

_, _ = ts.Run(t, []test.TestCase{
{Client: localClient, Code: 200, Path: "/test-classic", Domain: "tyktest.io", BodyMatch: `{"match":"serverA"}`},
{Client: localClient, Code: 200, Path: "/test-classic-extended", Domain: "tyktest.io", BodyMatch: `{"match":"serverB"}`},
}...)
})
}

// section A - end

func TestSortAPISpecs(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -684,6 +958,9 @@ func TestIdenticalDomains(t *testing.T) {
t.Run("With custom domain support", func(t *testing.T) {
globalConf := ts.Gw.GetConfig()
globalConf.EnableCustomDomains = true
globalConf.HttpServerOptions.EnableStrictRoutes = true
globalConf.HttpServerOptions.EnablePathSuffixMatching = true
globalConf.HttpServerOptions.EnablePathPrefixMatching = true
ts.Gw.SetConfig(globalConf)
defer ts.ResetTestConfig()

Expand Down Expand Up @@ -735,6 +1012,11 @@ func TestDifferentDomainsIdenticalListenPaths(t *testing.T) {
t.Run("With custom domain support", func(t *testing.T) {
globalConf := ts.Gw.GetConfig()
globalConf.EnableCustomDomains = true

globalConf.HttpServerOptions.EnableStrictRoutes = true
globalConf.HttpServerOptions.EnablePathSuffixMatching = true
globalConf.HttpServerOptions.EnablePathPrefixMatching = true

ts.Gw.SetConfig(globalConf)
defer ts.ResetTestConfig()

Expand Down Expand Up @@ -786,6 +1068,11 @@ func TestDifferentDomainsWithOneListenPathBeingASubstringOfTheOther(t *testing.T
t.Run("With custom domain support", func(t *testing.T) {
globalConf := ts.Gw.GetConfig()
globalConf.EnableCustomDomains = true

globalConf.HttpServerOptions.EnableStrictRoutes = true
globalConf.HttpServerOptions.EnablePathSuffixMatching = true
globalConf.HttpServerOptions.EnablePathPrefixMatching = true

ts.Gw.SetConfig(globalConf)
defer ts.ResetTestConfig()

Expand Down Expand Up @@ -816,7 +1103,7 @@ func TestDifferentDomainsWithOneListenPathBeingASubstringOfTheOther(t *testing.T
})
}

func TestAPIsHavingShorterSubstringListenPathButLongerCustomDomain(t *testing.T) { //the case that triggered the critical from TT-12873
func TestLongerListenPathHasLongerDomainThanSubstringListenPath(t *testing.T) {
ts := StartTest(nil)
t.Cleanup(ts.Close)

Expand All @@ -837,56 +1124,11 @@ func TestAPIsHavingShorterSubstringListenPathButLongerCustomDomain(t *testing.T)
t.Run("With custom domain support", func(t *testing.T) {
globalConf := ts.Gw.GetConfig()
globalConf.EnableCustomDomains = true
ts.Gw.SetConfig(globalConf)
defer ts.ResetTestConfig()

ts.Gw.BuildAndLoadAPI(
func(spec *APISpec) {
spec.APIID = "api-a"
spec.Proxy.ListenPath = "/test-classic"
spec.Proxy.TargetURL = mockServerA.URL
spec.Domain = "{subdomain:tyktest.io|abc.def.ghi}"
spec.Proxy.DisableStripSlash = true
spec.Proxy.StripListenPath = true
},
func(spec *APISpec) {
spec.APIID = "api-b"
spec.Proxy.ListenPath = "/test-classic-extended"
spec.Proxy.TargetURL = mockServerB.URL
spec.Domain = "{subdomain:tyktest.io}"
spec.Proxy.DisableStripSlash = true
spec.Proxy.StripListenPath = true
},
)

_, _ = ts.Run(t, []test.TestCase{
{Client: localClient, Code: 200, Path: "/test-classic", Domain: "tyktest.io", BodyMatch: `{"match":"serverA"}`},
{Client: localClient, Code: 200, Path: "/test-classic-extended", Domain: "tyktest.io", BodyMatch: `{"match":"serverB"}`},
}...)
})
}
globalConf.HttpServerOptions.EnableStrictRoutes = true
globalConf.HttpServerOptions.EnablePathSuffixMatching = true
globalConf.HttpServerOptions.EnablePathPrefixMatching = true

func TestLongerListenPathHasLongerDomainThanSubstringListenPath(t *testing.T) {
ts := StartTest(nil)
t.Cleanup(ts.Close)

localClient := test.NewClientLocal()

mockServerA := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"match":"serverA"}`))
}))
defer mockServerA.Close()

mockServerB := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"match":"serverB"}`))
}))
defer mockServerB.Close()

t.Run("With custom domain support", func(t *testing.T) {
globalConf := ts.Gw.GetConfig()
globalConf.EnableCustomDomains = true
ts.Gw.SetConfig(globalConf)
defer ts.ResetTestConfig()

Expand Down

0 comments on commit 771d1e5

Please sign in to comment.