@@ -9,23 +9,23 @@ import (
9
9
"github.com/sirupsen/logrus"
10
10
)
11
11
12
- func (ms MocksArray ) DoActions (c Context ) error {
12
+ func (ms MocksArray ) DoActions (ctx Context ) error {
13
13
for _ , m := range ms {
14
- if err := m .DoActions (c ); err != nil {
14
+ if err := m .DoActions (ctx ); err != nil {
15
15
return nil
16
16
}
17
17
}
18
18
return nil
19
19
}
20
20
21
- func (m * Mock ) DoActions (c Context ) error {
22
- c .Values = m .Values
23
- if ! c .MatchCondition (m .Expect .Condition ) {
21
+ func (m * Mock ) DoActions (ctx Context ) error {
22
+ ctx .Values = m .Values
23
+ if ! ctx .MatchCondition (m .Expect .Condition ) {
24
24
return nil
25
25
}
26
26
for _ , actionDispatcher := range m .Actions {
27
27
actualAction := getActualAction (actionDispatcher )
28
- if err := actualAction .Perform (c ); err != nil {
28
+ if err := actualAction .Perform (ctx ); err != nil {
29
29
logrus .WithFields (logrus.Fields {
30
30
"err" : err ,
31
31
"action" : fmt .Sprintf ("%T" , actualAction ),
@@ -35,13 +35,13 @@ func (m *Mock) DoActions(c Context) error {
35
35
return nil
36
36
}
37
37
38
- func (a ActionSendHTTP ) Perform (context Context ) error {
39
- bodyStr , err := context .Render (a .Body )
38
+ func (a ActionSendHTTP ) Perform (ctx Context ) error {
39
+ bodyStr , err := ctx .Render (a .Body )
40
40
if err != nil {
41
41
return err
42
42
}
43
43
44
- urlStr , err := context .Render (a .URL )
44
+ urlStr , err := ctx .Render (a .URL )
45
45
if err != nil {
46
46
return err
47
47
}
@@ -50,6 +50,11 @@ func (a ActionSendHTTP) Perform(context Context) error {
50
50
SetDebug (true ).
51
51
CustomMethod (a .Method , urlStr )
52
52
53
+ a .Headers , err = renderHeaders (ctx , a .Headers )
54
+ if err != nil {
55
+ return err
56
+ }
57
+
53
58
for k , v := range a .Headers {
54
59
request .Set (k , v )
55
60
}
@@ -61,19 +66,25 @@ func (a ActionSendHTTP) Perform(context Context) error {
61
66
return nil
62
67
}
63
68
64
- func (a ActionReplyHTTP ) Perform (context Context ) error {
65
- ec := context .HTTPContext
69
+ func (a ActionReplyHTTP ) Perform (ctx Context ) ( err error ) {
70
+ ec := ctx .HTTPContext
66
71
contentType := echo .MIMEApplicationJSON // default to JSON
67
72
if ct , ok := a .Headers [echo .HeaderContentType ]; ok {
68
73
contentType = ct
69
74
}
75
+
76
+ a .Headers , err = renderHeaders (ctx , a .Headers )
77
+ if err != nil {
78
+ return err
79
+ }
80
+
70
81
for k , v := range a .Headers {
71
82
ec .Response ().Header ().Set (k , v )
72
83
}
73
84
74
- msg , err := context .Render (a .Body )
85
+ msg , err := ctx .Render (a .Body )
75
86
if err != nil {
76
- logrus .WithField ("err" , err ).Error ("failed to render template for http" )
87
+ logrus .WithField ("err" , err ).Error ("failed to render template for http body " )
77
88
return err
78
89
}
79
90
@@ -98,46 +109,59 @@ func (a ActionReplyHTTP) Perform(context Context) error {
98
109
return nil
99
110
}
100
111
101
- func (a ActionRedis ) Perform (context Context ) error {
112
+ func (a ActionRedis ) Perform (ctx Context ) error {
102
113
for _ , cmd := range a {
103
- _ , err := context .Render (cmd )
114
+ _ , err := ctx .Render (cmd )
104
115
if err != nil {
105
116
return err
106
117
}
107
118
}
108
119
return nil
109
120
}
110
121
111
- func (a ActionSleep ) Perform (context Context ) error {
122
+ func (a ActionSleep ) Perform (ctx Context ) error {
112
123
time .Sleep (a .Duration )
113
124
return nil
114
125
}
115
126
116
- func (a ActionPublishKafka ) Perform (context Context ) error {
127
+ func (a ActionPublishKafka ) Perform (ctx Context ) error {
117
128
msg := a .Payload
118
- msg , err := context .Render (msg )
129
+ msg , err := ctx .Render (msg )
119
130
if err != nil {
120
131
logrus .WithField ("err" , err ).Error ("failed to render template for kafka payload" )
121
132
return err
122
133
}
123
- err = context .om .kafkaClient .sendMessage (a .Topic , []byte (msg ))
134
+ err = ctx .om .kafkaClient .sendMessage (a .Topic , []byte (msg ))
124
135
if err != nil {
125
136
logrus .WithField ("err" , err ).Error ("failed to publish to kafka" )
126
137
}
127
138
return err
128
139
}
129
140
130
- func (a ActionPublishAMQP ) Perform (context Context ) error {
131
- msg , err := context .Render (a .Payload )
141
+ func (a ActionPublishAMQP ) Perform (ctx Context ) error {
142
+ msg , err := ctx .Render (a .Payload )
132
143
if err != nil {
133
144
logrus .WithField ("err" , err ).Error ("failed to render template for amqp" )
134
145
return err
135
146
}
136
147
publishToAMQP (
137
- context .om .AMQPURL ,
148
+ ctx .om .AMQPURL ,
138
149
a .Exchange ,
139
150
a .RoutingKey ,
140
151
msg ,
141
152
)
142
153
return nil
143
154
}
155
+
156
+ func renderHeaders (ctx Context , headers map [string ]string ) (map [string ]string , error ) {
157
+ ret := make (map [string ]string )
158
+ for k , v := range headers {
159
+ msg , err := ctx .Render (v )
160
+ if err != nil {
161
+ logrus .WithField ("err" , err ).Error ("failed to render template for http headers" )
162
+ return nil , err
163
+ }
164
+ ret [k ] = msg
165
+ }
166
+ return ret , nil
167
+ }
0 commit comments