forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coprocess_test_helpers.go
176 lines (135 loc) · 4.23 KB
/
coprocess_test_helpers.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// +build coprocess
// +build !python
// +build !lua
// +build !grpc
package main
/*
#include <stdio.h>
#include <stdlib.h>
#include "coprocess/api.h"
void applyTestHooks();
static int TestMessageLength(struct CoProcessMessage* object) {
return object->length;
}
static struct CoProcessMessage* TestDispatchHook(struct CoProcessMessage* object) {
struct CoProcessMessage* outputObject = malloc(sizeof *outputObject);
outputObject->p_data = object->p_data;
outputObject->length = object->length;
applyTestHooks(outputObject);
return outputObject;
};
*/
import "C"
import (
"strings"
"unsafe"
"github.com/golang/protobuf/proto"
"github.com/TykTechnologies/tyk/coprocess"
)
var CoProcessReload = make(chan bool)
var CoProcessDispatchEvent = make(chan []byte)
type TestDispatcher struct {
coprocess.Dispatcher
reloaded bool
}
/* Basic CoProcessDispatcher functions */
func (d *TestDispatcher) Dispatch(objectPtr unsafe.Pointer) unsafe.Pointer {
object := (*C.struct_CoProcessMessage)(objectPtr)
newObjectPtr := C.TestDispatchHook(object)
return unsafe.Pointer(newObjectPtr)
}
func (d *TestDispatcher) DispatchEvent(eventJSON []byte) {
CoProcessDispatchEvent <- eventJSON
}
func (d *TestDispatcher) Reload() {
d.reloaded = true
}
/* General test helpers */
func NewCoProcessDispatcher() (dispatcher *TestDispatcher, err error) {
d := &TestDispatcher{}
GlobalDispatcher = d
EnableCoProcess = true
return d, nil
}
func (d *TestDispatcher) ToCoProcessMessage(object *coprocess.Object) unsafe.Pointer {
objectMsg, _ := proto.Marshal(object)
objectMsgStr := string(objectMsg)
CObjectStr := C.CString(objectMsgStr)
messagePtr := (*C.struct_CoProcessMessage)(C.malloc(C.size_t(unsafe.Sizeof(C.struct_CoProcessMessage{}))))
messagePtr.p_data = unsafe.Pointer(CObjectStr)
messagePtr.length = C.int(len(objectMsg))
return unsafe.Pointer(messagePtr)
}
func (d *TestDispatcher) ToCoProcessObject(messagePtr unsafe.Pointer) *coprocess.Object {
message := (*C.struct_CoProcessMessage)(messagePtr)
object := &coprocess.Object{}
objectBytes := C.GoBytes(message.p_data, message.length)
proto.Unmarshal(objectBytes, object)
return object
}
func (d *TestDispatcher) TestMessageLength(messagePtr unsafe.Pointer) int {
message := (*C.struct_CoProcessMessage)(messagePtr)
return int(C.TestMessageLength(message))
}
func TestTykStoreData(key, value string, ttl int) {
Ckey := C.CString(key)
Cvalue := C.CString(value)
Cttl := C.int(ttl)
TykStoreData(Ckey, Cvalue, Cttl)
}
func TestTykGetData(key string) string {
Ckey := C.CString(key)
Cvalue := TykGetData(Ckey)
return C.GoString(Cvalue)
}
/* Events */
func TestTykTriggerEvent(eventName, eventPayload string) {
CeventName := C.CString(eventName)
CeventPayload := C.CString(eventPayload)
TykTriggerEvent(CeventName, CeventPayload)
}
/* Middleware */
//export applyTestHooks
func applyTestHooks(objectPtr unsafe.Pointer) {
objectStruct := (*C.struct_CoProcessMessage)(objectPtr)
objectBytes := C.GoBytes(objectStruct.p_data, objectStruct.length)
object := &coprocess.Object{}
proto.Unmarshal(objectBytes, object)
if strings.Index(object.HookName, "hook_test") != 0 {
return
}
switch object.HookName {
case "hook_test_object_postprocess":
object.Request.SetHeaders = map[string]string{
"test": "value",
}
object.Request.DeleteHeaders = []string{"Deletethisheader"}
object.Request.AddParams = map[string]string{
"customparam": "customvalue",
}
object.Request.DeleteParams = []string{"remove"}
case "hook_test_bad_auth":
object.Request.ReturnOverrides = &coprocess.ReturnOverrides{
ResponseCode: 403,
ResponseError: "Key not authorised",
}
case "hook_test_return_overrides":
object.Request.ReturnOverrides = &coprocess.ReturnOverrides{
Headers: map[string]string{
"header": "value",
},
ResponseCode: 200,
ResponseError: "body",
}
case "hook_test_bad_auth_using_id_extractor":
case "hook_test_bad_auth_cp_error":
case "hook_test_successful_auth":
case "hook_test_successful_auth_using_id_extractor":
}
newObject, _ := proto.Marshal(object)
newObjectStr := string(newObject)
newObjectBytes := C.CString(newObjectStr)
newObjectLength := C.int(len(newObject))
objectStruct.p_data = unsafe.Pointer(newObjectBytes)
objectStruct.length = newObjectLength
}