forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mw_context_vars_test.go
76 lines (65 loc) · 1.64 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
package main
import (
"net/http/httptest"
"net/url"
"testing"
)
const contextVarsMiddlewareDefinition = `{
"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",
"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 + `"
},
"enable_context_vars": true
}`
func TestContextVarsMiddleware(t *testing.T) {
spec := createSpecTest(t, contextVarsMiddlewareDefinition)
session := createNonThrottledSession()
spec.SessionManager.UpdateSession("1234wer", session, 60)
uri := "/test/this/path"
method := "GET"
recorder := httptest.NewRecorder()
param := make(url.Values)
req := testReq(t, method, uri+param.Encode(), nil)
req.RemoteAddr = "127.0.0.1:80"
req.Header.Set("authorization", "1234wer")
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")
}
}