github.com/Tyktechnologies/tyk@v2.9.5+incompatible/goplugin/mw_go_plugin_test.go (about) 1 // +build goplugin 2 3 package goplugin_test 4 5 import ( 6 "context" 7 "net/http" 8 "os" 9 "testing" 10 "time" 11 12 "github.com/TykTechnologies/tyk/apidef" 13 "github.com/TykTechnologies/tyk/gateway" 14 "github.com/TykTechnologies/tyk/test" 15 ) 16 17 func TestMain(m *testing.M) { 18 os.Exit(gateway.InitTestMain(context.Background(), m)) 19 } 20 21 // TestGoPluginMWs tests all possible Go-plugin MWs ("pre", "auth_check", "post_key_auth" and "post") 22 // Please see ./test/goplugins/test_goplugins.go for plugin implementation details 23 func TestGoPluginMWs(t *testing.T) { 24 ts := gateway.StartTest() 25 defer ts.Close() 26 27 gateway.BuildAndLoadAPI(func(spec *gateway.APISpec) { 28 spec.APIID = "plugin_api" 29 spec.Proxy.ListenPath = "/goplugin" 30 spec.UseKeylessAccess = false 31 spec.UseStandardAuth = false 32 spec.UseGoPluginAuth = true 33 spec.CustomMiddleware = apidef.MiddlewareSection{ 34 Driver: apidef.GoPluginDriver, 35 Pre: []apidef.MiddlewareDefinition{ 36 { 37 Name: "MyPluginPre", 38 Path: "../test/goplugins/goplugins.so", 39 }, 40 }, 41 AuthCheck: apidef.MiddlewareDefinition{ 42 Name: "MyPluginAuthCheck", 43 Path: "../test/goplugins/goplugins.so", 44 }, 45 PostKeyAuth: []apidef.MiddlewareDefinition{ 46 { 47 Name: "MyPluginPostKeyAuth", 48 Path: "../test/goplugins/goplugins.so", 49 }, 50 }, 51 Post: []apidef.MiddlewareDefinition{ 52 { 53 Name: "MyPluginPost", 54 Path: "../test/goplugins/goplugins.so", 55 }, 56 }, 57 } 58 }) 59 60 time.Sleep(1 * time.Second) 61 62 t.Run("Run Go-plugin auth failed", func(t *testing.T) { 63 ts.Run(t, []test.TestCase{ 64 { 65 Path: "/goplugin/plugin_hit", 66 Headers: map[string]string{"Authorization": "invalid_token"}, 67 HeadersMatch: map[string]string{ 68 "X-Auth-Result": "failed", 69 }, 70 Code: http.StatusForbidden, 71 }, 72 }...) 73 }) 74 75 t.Run("Run Go-plugin all middle-wares", func(t *testing.T) { 76 ts.Run(t, []test.TestCase{ 77 { 78 Path: "/goplugin/plugin_hit", 79 Headers: map[string]string{"Authorization": "abc"}, 80 Code: http.StatusOK, 81 HeadersMatch: map[string]string{ 82 "X-Initial-URI": "/goplugin/plugin_hit", 83 "X-Auth-Result": "OK", 84 "X-Session-Alias": "abc-session", 85 }, 86 BodyMatch: `"message":"post message"`, 87 }, 88 { 89 Method: "DELETE", 90 Path: "/tyk/keys/abc", 91 AdminAuth: true, 92 Code: http.StatusOK, 93 BodyMatch: `"action":"deleted"`}, 94 }...) 95 }) 96 }