Skip to content

Commit

Permalink
fix: add support for binary body in http (keploy#160)
Browse files Browse the repository at this point in the history
* refactor: rename constants used in yaml parsing

Signed-off-by: slayerjain <[email protected]>

* feat: wip sql yaml export

Signed-off-by: slayerjain <[email protected]>

* feat: add sql spec for test export

Signed-off-by: re-Tick <[email protected]>

* fix: send request and response body in binary to record as mocks

Signed-off-by: re-Tick <[email protected]>

Signed-off-by: slayerjain <[email protected]>
Signed-off-by: re-Tick <[email protected]>
Signed-off-by: Ritik Jain <[email protected]>
Co-authored-by: slayerjain <[email protected]>
  • Loading branch information
re-Tick and slayerjain authored Jan 6, 2023
1 parent 5ec0fa3 commit 93c767d
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/gorilla/mux v1.8.0
github.com/lestrrat-go/jwx v1.2.25
github.com/valyala/fasthttp v1.40.0
go.keploy.io/server v0.7.10
go.keploy.io/server v0.7.12
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1
go.keploy.io/server v0.5.2/go.mod h1:TL7IcngMUjrpm9uoJbY7dfqfNNOqE5NSgQ+eHh3pp0I=
go.keploy.io/server v0.5.6/go.mod h1:/wHYlepZLITJ+MsYnRfRED9K/ENset2JhfzeAqSGnV0=
go.keploy.io/server v0.7.8/go.mod h1:Xm0Zzt2iBRrvoDY7fBMm8M+geV+ZlkknbqKRVjC3K0I=
go.keploy.io/server v0.7.10 h1:od2+svRb6I54YOQ/XkSKHbG1S7gvEMAEDn51IbsPyQw=
go.keploy.io/server v0.7.10/go.mod h1:ch4rD1NCgtxozDHD9yVk+sLHWz5HgefOqrgEdEIgfBQ=
go.keploy.io/server v0.7.12 h1:DKDSO6T9Q4d4A8MKL+sk7U26KRcvZ+ZG0mbFhYIJJyk=
go.keploy.io/server v0.7.12/go.mod h1:ch4rD1NCgtxozDHD9yVk+sLHWz5HgefOqrgEdEIgfBQ=
go.mongodb.org/mongo-driver v1.8.3 h1:TDKlTkGDKm9kkJVUOAXDK5/fkqKHJVwYQSpoRfB43R4=
go.mongodb.org/mongo-driver v1.8.3/go.mod h1:0sQWfOeY63QTntERDJJ/0SuKK0T1uVSgKCuAROlKEPY=
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
Expand Down
22 changes: 15 additions & 7 deletions integrations/khttpclient/httpClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,17 @@ func (i Interceptor) RoundTrip(r *http.Request) (*http.Response, error) {
if len(kctx.Mock) > 0 && kctx.Mock[0].Kind == string(models.HTTP) {
mocks := kctx.Mock
if len(mocks) > 0 && len(mocks[0].Spec.Objects) > 0 {
bin := string(mocks[0].Spec.Objects[0].Data)
if bin != "" {
err = errors.New(string(bin))
errStr := string(mocks[0].Spec.Objects[0].Data)
if errStr != "" {
err = errors.New(string(errStr))
}
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(mocks[0].Spec.Res.Body)))
bin := []byte{}
if mocks[0].Spec.Res.BodyData != nil {
bin = mocks[0].Spec.Res.BodyData
} else {
bin = []byte(mocks[0].Spec.Res.Body)
}
resp.Body = ioutil.NopCloser(bytes.NewBuffer(bin))
resp.Header = mock.GetHttpHeader(mocks[0].Spec.Res.Header)
resp.StatusCode = int(mocks[0].Spec.Res.StatusCode)
if kctx.FileExport {
Expand Down Expand Up @@ -194,7 +200,7 @@ func (i Interceptor) RoundTrip(r *http.Request) (*http.Response, error) {
errStr = err.Error()
}
httpMock := &proto.Mock{
Version: string(models.V1Beta1),
Version: string(models.V1Beta2),
Name: kctx.TestID,
Kind: string(models.HTTP),
Spec: &proto.Mock_SpecSchema{
Expand All @@ -211,12 +217,14 @@ func (i Interceptor) RoundTrip(r *http.Request) (*http.Response, error) {
ProtoMinor: int64(r.ProtoMinor),
URL: r.URL.String(),
Header: mock.GetProtoMap(r.Header),
Body: string(reqBody),
// Body: string(reqBody),
BodyData: reqBody,
},
Res: &proto.HttpResp{
StatusCode: int64(statusCode),
Header: mock.GetProtoMap(respHeader),
Body: string(respBody),
// Body: string(respBody),
BodyData: respBody,
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion integrations/ksql/v2/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func CaptureSqlMocks(kctx *internal.Context, log *zap.Logger, meta map[string]st
obj = []*proto.Mock_Object{}
)
sqlMock := &proto.Mock{
Version: string(models.V1Beta1),
Version: string(models.V1Beta2),
Name: kctx.TestID,
Kind: string(models.SQL),
Spec: &proto.Mock_SpecSchema{
Expand Down
9 changes: 3 additions & 6 deletions keploy/keploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,21 @@ import (
"encoding/json"
"errors"
"fmt"
"path/filepath"
"strings"

"github.com/keploy/go-sdk/mock"

"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"testing"
"time"

"github.com/creasty/defaults"
"github.com/go-playground/validator/v10"
"github.com/keploy/go-sdk/internal/keploy"

"github.com/keploy/go-sdk/mock"
proto "go.keploy.io/server/grpc/regression"
"go.keploy.io/server/http/regression"
"go.keploy.io/server/pkg/models"
Expand Down
4 changes: 2 additions & 2 deletions keploy/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func ProcessDep(ctx context.Context, log *zap.Logger, meta map[string]string, ou
}
if keploy.GetGrpcClient() != nil && kctx.FileExport && keploy.MockId.Unique(kctx.TestID) {
recorded := keploy.PutMock(ctx, keploy.MockPath, &proto.Mock{
Version: string(models.V1Beta1),
Version: string(models.V1Beta2),
Kind: string(models.GENERIC),
Name: kctx.TestID,
Spec: &proto.Mock_SpecSchema{
Expand All @@ -162,7 +162,7 @@ func ProcessDep(ctx context.Context, log *zap.Logger, meta map[string]string, ou
Meta: meta,
})
kctx.Mock = append(kctx.Mock, &proto.Mock{
Version: string(models.V1Beta1),
Version: string(models.V1Beta2),
Kind: string(models.GENERIC),
Name: "",
Spec: &proto.Mock_SpecSchema{
Expand Down

0 comments on commit 93c767d

Please sign in to comment.