Skip to content

Commit ceb818f

Browse files
authored
chore: http client and string cleanup (#115)
* fix: merges mock/httpclient into integrations and uses mode enums Signed-off-by: re-Tick <[email protected]> * fix: removes redundant mock/httpclient Signed-off-by: re-Tick <[email protected]> Signed-off-by: re-Tick <[email protected]>
1 parent 756b392 commit ceb818f

22 files changed

+219
-249
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ port := "8080"
6464
```
6565
## Configure
6666
```
67-
export KEPLOY_MODE="test"
67+
export KEPLOY_MODE=keploy.MODE_TEST
6868
```
6969
### KEPLOY_MODE
7070
There are 3 modes:

integrations/kfasthttp/fasthttp.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func FastHttpMiddleware(k *keploy.Keploy) func(fasthttp.RequestHandler) fasthttp
5151
id := string(c.Request.Header.Peek("KEPLOY_TEST_ID"))
5252
if id != "" {
5353
setContextValFast(c, &keploy.Context{
54-
Mode: "test",
54+
Mode: keploy.MODE_TEST,
5555
TestID: id,
5656
Deps: k.GetDependencies(id),
5757
})
@@ -61,7 +61,7 @@ func FastHttpMiddleware(k *keploy.Keploy) func(fasthttp.RequestHandler) fasthttp
6161

6262
}
6363
setContextValFast(c, &keploy.Context{
64-
Mode: "record",
64+
Mode: keploy.MODE_RECORD,
6565
})
6666
var reqBody []byte
6767
var err error

integrations/kgin/v1/gin-v1.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func mw(k *keploy.Keploy) gin.HandlerFunc {
6868
// id is only present during simulation
6969
// run it similar to how testcases would run
7070
setContextValGin(c, &keploy.Context{
71-
Mode: "test",
71+
Mode: keploy.MODE_TEST,
7272
TestID: id,
7373
Deps: k.GetDependencies(id),
7474
})
@@ -77,7 +77,7 @@ func mw(k *keploy.Keploy) gin.HandlerFunc {
7777
return
7878
}
7979

80-
setContextValGin(c, &keploy.Context{Mode: "record"})
80+
setContextValGin(c, &keploy.Context{Mode: keploy.MODE_RECORD})
8181

8282
// Request
8383
var reqBody []byte

integrations/kgrpc/grpc.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ func clientInterceptor(k *keploy.Keploy) func(
4848

4949
mode := kctx.Mode
5050
switch mode {
51-
case "test":
51+
case keploy.MODE_TEST:
5252
//dont run invoker
53-
case "record":
53+
case keploy.MODE_RECORD:
5454
err = invoker(ctx, method, req, reply, cc, opts...)
5555
default:
5656
return errors.New("integrations: Not in a valid sdk mode")
@@ -111,11 +111,11 @@ func streamClientInterceptor(k *keploy.Keploy) func(ctx context.Context, desc *g
111111
mode := kctx.Mode
112112

113113
switch mode {
114-
case "test":
114+
case keploy.MODE_TEST:
115115
//dont run invoker
116116
clientStreamAdd := new(grpc.ClientStream)
117117
clientStream = *clientStreamAdd
118-
case "record":
118+
case keploy.MODE_RECORD:
119119
clientStream, err = streamer(ctx, desc, cc, method, opts...)
120120
}
121121

@@ -151,9 +151,9 @@ func (s *tracedClientStream) CloseSend() error {
151151
}
152152
mode := kctx.Mode
153153
switch mode {
154-
case "record":
154+
case keploy.MODE_RECORD:
155155
err = s.ClientStream.CloseSend()
156-
case "test":
156+
case keploy.MODE_TEST:
157157
// don't call CloseSend
158158

159159
}
@@ -192,9 +192,9 @@ func (s *tracedClientStream) SendMsg(m interface{}) error {
192192
}
193193
mode := kctx.Mode
194194
switch mode {
195-
case "record":
195+
case keploy.MODE_RECORD:
196196
err = s.ClientStream.SendMsg(m)
197-
case "test":
197+
case keploy.MODE_TEST:
198198
// don't call SendMsg
199199

200200
}
@@ -231,9 +231,9 @@ func (s *tracedClientStream) Context() context.Context {
231231
}
232232
mode := kctx.Mode
233233
switch mode {
234-
case "record":
234+
case keploy.MODE_RECORD:
235235
ctxOutput = s.ClientStream.Context()
236-
case "test":
236+
case keploy.MODE_TEST:
237237
// don't call Context
238238

239239
}
@@ -268,9 +268,9 @@ func (s *tracedClientStream) RecvMsg(m interface{}) error {
268268
}
269269
mode := kctx.Mode
270270
switch mode {
271-
case "record":
271+
case keploy.MODE_RECORD:
272272
err = s.ClientStream.RecvMsg(m)
273-
case "test":
273+
case keploy.MODE_TEST:
274274
// don't call RecvMsg
275275

276276
}

integrations/khttpclient/httpClient.go

+68-6
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import (
1212
"io"
1313
"io/ioutil"
1414
"net/http"
15+
"os"
1516
"strconv"
1617

1718
"github.com/keploy/go-sdk/keploy"
19+
"github.com/keploy/go-sdk/mock"
1820
"go.keploy.io/server/pkg/models"
1921
"go.uber.org/zap"
2022
)
@@ -94,6 +96,10 @@ func (i *Interceptor) setRequestContext(ctx context.Context) context.Context {
9496
// RoundTrip is the custom method which is called before making http client calls to
9597
// capture or replay the outputs of external http service.
9698
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+
97103
// Read the request body to store in meta
98104
var reqBody []byte
99105
if r.Body != nil { // Read
@@ -113,9 +119,6 @@ func (i Interceptor) RoundTrip(r *http.Request) (*http.Response, error) {
113119
r = r.WithContext(ctx)
114120
}
115121

116-
if keploy.GetModeFromContext(r.Context()) == keploy.MODE_OFF {
117-
return i.core.RoundTrip(r)
118-
}
119122
var (
120123
err error
121124
kerr *keploy.KError = &keploy.KError{}
@@ -139,10 +142,69 @@ func (i Interceptor) RoundTrip(r *http.Request) (*http.Response, error) {
139142
"ProtoMinor": strconv.Itoa(r.ProtoMinor),
140143
}
141144
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:
145158
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+
}
146208
if resp == nil {
147209
isRespNil = true
148210
resp = &http.Response{}

integrations/kmongo/cursor.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ func (cr *Cursor) Err() error {
3939
}
4040
mode := kctx.Mode
4141
switch mode {
42-
case "test":
42+
case keploy.MODE_TEST:
4343
//dont run mongo query as it is stored in context
4444
err = nil
45-
case "record":
45+
case keploy.MODE_RECORD:
4646
err = cr.Cursor.Err()
4747
default:
4848
return errors.New("integrations: Not in a valid sdk mode")
@@ -95,10 +95,10 @@ func (cr *Cursor) Close(ctx context.Context) error {
9595
}
9696
mode := kctx.Mode
9797
switch mode {
98-
case "test":
98+
case keploy.MODE_TEST:
9999
//dont run mongo query as it is stored in context
100100
err = nil
101-
case "record":
101+
case keploy.MODE_RECORD:
102102
err = cr.Cursor.Close(ctx)
103103
default:
104104
return errors.New("integrations: Not in a valid sdk mode")
@@ -149,11 +149,11 @@ func (cr *Cursor) TryNext(ctx context.Context) bool {
149149
var output *bool
150150
mode := kctx.Mode
151151
switch mode {
152-
case "test":
152+
case keploy.MODE_TEST:
153153
//dont run mongo query as it is stored in context
154154
n := false
155155
output = &n
156-
case "record":
156+
case keploy.MODE_RECORD:
157157
n := cr.Cursor.TryNext(ctx)
158158
output = &n
159159
default:
@@ -201,10 +201,10 @@ func (cr *Cursor) All(ctx context.Context, results interface{}) error {
201201
}
202202
mode := kctx.Mode
203203
switch mode {
204-
case "test":
204+
case keploy.MODE_TEST:
205205
//dont run mongo query as it is stored in context
206206
err = nil
207-
case "record":
207+
case keploy.MODE_RECORD:
208208
err = cr.Cursor.All(ctx, results)
209209
default:
210210
return errors.New("integrations: Not in a valid sdk mode")
@@ -255,11 +255,11 @@ func (cr *Cursor) Next(ctx context.Context) bool {
255255
var output *bool
256256
mode := kctx.Mode
257257
switch mode {
258-
case "test":
258+
case keploy.MODE_TEST:
259259
//dont run mongo query as it is stored in context
260260
n := false
261261
output = &n
262-
case "record":
262+
case keploy.MODE_RECORD:
263263
n := cr.Cursor.Next(ctx)
264264
output = &n
265265
default:
@@ -307,10 +307,10 @@ func (cr *Cursor) Decode(v interface{}) error {
307307
}
308308
mode := kctx.Mode
309309
switch mode {
310-
case "test":
310+
case keploy.MODE_TEST:
311311
//dont run mongo query as it is stored in context
312312
err = nil
313-
case "record":
313+
case keploy.MODE_RECORD:
314314
err = cr.Cursor.Decode(v)
315315
default:
316316
return errors.New("integrations: Not in a valid sdk mode")
@@ -348,7 +348,7 @@ func (cr *Cursor) Decode(v interface{}) error {
348348
}
349349

350350
// // Find creates and returns the instance of pointer to Cursor which have overridden methods of mongo.Cursor.
351-
// // Actual Collection.Find is called only in "record" or "off" mode.
351+
// // Actual Collection.Find is called only in keploy.MODE_RECORD or "off" mode.
352352
// //
353353
// // For information about Collection.Find, See https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#Collection.Find.
354354
// func (c *Collection) Find(ctx context.Context, filter interface{},
@@ -382,15 +382,15 @@ func (cr *Cursor) Decode(v interface{}) error {
382382
// err error
383383
// )
384384
// switch mode {
385-
// case "test":
385+
// case keploy.MODE_TEST:
386386
// //don't call method in test mode
387387
// return &Cursor{
388388
// filter: filter,
389389
// findOpts: derivedOpts,
390390
// log: c.log,
391391
// ctx: ctx,
392392
// }, err
393-
// case "record":
393+
// case keploy.MODE_RECORD:
394394
// cursor, err = c.Collection.Find(ctx, filter, opts...)
395395
// return &Cursor{
396396
// Cursor: *cursor,

integrations/kmongo/delete.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (c *Collection) DeleteOne(ctx context.Context, filter interface{},
7070
return output, err
7171
}
7272

73-
// DeleteMany method mocks Collection.DeleteMany of mongo inorder to call it only in "record" or "off" mode.
73+
// DeleteMany method mocks Collection.DeleteMany of mongo inorder to call it only in keploy.MODE_RECORD or "off" mode.
7474
//
7575
// See https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#Collection.DeleteMany for information about Collection.DeleteMany.
7676
func (c *Collection) DeleteMany(ctx context.Context, filter interface{},

integrations/kmongo/find.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ func (c *Collection) FindOne(ctx context.Context, filter interface{}, opts ...*o
4242
var sr *mongo.SingleResult
4343

4444
switch mode {
45-
case "test":
45+
case keploy.MODE_TEST:
4646
return singleResult
47-
case "record":
47+
case keploy.MODE_RECORD:
4848
sr = c.Collection.FindOne(ctx, filter, opts...)
4949
if sr != nil {
5050
singleResult.SingleResult = *sr
@@ -57,7 +57,7 @@ func (c *Collection) FindOne(ctx context.Context, filter interface{}, opts ...*o
5757
}
5858

5959
// Find creates and returns the instance of pointer to keploy Cursor struct which have overridden methods of mongo.Cursor.
60-
// Actual Collection.Find is called only in "record" or "off" mode.
60+
// Actual Collection.Find is called only in keploy.MODE_RECORD or "off" mode.
6161
//
6262
// For information about Collection.Find, See https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#Collection.Find.
6363
func (c *Collection) Find(ctx context.Context, filter interface{},
@@ -93,10 +93,10 @@ func (c *Collection) Find(ctx context.Context, filter interface{},
9393
)
9494

9595
switch mode {
96-
case "test":
96+
case keploy.MODE_TEST:
9797
//don't call method in test mode
9898
return result, err
99-
case "record":
99+
case keploy.MODE_RECORD:
100100
cursor, err = c.Collection.Find(ctx, filter, opts...)
101101
if cursor != nil {
102102
result.Cursor = *cursor

0 commit comments

Comments
 (0)