forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coprocess_test_helpers.go
192 lines (140 loc) · 4.43 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// +build coprocess
// +build !python
// +build !lua
// +build !grpc
package main
/*
#include <stdio.h>
#include <stdlib.h>
#include "coprocess/sds/sds.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);
printf("TestDispatchHook: %p\n", object);
outputObject->p_data = object->p_data;
outputObject->length = object->length;
applyTestHooks(outputObject);
return outputObject;
};
*/
import "C"
import (
"strings"
"unsafe"
"github.com/TykTechnologies/tyk/coprocess"
"github.com/golang/protobuf/proto"
)
var CoProcessReload = make(chan bool)
var CoProcessDispatchEvent = make(chan []byte)
type TestDispatcher struct {
coprocess.Dispatcher
}
/* Basic CoProcessDispatcher functions */
func (d *TestDispatcher) Dispatch(objectPtr unsafe.Pointer) unsafe.Pointer {
var object *C.struct_CoProcessMessage
object = (*C.struct_CoProcessMessage)(objectPtr)
var newObjectPtr *C.struct_CoProcessMessage
newObjectPtr = C.TestDispatchHook(object)
return unsafe.Pointer(newObjectPtr)
}
func (d *TestDispatcher) DispatchEvent(eventJSON []byte) {
CoProcessDispatchEvent <- eventJSON
return
}
func (d *TestDispatcher) Reload() {
CoProcessReload <- true
return
}
/* 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)
var CObjectStr *C.char
CObjectStr = C.CString(objectMsgStr)
var messagePtr *C.struct_CoProcessMessage
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 {
var message *C.struct_CoProcessMessage
message = (*C.struct_CoProcessMessage)(messagePtr)
var object *coprocess.Object
object = &coprocess.Object{}
objectBytes := C.GoBytes(message.p_data, message.length)
proto.Unmarshal(objectBytes, object)
return object
}
func (d *TestDispatcher) TestMessageLength(messagePtr unsafe.Pointer) (length int) {
var message *C.struct_CoProcessMessage
message = (*C.struct_CoProcessMessage)(messagePtr)
length = int(C.TestMessageLength(message))
return length
}
func TestTykStoreData(key string, 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 string, eventPayload string) {
CeventName := C.CString(eventName)
CeventPayload := C.CString(eventPayload)
TykTriggerEvent(CeventName, CeventPayload)
}
/* Middleware */
//export applyTestHooks
func applyTestHooks(objectPtr unsafe.Pointer) {
var objectStruct *C.struct_CoProcessMessage
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{403, "Key not authorised"}
case "hook_test_bad_auth_using_id_extractor":
break
case "hook_test_bad_auth_cp_error":
break
case "hook_test_successful_auth":
break
case "hook_test_successful_auth_using_id_extractor":
break
}
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
}