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