@@ -10,8 +10,8 @@ import (
10
10
"time"
11
11
12
12
"github.com/frain-dev/immune"
13
+ "github.com/frain-dev/immune/database"
13
14
"github.com/frain-dev/immune/url"
14
- "github.com/google/uuid"
15
15
"github.com/pkg/errors"
16
16
log "github.com/sirupsen/logrus"
17
17
)
@@ -21,17 +21,28 @@ type Executor struct {
21
21
callbackIDLocation string
22
22
baseURL string
23
23
maxCallbackWaitSeconds uint
24
- s immune. CallbackServer
24
+ idFn func () string
25
25
client * http.Client
26
+ dbTruncator database.Truncator
26
27
vm * immune.VariableMap
28
+ s immune.CallbackServer
27
29
}
28
30
29
- func NewExecutor (s immune.CallbackServer , client * http.Client , vm * immune.VariableMap , maxCallbackWaitSeconds uint , baseURL string , callbackIDLocation string ) * Executor {
31
+ func NewExecutor (
32
+ s immune.CallbackServer ,
33
+ client * http.Client ,
34
+ vm * immune.VariableMap ,
35
+ maxCallbackWaitSeconds uint ,
36
+ baseURL string ,
37
+ callbackIDLocation string ,
38
+ dbTruncator database.Truncator , idFn func () string ) * Executor {
30
39
return & Executor {
31
40
s : s ,
32
41
vm : vm ,
42
+ idFn : idFn ,
33
43
client : client ,
34
44
baseURL : baseURL ,
45
+ dbTruncator : dbTruncator ,
35
46
callbackIDLocation : callbackIDLocation ,
36
47
maxCallbackWaitSeconds : maxCallbackWaitSeconds ,
37
48
}
@@ -41,12 +52,12 @@ func NewExecutor(s immune.CallbackServer, client *http.Client, vm *immune.Variab
41
52
func (ex * Executor ) ExecuteSetupTestCase (ctx context.Context , setupTC * immune.SetupTestCase ) error {
42
53
u , err := url .Parse (fmt .Sprintf ("%s%s" , ex .baseURL , setupTC .Endpoint ))
43
54
if err != nil {
44
- return errors .Wrapf (err , "setup_test_case %d : failed to parse url" , setupTC .Position )
55
+ return errors .Wrapf (err , "setup_test_case %s : failed to parse url" , setupTC .Name )
45
56
}
46
57
47
58
result , err := u .ProcessWithVariableMap (ex .vm )
48
59
if err != nil {
49
- return errors .Wrapf (err , "setup_test_case %d : failed to process parsed url with variable map" , setupTC .Position )
60
+ return errors .Wrapf (err , "setup_test_case %s : failed to process parsed url with variable map" , setupTC .Name )
50
61
}
51
62
52
63
r := & request {
@@ -58,39 +69,39 @@ func (ex *Executor) ExecuteSetupTestCase(ctx context.Context, setupTC *immune.Se
58
69
59
70
err = r .processWithVariableMap (ex .vm )
60
71
if err != nil {
61
- return errors .Wrapf (err , "setup_test_case %d : failed to process request body with variable map" , setupTC .Position )
72
+ return errors .Wrapf (err , "setup_test_case %s : failed to process request body with variable map" , setupTC .Name )
62
73
}
63
- //log.Infof("setup_test_case %d : request body: %s", setupTC.Position , pretty.Sprint(r.body))
74
+ //log.Infof("setup_test_case %s : request body: %s", setupTC.Name , pretty.Sprint(r.body))
64
75
65
76
resp , err := ex .sendRequest (ctx , r )
66
77
if err != nil {
67
78
return err
68
79
}
69
80
70
81
if setupTC .StatusCode != resp .statusCode {
71
- return errors .Errorf ("setup_test_case %d : wants status code %d but got status code %d" , setupTC .Position , setupTC .StatusCode , resp .statusCode )
82
+ return errors .Errorf ("setup_test_case %s : wants status code %d but got status code %d, response body: %s " , setupTC .Name , setupTC .StatusCode , resp .statusCode , resp . body . String () )
72
83
}
73
84
74
85
if setupTC .ResponseBody {
75
86
if resp .body .Len () == 0 {
76
- return errors .Errorf ("setup_test_case %d : wants response body but got no response body" , setupTC .Position )
87
+ return errors .Errorf ("setup_test_case %s : wants response body but got no response body" , setupTC .Name )
77
88
}
78
89
79
90
m := immune.M {}
80
91
err = resp .Decode (& m )
81
92
if err != nil {
82
- return errors .Wrapf (err , "setup_test_case %d : failed to decode response body" , setupTC .Position )
93
+ return errors .Wrapf (err , "setup_test_case %s : failed to decode response body: response body: %s " , setupTC .Name , resp . body . String () )
83
94
}
84
95
85
96
if setupTC .StoreResponseVariables != nil {
86
97
err = ex .vm .ProcessResponse (ctx , setupTC .StoreResponseVariables , m )
87
98
if err != nil {
88
- return errors .Wrapf (err , "setup_test_case %d : failed to process response body" , setupTC .Position )
99
+ return errors .Wrapf (err , "setup_test_case %s : failed to process response body: response body: %s " , setupTC .Name , resp . body . String () )
89
100
}
90
101
}
91
102
} else {
92
103
if resp .body .Len () > 0 {
93
- return errors .Errorf ("setup_test_case %d : does not want a response body but got a response body: '%s'" , setupTC .Position , resp .body .String ())
104
+ return errors .Errorf ("setup_test_case %s : does not want a response body but got a response body: '%s'" , setupTC .Name , resp .body .String ())
94
105
}
95
106
}
96
107
@@ -101,20 +112,20 @@ func (ex *Executor) ExecuteSetupTestCase(ctx context.Context, setupTC *immune.Se
101
112
func (ex * Executor ) ExecuteTestCase (ctx context.Context , tc * immune.TestCase ) error {
102
113
u , err := url .Parse (fmt .Sprintf ("%s%s" , ex .baseURL , tc .Endpoint ))
103
114
if err != nil {
104
- return errors .Wrapf (err , "test_case %d : failed to parse url" , tc .Position )
115
+ return errors .Wrapf (err , "test_case %s : failed to parse url" , tc .Name )
105
116
}
106
117
107
118
result , err := u .ProcessWithVariableMap (ex .vm )
108
119
if err != nil {
109
- return errors .Wrapf (err , "test_case %d : failed to process parsed url with variable map" , tc .Position )
120
+ return errors .Wrapf (err , "test_case %s : failed to process parsed url with variable map" , tc .Name )
110
121
}
111
122
112
123
var uid string
113
124
if tc .Callback .Enabled {
114
- uid = uuid . New (). String ()
125
+ uid = ex . idFn ()
115
126
err = immune .InjectCallbackID (ex .callbackIDLocation , uid , tc .RequestBody )
116
127
if err != nil {
117
- return errors .Wrapf (err , "test_case %d : failed to inject callback id into request body" , tc .Position )
128
+ return errors .Wrapf (err , "test_case %s : failed to inject callback id into request body" , tc .Name )
118
129
}
119
130
}
120
131
@@ -127,7 +138,7 @@ func (ex *Executor) ExecuteTestCase(ctx context.Context, tc *immune.TestCase) er
127
138
128
139
err = r .processWithVariableMap (ex .vm )
129
140
if err != nil {
130
- return errors .Wrapf (err , "test_case %d : failed to process request body with variable map" , tc .Position )
141
+ return errors .Wrapf (err , "test_case %s : failed to process request body with variable map" , tc .Name )
131
142
}
132
143
133
144
resp , err := ex .sendRequest (ctx , r )
@@ -136,23 +147,23 @@ func (ex *Executor) ExecuteTestCase(ctx context.Context, tc *immune.TestCase) er
136
147
}
137
148
138
149
if tc .StatusCode != resp .statusCode {
139
- return errors .Errorf ("test_case %d : wants status code %d but got status code %d" , tc .Position , tc .StatusCode , resp .statusCode )
150
+ return errors .Errorf ("test_case %s : wants status code %d but got status code %d" , tc .Name , tc .StatusCode , resp .statusCode )
140
151
}
141
152
142
153
if tc .ResponseBody {
143
154
if resp .body .Len () == 0 {
144
- return errors .Errorf ("test_case %d : wants response body but got no response body: %+v " , tc .Position , resp )
155
+ return errors .Errorf ("test_case %s : wants response body but got no response body: status_code: %d " , tc .Name , resp . statusCode )
145
156
}
146
157
147
158
m := immune.M {}
148
159
err = resp .Decode (& m )
149
160
if err != nil {
150
- return errors .Errorf ( "test_case %d : failed to decode response body: %+v " , tc .Position , resp )
161
+ return errors .Wrapf ( err , "test_case %s : failed to decode response body: %s " , tc .Name , string ( resp . buf ) )
151
162
}
152
163
153
164
} else {
154
165
if resp .body .Len () > 0 {
155
- return errors .Wrapf ( err , "test_case %d : does not want a response body but got a response body: '%s'" , tc .Position , resp .body .String ())
166
+ return errors .Errorf ( "test_case %s : does not want a response body but got a response body: '%s'" , tc .Name , resp .body .String ())
156
167
}
157
168
}
158
169
@@ -163,19 +174,19 @@ func (ex *Executor) ExecuteTestCase(ctx context.Context, tc *immune.TestCase) er
163
174
for i := uint (1 ); i <= tc .Callback .Times ; i ++ {
164
175
select {
165
176
case <- cctx .Done ():
166
- log .Infof ("succesfully received %d callbacks for test_case %d before max callback wait seconds elapsed" , i , tc .Position )
177
+ log .Infof ("succesfully received %d callbacks for test_case %s before max callback wait seconds elapsed" , i , tc .Name )
167
178
break
168
179
default :
169
180
sig := ex .s .ReceiveCallback ()
170
181
if sig .ImmuneCallBackID != uid {
171
- return errors .Errorf ("test_case %d : incorrect callback_id: expected_callback_id '%s', got_callback_id '%s'" , tc .Position , uid , sig .ImmuneCallBackID )
182
+ return errors .Errorf ("test_case %s : incorrect callback_id: expected_callback_id '%s', got_callback_id '%s'" , tc .Name , uid , sig .ImmuneCallBackID )
172
183
}
173
- log .Infof ("callback %d for test_case %d received" , i , tc .Position )
184
+ log .Infof ("callback %d for test_case %s received" , i , tc .Name )
174
185
}
175
186
}
176
187
}
177
188
178
- return nil
189
+ return ex . dbTruncator . Truncate ( ctx )
179
190
}
180
191
181
192
func (ex * Executor ) sendRequest (ctx context.Context , r * request ) (* response , error ) {
@@ -202,5 +213,5 @@ func (ex *Executor) sendRequest(ctx context.Context, r *request) (*response, err
202
213
return nil , err
203
214
}
204
215
205
- return & response {body : bytes .NewBuffer (buf ), statusCode : resp .StatusCode }, nil
216
+ return & response {body : bytes .NewBuffer (buf ), buf : buf , statusCode : resp .StatusCode }, nil
206
217
}
0 commit comments