forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_context_vars_test.go
96 lines (83 loc) · 2.08 KB
/
middleware_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
package main
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
const contextVarsMiddlewareDefinition = `{
"name": "Tyk Test API",
"api_id": "1",
"org_id": "default",
"definition": {
"location": "header",
"key": "version"
},
"auth": {
"auth_header_name": "authorization"
},
"version_data": {
"not_versioned": true,
"versions": {
"v1": {
"name": "v1",
"expires": "2100-01-02 15:04",
"use_extended_paths": true,
"paths": {
"ignored": [],
"white_list": [],
"black_list": []
},
"global_headers":{
"X-Static": "foo",
"X-Request-ID":"$tyk_context.request_id",
"X-Path": "$tyk_context.path",
"X-Remote-Addr": "$tyk_context.remote_addr"
}
}
}
},
"proxy": {
"listen_path": "/v1",
"target_url": "` + testHttpAny + `",
"strip_listen_path": false
},
"enable_context_vars": true
}`
func createContextVarsSampleAPI(t *testing.T, apiTestDef string) *APISpec {
spec := createSpecTest(t, apiTestDef)
loadApps([]*APISpec{spec}, discardMuxer)
return spec
}
func TestContextVarsMiddleware(t *testing.T) {
spec := createContextVarsSampleAPI(t, contextVarsMiddlewareDefinition)
session := createNonThrottledSession()
spec.SessionManager.UpdateSession("1234wer", session, 60)
uri := "/test/this/path"
method := "GET"
recorder := httptest.NewRecorder()
param := make(url.Values)
req, err := http.NewRequest(method, uri+param.Encode(), nil)
req.RemoteAddr = "127.0.0.1:80"
req.Header.Add("authorization", "1234wer")
if err != nil {
t.Fatal(err)
}
chain := getChain(spec)
chain.ServeHTTP(recorder, req)
if recorder.Code != 200 {
t.Fatal("Invalid response code, should be 200: \n", recorder.Code, recorder.Body)
}
if req.Header.Get("X-Static") == "" {
t.Fatal("Sanity check failed: could not find static const in header")
}
if req.Header.Get("X-Path") == "" {
t.Fatal("Could not find Path in header")
}
if req.Header.Get("X-Remote-Addr") == "" {
t.Fatal("Could not find Remote-Addr in header")
}
if req.Header.Get("X-Request-ID") == "" {
t.Fatal("Could not find Correlation ID in header")
}
}