Skip to content

Commit 9a2561a

Browse files
authored
Merging to release-5.8: Tyk OAS API definition is not available to Response Plugin if no Request Plugin loaded (#7064)
Tyk OAS API definition is not available to Response Plugin if no Request Plugin loaded (#7064) ### **User description** <!-- Provide a general summary of your changes in the Title above --> ## Description Couple of tests were added to meet acceptance criteria ## Related Issue <!-- This project only accepts pull requests related to open issues. --> <!-- If suggesting a new feature or change, please discuss it in an issue first. --> <!-- If fixing a bug, there should be an issue describing it with steps to reproduce. --> <!-- OSS: Please link to the issue here. Tyk: please create/link the JIRA ticket. --> ## Motivation and Context <!-- Why is this change required? What problem does it solve? --> ## How This Has Been Tested <!-- Please describe in detail how you tested your changes --> <!-- Include details of your testing environment, and the tests --> <!-- you ran to see how your change affects other areas of the code, etc. --> <!-- This information is helpful for reviewers and QA. --> ## Screenshots (if appropriate) ## Types of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Refactoring or add test (improvements in base code or adds test coverage to functionality) ## Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply --> <!-- If there are no documentation updates required, mark the item as checked. --> <!-- Raise up any additional concerns not covered by the checklist. --> - [ ] I ensured that the documentation is up to date - [ ] I explained why this PR updates go.mod in detail with reasoning why it's required - [ ] I would like a code coverage CI quality gate exception and have explained why ___ ### **PR Type** Tests ___ ### **Description** - Added acceptance tests for Go-plugin OAS API access in response plugins - Tested standalone, chained, and combined request/response plugin scenarios - Enhanced test plugins to set additional headers for OAS API info - Implemented new response plugin for chained middleware validation ___ ### **Changes walkthrough** 📝 <table><thead><tr><th></th><th align="left">Relevant files</th></tr></thead><tbody><tr><td><strong>Tests</strong></td><td><table> <tr> <td> <details> <summary><strong>mw_go_plugin_test.go</strong><dd><code>Add acceptance tests for Go-plugin OAS API in response plugins</code></dd></summary> <hr> goplugin/mw_go_plugin_test.go <li>Added three new subtests for Go-plugin OAS API access in response <br>plugins<br> <li> Tested standalone response plugin, chained response plugins, and <br>combined request/response plugins<br> <li> Validated correct OAS API info propagation via custom headers<br> <li> Updated test paths and header assertions for new scenarios </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/7064/files#diff-0ad6c75c29b2656d9d5cfa9108d32a3b242c339c1688ce168516ed213a5f482b">+92/-17</a>&nbsp; </td> </tr> <tr> <td> <details> <summary><strong>test_goplugin.go</strong><dd><code>Enhance test plugins for OAS API header assertions</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary> <hr> test/goplugins/test_goplugin.go <li>Removed unused XOASDocTitle constant<br> <li> Enhanced MyPluginAccessingOASAPI and MyResponsePluginAccessingOASAPI <br>to set extra headers<br> <li> Added new MyResponsePluginAccessingOASAPI2 for chained response plugin <br>testing </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/7064/files#diff-6b57b162c0610abdd8c4edf02dab0718ef7daa1c986aeae2e13adf9904ec3459">+12/-7</a>&nbsp; &nbsp; </td> </tr> </table></td></tr></tr></tbody></table> ___ > <details> <summary> Need help?</summary><li>Type <code>/help how to ...</code> in the comments thread for any questions about PR-Agent usage.</li><li>Check out the <a href="https://qodo-merge-docs.qodo.ai/usage-guide/">documentation</a> for more information.</li></details>
1 parent 974acb7 commit 9a2561a

File tree

3 files changed

+105
-24
lines changed

3 files changed

+105
-24
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
.vagrant/
44
.venv/
55
.vscode/
6+
.air.toml
67
coverage/
78
temp/
9+
/tmp
810
/middleware/bundles
911
testapps/
1012

goplugin/mw_go_plugin_test.go

Lines changed: 91 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -574,31 +574,105 @@ func TestGoPlugin_MyResponsePluginAccessingOASAPI(t *testing.T) {
574574

575575
require.NoError(t, err)
576576

577-
ts.Gw.BuildAndLoadAPI(func(spec *gateway.APISpec) {
578-
spec.IsOAS = true
579-
spec.OAS = oasDoc
580-
spec.Proxy.ListenPath = "/goplugin-response"
581-
spec.UseKeylessAccess = true
582-
spec.UseStandardAuth = false
583-
spec.UseGoPluginAuth = false
584-
spec.CustomMiddleware = apidef.MiddlewareSection{
585-
Driver: apidef.GoPluginDriver,
586-
Response: []apidef.MiddlewareDefinition{
587-
{
588-
Name: "MyResponsePluginAccessingOASAPI",
589-
Path: goPluginFilename(),
577+
t.Run("Run Go-plugin on standalone response plugin", func(t *testing.T) {
578+
ts.Gw.BuildAndLoadAPI(func(spec *gateway.APISpec) {
579+
spec.IsOAS = true
580+
spec.OAS = oasDoc
581+
spec.Proxy.ListenPath = "/goplugin/standalone_response_plugin"
582+
spec.UseKeylessAccess = true
583+
spec.UseStandardAuth = false
584+
spec.UseGoPluginAuth = false
585+
spec.CustomMiddleware = apidef.MiddlewareSection{
586+
Driver: apidef.GoPluginDriver,
587+
Response: []apidef.MiddlewareDefinition{
588+
{
589+
Name: "MyResponsePluginAccessingOASAPI",
590+
Path: goPluginFilename(),
591+
},
592+
},
593+
}
594+
})
595+
596+
ts.Run(t, []test.TestCase{
597+
{
598+
Path: "/goplugin/standalone_response_plugin/plugin_hit",
599+
Code: http.StatusOK,
600+
HeadersMatch: map[string]string{
601+
"X-OAS-Doc-Title": oasDoc.Info.Title,
590602
},
591603
},
592-
}
604+
}...)
593605
})
594606

595-
t.Run("Run Go-plugin all middle-wares", func(t *testing.T) {
607+
t.Run("request-pre and response plugins chained work good", func(t *testing.T) {
608+
ts.Gw.BuildAndLoadAPI(func(spec *gateway.APISpec) {
609+
spec.IsOAS = true
610+
spec.OAS = oasDoc
611+
spec.Proxy.ListenPath = "/goplugin/request_and_response_plugin"
612+
spec.UseKeylessAccess = true
613+
spec.UseStandardAuth = false
614+
spec.UseGoPluginAuth = false
615+
spec.CustomMiddleware = apidef.MiddlewareSection{
616+
Driver: apidef.GoPluginDriver,
617+
Pre: []apidef.MiddlewareDefinition{
618+
{
619+
Name: "MyPluginAccessingOASAPI",
620+
Path: goPluginFilename(),
621+
},
622+
},
623+
Response: []apidef.MiddlewareDefinition{
624+
{
625+
Name: "MyResponsePluginAccessingOASAPI",
626+
Path: goPluginFilename(),
627+
},
628+
},
629+
}
630+
})
631+
596632
ts.Run(t, []test.TestCase{
597633
{
598-
Path: "/goplugin-response/plugin_hit",
634+
Path: "/goplugin/request_and_response_plugin/plugin_hit",
599635
Code: http.StatusOK,
600636
HeadersMatch: map[string]string{
601-
"X-OAS-Doc-Title": oasDoc.Info.Title,
637+
"X-OAS-Doc-Title": oasDoc.Info.Title,
638+
"X-My-Plugin-Accessing-OAS-API": oasDoc.Info.Title,
639+
"X-My-Response-Plugin-Accessing-OAS-API": oasDoc.Info.Title,
640+
},
641+
},
642+
}...)
643+
})
644+
645+
t.Run("chained response plugin work fine", func(t *testing.T) {
646+
ts.Gw.BuildAndLoadAPI(func(spec *gateway.APISpec) {
647+
spec.IsOAS = true
648+
spec.OAS = oasDoc
649+
spec.Proxy.ListenPath = "/goplugin/request_chaining"
650+
spec.UseKeylessAccess = true
651+
spec.UseStandardAuth = false
652+
spec.UseGoPluginAuth = false
653+
spec.CustomMiddleware = apidef.MiddlewareSection{
654+
Driver: apidef.GoPluginDriver,
655+
Response: []apidef.MiddlewareDefinition{
656+
{
657+
Name: "MyResponsePluginAccessingOASAPI",
658+
Path: goPluginFilename(),
659+
},
660+
{
661+
Name: "MyResponsePluginAccessingOASAPI2",
662+
Path: goPluginFilename(),
663+
},
664+
},
665+
}
666+
})
667+
668+
ts.Run(t, []test.TestCase{
669+
{
670+
Path: "/goplugin/request_chaining/plugin_hit",
671+
Code: http.StatusOK,
672+
HeadersMatch: map[string]string{
673+
"X-OAS-Doc-Title": oasDoc.Info.Title,
674+
"X-My-Response-Plugin-Accessing-OAS-API-2": oasDoc.Info.Title,
675+
"X-My-Response-Plugin-Accessing-OAS-API": oasDoc.Info.Title,
602676
},
603677
},
604678
}...)

test/goplugins/test_goplugin.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ import (
1616
"github.com/TykTechnologies/tyk/user"
1717
)
1818

19-
const (
20-
XOASDocTitle = "X-OAS-Doc-Title"
21-
)
22-
2319
// MyPluginPre checks if session is NOT present, adds custom header
2420
// with initial URI path and will be used as "pre" custom MW
2521
func MyPluginPre(rw http.ResponseWriter, r *http.Request) {
@@ -200,13 +196,22 @@ func MyAnalyticsPluginMaskJSONLoginBody(record *analytics.AnalyticsRecord) {
200196

201197
func MyPluginAccessingOASAPI(rw http.ResponseWriter, r *http.Request) {
202198
oas := ctx.GetOASDefinition(r)
203-
rw.Header().Add(XOASDocTitle, oas.Info.Title)
199+
rw.Header().Add("X-OAS-Doc-Title", oas.Info.Title)
200+
rw.Header().Add("X-My-Plugin-Accessing-OAS-API", oas.Info.Title)
204201
}
205202

206-
// MyResponsePluginAccessingOASAPI fake
203+
// MyResponsePluginAccessingOASAPI fake plugin which modifies data
207204
func MyResponsePluginAccessingOASAPI(rw http.ResponseWriter, _ *http.Response, req *http.Request) {
208205
oas := ctx.GetOASDefinition(req)
209-
rw.Header().Add(XOASDocTitle, oas.Info.Title)
206+
rw.Header().Add("X-OAS-Doc-Title", oas.Info.Title)
207+
rw.Header().Add("X-My-Response-Plugin-Accessing-OAS-API", oas.Info.Title)
208+
}
209+
210+
// MyResponsePluginAccessingOASAPI2 fake plugin which modifies headers
211+
func MyResponsePluginAccessingOASAPI2(rw http.ResponseWriter, _ *http.Response, req *http.Request) {
212+
oas := ctx.GetOASDefinition(req)
213+
rw.Header().Add("X-OAS-Doc-Title", oas.Info.Title)
214+
rw.Header().Add("X-My-Response-Plugin-Accessing-OAS-API-2", oas.Info.Title)
210215
}
211216

212217
func MyPluginReturningError(rw http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)