Skip to content

Commit

Permalink
chore: http client and string cleanup (#115)
Browse files Browse the repository at this point in the history
* fix: merges mock/httpclient into integrations and uses mode enums

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

* fix: removes redundant mock/httpclient

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>
  • Loading branch information
re-Tick authored Aug 26, 2022
1 parent 756b392 commit ceb818f
Show file tree
Hide file tree
Showing 22 changed files with 219 additions and 249 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ port := "8080"
```
## Configure
```
export KEPLOY_MODE="test"
export KEPLOY_MODE=keploy.MODE_TEST
```
### KEPLOY_MODE
There are 3 modes:
Expand Down
4 changes: 2 additions & 2 deletions integrations/kfasthttp/fasthttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func FastHttpMiddleware(k *keploy.Keploy) func(fasthttp.RequestHandler) fasthttp
id := string(c.Request.Header.Peek("KEPLOY_TEST_ID"))
if id != "" {
setContextValFast(c, &keploy.Context{
Mode: "test",
Mode: keploy.MODE_TEST,
TestID: id,
Deps: k.GetDependencies(id),
})
Expand All @@ -61,7 +61,7 @@ func FastHttpMiddleware(k *keploy.Keploy) func(fasthttp.RequestHandler) fasthttp

}
setContextValFast(c, &keploy.Context{
Mode: "record",
Mode: keploy.MODE_RECORD,
})
var reqBody []byte
var err error
Expand Down
4 changes: 2 additions & 2 deletions integrations/kgin/v1/gin-v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func mw(k *keploy.Keploy) gin.HandlerFunc {
// id is only present during simulation
// run it similar to how testcases would run
setContextValGin(c, &keploy.Context{
Mode: "test",
Mode: keploy.MODE_TEST,
TestID: id,
Deps: k.GetDependencies(id),
})
Expand All @@ -77,7 +77,7 @@ func mw(k *keploy.Keploy) gin.HandlerFunc {
return
}

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

// Request
var reqBody []byte
Expand Down
24 changes: 12 additions & 12 deletions integrations/kgrpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func clientInterceptor(k *keploy.Keploy) func(

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

switch mode {
case "test":
case keploy.MODE_TEST:
//dont run invoker
clientStreamAdd := new(grpc.ClientStream)
clientStream = *clientStreamAdd
case "record":
case keploy.MODE_RECORD:
clientStream, err = streamer(ctx, desc, cc, method, opts...)
}

Expand Down Expand Up @@ -151,9 +151,9 @@ func (s *tracedClientStream) CloseSend() error {
}
mode := kctx.Mode
switch mode {
case "record":
case keploy.MODE_RECORD:
err = s.ClientStream.CloseSend()
case "test":
case keploy.MODE_TEST:
// don't call CloseSend

}
Expand Down Expand Up @@ -192,9 +192,9 @@ func (s *tracedClientStream) SendMsg(m interface{}) error {
}
mode := kctx.Mode
switch mode {
case "record":
case keploy.MODE_RECORD:
err = s.ClientStream.SendMsg(m)
case "test":
case keploy.MODE_TEST:
// don't call SendMsg

}
Expand Down Expand Up @@ -231,9 +231,9 @@ func (s *tracedClientStream) Context() context.Context {
}
mode := kctx.Mode
switch mode {
case "record":
case keploy.MODE_RECORD:
ctxOutput = s.ClientStream.Context()
case "test":
case keploy.MODE_TEST:
// don't call Context

}
Expand Down Expand Up @@ -268,9 +268,9 @@ func (s *tracedClientStream) RecvMsg(m interface{}) error {
}
mode := kctx.Mode
switch mode {
case "record":
case keploy.MODE_RECORD:
err = s.ClientStream.RecvMsg(m)
case "test":
case keploy.MODE_TEST:
// don't call RecvMsg

}
Expand Down
74 changes: 68 additions & 6 deletions integrations/khttpclient/httpClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
"io"
"io/ioutil"
"net/http"
"os"
"strconv"

"github.com/keploy/go-sdk/keploy"
"github.com/keploy/go-sdk/mock"
"go.keploy.io/server/pkg/models"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -94,6 +96,10 @@ func (i *Interceptor) setRequestContext(ctx context.Context) context.Context {
// RoundTrip is the custom method which is called before making http client calls to
// capture or replay the outputs of external http service.
func (i Interceptor) RoundTrip(r *http.Request) (*http.Response, error) {
if keploy.GetModeFromContext(r.Context()) == keploy.MODE_OFF {
return i.core.RoundTrip(r)
}

// Read the request body to store in meta
var reqBody []byte
if r.Body != nil { // Read
Expand All @@ -113,9 +119,6 @@ func (i Interceptor) RoundTrip(r *http.Request) (*http.Response, error) {
r = r.WithContext(ctx)
}

if keploy.GetModeFromContext(r.Context()) == keploy.MODE_OFF {
return i.core.RoundTrip(r)
}
var (
err error
kerr *keploy.KError = &keploy.KError{}
Expand All @@ -139,10 +142,69 @@ func (i Interceptor) RoundTrip(r *http.Request) (*http.Response, error) {
"ProtoMinor": strconv.Itoa(r.ProtoMinor),
}
switch mode {
case "test":
//don't call i.core.RoundTrip method
case "record":
case keploy.MODE_TEST:
//don't call i.core.RoundTrip method when not in file export
if kctx.FileExport {
mock := kctx.Mock
if len(mock) > 0 {
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(mock[0].Spec.Response.Body)))
resp.Header = mock[0].Spec.Response.Header
resp.StatusCode = mock[0].Spec.Response.StatusCode
kctx.Mock = mock[1:]
}
return resp, err
}
case keploy.MODE_RECORD:
resp, err = i.core.RoundTrip(r)
if kctx.FileExport {
var (
respBody []byte
statusCode int
respHeader http.Header
)
if resp != nil {
// Read the response body to capture
if resp.Body != nil { // Read
var err error
respBody, err = ioutil.ReadAll(resp.Body)
if err != nil {
// TODO right way to log errors
i.log.Error("Unable to read request body", zap.Error(err))
return nil, err
}
}
resp.Body = ioutil.NopCloser(bytes.NewBuffer(respBody)) // Reset
statusCode = resp.StatusCode
respHeader = resp.Header
}

path, err := os.Getwd()
if err != nil {
i.log.Error("cannot find current directory", zap.Error(err))
return nil, err
}
mock.PostMock(context.Background(), path, models.Mock{
Name: kctx.TestID,
Spec: models.SpecSchema{
Type: string(models.HttpClient),
Metadata: meta,
Request: models.HttpReq{
Method: models.Method(r.Method),
ProtoMajor: r.ProtoMajor,
ProtoMinor: r.ProtoMinor,
URL: r.URL.String(),
Header: r.Header,
Body: string(reqBody),
},
Response: models.HttpResp{
StatusCode: statusCode,
Header: respHeader,
Body: string(respBody),
},
},
})
return resp, err
}
if resp == nil {
isRespNil = true
resp = &http.Response{}
Expand Down
30 changes: 15 additions & 15 deletions integrations/kmongo/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ func (cr *Cursor) Err() error {
}
mode := kctx.Mode
switch mode {
case "test":
case keploy.MODE_TEST:
//dont run mongo query as it is stored in context
err = nil
case "record":
case keploy.MODE_RECORD:
err = cr.Cursor.Err()
default:
return errors.New("integrations: Not in a valid sdk mode")
Expand Down Expand Up @@ -95,10 +95,10 @@ func (cr *Cursor) Close(ctx context.Context) error {
}
mode := kctx.Mode
switch mode {
case "test":
case keploy.MODE_TEST:
//dont run mongo query as it is stored in context
err = nil
case "record":
case keploy.MODE_RECORD:
err = cr.Cursor.Close(ctx)
default:
return errors.New("integrations: Not in a valid sdk mode")
Expand Down Expand Up @@ -149,11 +149,11 @@ func (cr *Cursor) TryNext(ctx context.Context) bool {
var output *bool
mode := kctx.Mode
switch mode {
case "test":
case keploy.MODE_TEST:
//dont run mongo query as it is stored in context
n := false
output = &n
case "record":
case keploy.MODE_RECORD:
n := cr.Cursor.TryNext(ctx)
output = &n
default:
Expand Down Expand Up @@ -201,10 +201,10 @@ func (cr *Cursor) All(ctx context.Context, results interface{}) error {
}
mode := kctx.Mode
switch mode {
case "test":
case keploy.MODE_TEST:
//dont run mongo query as it is stored in context
err = nil
case "record":
case keploy.MODE_RECORD:
err = cr.Cursor.All(ctx, results)
default:
return errors.New("integrations: Not in a valid sdk mode")
Expand Down Expand Up @@ -255,11 +255,11 @@ func (cr *Cursor) Next(ctx context.Context) bool {
var output *bool
mode := kctx.Mode
switch mode {
case "test":
case keploy.MODE_TEST:
//dont run mongo query as it is stored in context
n := false
output = &n
case "record":
case keploy.MODE_RECORD:
n := cr.Cursor.Next(ctx)
output = &n
default:
Expand Down Expand Up @@ -307,10 +307,10 @@ func (cr *Cursor) Decode(v interface{}) error {
}
mode := kctx.Mode
switch mode {
case "test":
case keploy.MODE_TEST:
//dont run mongo query as it is stored in context
err = nil
case "record":
case keploy.MODE_RECORD:
err = cr.Cursor.Decode(v)
default:
return errors.New("integrations: Not in a valid sdk mode")
Expand Down Expand Up @@ -348,7 +348,7 @@ func (cr *Cursor) Decode(v interface{}) error {
}

// // Find creates and returns the instance of pointer to Cursor which have overridden methods of mongo.Cursor.
// // Actual Collection.Find is called only in "record" or "off" mode.
// // Actual Collection.Find is called only in keploy.MODE_RECORD or "off" mode.
// //
// // For information about Collection.Find, See https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#Collection.Find.
// func (c *Collection) Find(ctx context.Context, filter interface{},
Expand Down Expand Up @@ -382,15 +382,15 @@ func (cr *Cursor) Decode(v interface{}) error {
// err error
// )
// switch mode {
// case "test":
// case keploy.MODE_TEST:
// //don't call method in test mode
// return &Cursor{
// filter: filter,
// findOpts: derivedOpts,
// log: c.log,
// ctx: ctx,
// }, err
// case "record":
// case keploy.MODE_RECORD:
// cursor, err = c.Collection.Find(ctx, filter, opts...)
// return &Cursor{
// Cursor: *cursor,
Expand Down
2 changes: 1 addition & 1 deletion integrations/kmongo/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (c *Collection) DeleteOne(ctx context.Context, filter interface{},
return output, err
}

// DeleteMany method mocks Collection.DeleteMany of mongo inorder to call it only in "record" or "off" mode.
// DeleteMany method mocks Collection.DeleteMany of mongo inorder to call it only in keploy.MODE_RECORD or "off" mode.
//
// See https://pkg.go.dev/go.mongodb.org/mongo-driver@v1.8.0/mongo#Collection.DeleteMany for information about Collection.DeleteMany.
func (c *Collection) DeleteMany(ctx context.Context, filter interface{},
Expand Down
10 changes: 5 additions & 5 deletions integrations/kmongo/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func (c *Collection) FindOne(ctx context.Context, filter interface{}, opts ...*o
var sr *mongo.SingleResult

switch mode {
case "test":
case keploy.MODE_TEST:
return singleResult
case "record":
case keploy.MODE_RECORD:
sr = c.Collection.FindOne(ctx, filter, opts...)
if sr != nil {
singleResult.SingleResult = *sr
Expand All @@ -57,7 +57,7 @@ func (c *Collection) FindOne(ctx context.Context, filter interface{}, opts ...*o
}

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

switch mode {
case "test":
case keploy.MODE_TEST:
//don't call method in test mode
return result, err
case "record":
case keploy.MODE_RECORD:
cursor, err = c.Collection.Find(ctx, filter, opts...)
if cursor != nil {
result.Cursor = *cursor
Expand Down
Loading

0 comments on commit ceb818f

Please sign in to comment.