forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmw_context_vars_test.go
112 lines (88 loc) · 2.62 KB
/
mw_context_vars_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/test"
)
func testPreparetContextVarsMiddleware() {
buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.EnableContextVars = true
spec.VersionData.Versions = map[string]apidef.VersionInfo{
"v1": {
UseExtendedPaths: true,
GlobalHeaders: map[string]string{
"X-Static": "foo",
"X-Request-ID": "$tyk_context.request_id",
"X-Path": "$tyk_context.path",
"X-Remote-Addr": "$tyk_context.remote_addr",
},
},
}
})
}
func TestContextVarsMiddleware(t *testing.T) {
ts := newTykTestServer()
defer ts.Close()
testPreparetContextVarsMiddleware()
ts.Run(t, []test.TestCase{
{Path: "/test/path", Code: 200, BodyMatch: `"X-Remote-Addr":"127.0.0.1"`},
{Path: "/test/path", Code: 200, BodyMatch: `"X-Path":"/test/path"`},
{Path: "/test/path", Code: 200, BodyMatch: `"X-Static":"foo"`},
{Path: "/test/path", Code: 200, BodyMatch: `"X-Request-Id":"`},
}...)
}
func BenchmarkContextVarsMiddleware(b *testing.B) {
b.ReportAllocs()
ts := newTykTestServer()
defer ts.Close()
testPreparetContextVarsMiddleware()
for i := 0; i < b.N; i++ {
ts.Run(b, []test.TestCase{
{Path: "/test/path", Code: 200, BodyMatch: `"X-Remote-Addr":"127.0.0.1"`},
{Path: "/test/path", Code: 200, BodyMatch: `"X-Path":"/test/path"`},
{Path: "/test/path", Code: 200, BodyMatch: `"X-Static":"foo"`},
{Path: "/test/path", Code: 200, BodyMatch: `"X-Request-Id":"`},
}...)
}
}
func TestMiddlewareContextVars_ProcessRequest_cookies(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, "/", nil)
res := httptest.NewRecorder()
req.Header.Set("Cookie", "abc=123; def=456")
err, code := (&MiddlewareContextVars{}).ProcessRequest(res, req, nil)
if err != nil {
t.Fatal(err)
}
if code != http.StatusOK {
t.Fatal(errors.New("non 200 status code"))
}
ctx := ctxGetData(req)
if ctx["cookies_abc"].(string) != "123" {
t.Error("abc should be 123")
}
if ctx["cookies_def"].(string) != "456" {
t.Error("def should be 456")
}
if ctx["cookies_ghi"] != nil {
t.Error("ghi should be nil")
}
}
func BenchmarkMiddlewareContextVars_ProcessRequest_cookies(b *testing.B) {
b.ReportAllocs()
req, _ := http.NewRequest(http.MethodGet, "/", nil)
res := httptest.NewRecorder()
req.Header.Set("Cookie", "abc=123; def=456")
for i := 0; i < b.N; i++ {
err, code := (&MiddlewareContextVars{}).ProcessRequest(res, req, nil)
if err != nil {
b.Fatal(err)
}
if code != http.StatusOK {
b.Fatal(errors.New("non 200 status code"))
}
}
}