@@ -12,9 +12,11 @@ import (
12
12
"io"
13
13
"io/ioutil"
14
14
"net/http"
15
+ "os"
15
16
"strconv"
16
17
17
18
"github.com/keploy/go-sdk/keploy"
19
+ "github.com/keploy/go-sdk/mock"
18
20
"go.keploy.io/server/pkg/models"
19
21
"go.uber.org/zap"
20
22
)
@@ -94,6 +96,10 @@ func (i *Interceptor) setRequestContext(ctx context.Context) context.Context {
94
96
// RoundTrip is the custom method which is called before making http client calls to
95
97
// capture or replay the outputs of external http service.
96
98
func (i Interceptor ) RoundTrip (r * http.Request ) (* http.Response , error ) {
99
+ if keploy .GetModeFromContext (r .Context ()) == keploy .MODE_OFF {
100
+ return i .core .RoundTrip (r )
101
+ }
102
+
97
103
// Read the request body to store in meta
98
104
var reqBody []byte
99
105
if r .Body != nil { // Read
@@ -113,9 +119,6 @@ func (i Interceptor) RoundTrip(r *http.Request) (*http.Response, error) {
113
119
r = r .WithContext (ctx )
114
120
}
115
121
116
- if keploy .GetModeFromContext (r .Context ()) == keploy .MODE_OFF {
117
- return i .core .RoundTrip (r )
118
- }
119
122
var (
120
123
err error
121
124
kerr * keploy.KError = & keploy.KError {}
@@ -139,10 +142,69 @@ func (i Interceptor) RoundTrip(r *http.Request) (*http.Response, error) {
139
142
"ProtoMinor" : strconv .Itoa (r .ProtoMinor ),
140
143
}
141
144
switch mode {
142
- case "test" :
143
- //don't call i.core.RoundTrip method
144
- case "record" :
145
+ case keploy .MODE_TEST :
146
+ //don't call i.core.RoundTrip method when not in file export
147
+ if kctx .FileExport {
148
+ mock := kctx .Mock
149
+ if len (mock ) > 0 {
150
+ resp .Body = ioutil .NopCloser (bytes .NewBuffer ([]byte (mock [0 ].Spec .Response .Body )))
151
+ resp .Header = mock [0 ].Spec .Response .Header
152
+ resp .StatusCode = mock [0 ].Spec .Response .StatusCode
153
+ kctx .Mock = mock [1 :]
154
+ }
155
+ return resp , err
156
+ }
157
+ case keploy .MODE_RECORD :
145
158
resp , err = i .core .RoundTrip (r )
159
+ if kctx .FileExport {
160
+ var (
161
+ respBody []byte
162
+ statusCode int
163
+ respHeader http.Header
164
+ )
165
+ if resp != nil {
166
+ // Read the response body to capture
167
+ if resp .Body != nil { // Read
168
+ var err error
169
+ respBody , err = ioutil .ReadAll (resp .Body )
170
+ if err != nil {
171
+ // TODO right way to log errors
172
+ i .log .Error ("Unable to read request body" , zap .Error (err ))
173
+ return nil , err
174
+ }
175
+ }
176
+ resp .Body = ioutil .NopCloser (bytes .NewBuffer (respBody )) // Reset
177
+ statusCode = resp .StatusCode
178
+ respHeader = resp .Header
179
+ }
180
+
181
+ path , err := os .Getwd ()
182
+ if err != nil {
183
+ i .log .Error ("cannot find current directory" , zap .Error (err ))
184
+ return nil , err
185
+ }
186
+ mock .PostMock (context .Background (), path , models.Mock {
187
+ Name : kctx .TestID ,
188
+ Spec : models.SpecSchema {
189
+ Type : string (models .HttpClient ),
190
+ Metadata : meta ,
191
+ Request : models.HttpReq {
192
+ Method : models .Method (r .Method ),
193
+ ProtoMajor : r .ProtoMajor ,
194
+ ProtoMinor : r .ProtoMinor ,
195
+ URL : r .URL .String (),
196
+ Header : r .Header ,
197
+ Body : string (reqBody ),
198
+ },
199
+ Response : models.HttpResp {
200
+ StatusCode : statusCode ,
201
+ Header : respHeader ,
202
+ Body : string (respBody ),
203
+ },
204
+ },
205
+ })
206
+ return resp , err
207
+ }
146
208
if resp == nil {
147
209
isRespNil = true
148
210
resp = & http.Response {}
0 commit comments