forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coprocess_python_test.go
66 lines (55 loc) · 1.54 KB
/
coprocess_python_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// +build coprocess
// +build python
package main
import (
"testing"
"github.com/TykTechnologies/tyk/test"
)
var pythonBundleWithAuthCheck = map[string]string{
"manifest.json": `
{
"file_list": [
"middleware.py"
],
"custom_middleware": {
"driver": "python",
"auth_check": {
"name": "MyAuthHook"
}
}
}
`,
"middleware.py": `
from tyk.decorators import *
from gateway import TykGateway as tyk
@Hook
def MyAuthHook(request, session, metadata, spec):
print("MyAuthHook is called")
auth_header = request.get_header('Authorization')
if auth_header == 'valid_token':
session.rate = 1000.0
session.per = 1.0
metadata["token"] = "valid_token"
return request, session, metadata
`,
}
func TestPythonBundles(t *testing.T) {
ts := newTykTestServer()
defer ts.Close()
bundleID := registerBundle("python_with_auth_check", pythonBundleWithAuthCheck)
t.Run("Single-file bundle with authentication hook", func(t *testing.T) {
buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/test-api/"
spec.UseKeylessAccess = false
spec.EnableCoProcessAuth = true
spec.CustomMiddlewareBundle = bundleID
spec.VersionData.NotVersioned = true
})
validAuth := map[string]string{"Authorization": "valid_token"}
invalidAuth := map[string]string{"Authorization": "invalid_token"}
ts.Run(t, []test.TestCase{
{Path: "/test-api/", Code: 200, Headers: validAuth},
{Path: "/test-api/", Code: 403, Headers: invalidAuth},
}...)
})
}