Skip to content

Commit

Permalink
feat(mux) : Adds support for Gorilla/Mux multiplexer and refactor mon…
Browse files Browse the repository at this point in the history
…go (#67)

* feat(mongo): Adds Distinct, CountDocuments and Aggregate methods to mock their outputs.

* fix(keploy): adds a delay in between capture and denoise calls to keploy server

delay is necessary to capture the noise fields of response accurately

* fix(kmongo): adds a check for nil pointer to prevent crash in find, findone mocked method

refactors kmongo code to improve maintainence #63

* feat(kmux): adds a middleware function for gorilla/mux multiplexer #66

Co-authored-by: Ritik Jain <ritikjain@Ritiks-MacBook-Air.local>
  • Loading branch information
re-Tick and Ritik Jain authored Mar 6, 2022
1 parent 9c37af0 commit 154fc96
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 2 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,33 @@ kwebgo.WebGoV4(k
, router)
router.Start()
```
### 5. Gorilla/Mux
```go
r := mux.NewRouter()
kmux.Mux(k, r)
```
#### Example
```go
import(
"github.com/keploy/go-sdk/integrations/kmux"
"net/http"
)

r := mux.NewRouter()
port := "8080"
k := keploy.New(keploy.Config{
App: keploy.AppConfig{
Name: "my-app",
Port: port,
},
Server: keploy.ServerConfig{
URL: "http://localhost:8081/api",
},
})
kmux.Mux(k, r)
http.ListenAndServe(":"+port, r)
```

## Supported Databases
### 1. MongoDB
```go
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/golang/protobuf v1.4.3 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/mux v1.8.0
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.9 // indirect
github.com/klauspost/compress v1.13.6 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
Expand Down
100 changes: 100 additions & 0 deletions integrations/kmux/mux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package kmux

import (
"bytes"
"context"
"io"
"io/ioutil"
"net/http"

"github.com/gorilla/mux"
"github.com/keploy/go-sdk/keploy"
"go.keploy.io/server/pkg/models"
"go.uber.org/zap"
)

// Mux adds keploy instrumentation for Mux router.
// It should be ideally used after other instrumentation libraries like APMs.
//
// k is the Keploy instance
//
// w is the mux router instance
func Mux(k *keploy.Keploy, w *mux.Router) {
if keploy.GetMode() == keploy.MODE_OFF {
return
}
w.Use(mw(k))
}

func captureRespMux(w http.ResponseWriter, r *http.Request, next http.Handler) models.HttpResp {
resBody := new(bytes.Buffer)
mw := io.MultiWriter(w, resBody)
writer := &keploy.BodyDumpResponseWriter{
Writer: mw,
ResponseWriter: w,
Status: http.StatusOK,
}
w = writer

next.ServeHTTP(w, r)
return models.HttpResp{
//Status

StatusCode: writer.Status,
Header: w.Header(),
Body: resBody.String(),
}
}

func mw(k *keploy.Keploy) func( http.Handler) http.Handler {
if k == nil {
return func(next http.Handler) http.Handler{
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
})
}
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id := r.Header.Get("KEPLOY_TEST_ID")
if id != "" {
// id is only present during simulation
// run it similar to how testcases would run
ctx := context.WithValue(r.Context(), keploy.KCTX, &keploy.Context{
Mode: "test",
TestID: id,
Deps: k.GetDependencies(id),
})

r = r.WithContext(ctx)
resp := captureRespMux(w, r, next)
k.PutResp(id, resp)
return
}
ctx := context.WithValue(r.Context(), keploy.KCTX, &keploy.Context{
Mode: "capture",
})

r = r.WithContext(ctx)

// Request
var reqBody []byte
var err error
if r.Body != nil { // Read
reqBody, err = ioutil.ReadAll(r.Body)
if err != nil {
// TODO right way to log errors
k.Log.Error("Unable to read request body", zap.Error(err))
return
}
}
r.Body = ioutil.NopCloser(bytes.NewBuffer(reqBody)) // Reset

resp := captureRespMux(w, r, next)
// params := webgo.Context(r).Params()
params := mux.Vars(r)
keploy.CaptureTestcase(k, r, reqBody, resp, params)

})
}
}
4 changes: 2 additions & 2 deletions keploy/keploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ type Keploy struct {
Log *zap.Logger
client *http.Client
deps sync.Map
//Deps map[string][]models.Dependency
resp sync.Map
//Deps map[string][]models.Dependency
resp sync.Map
//Resp map[string]models.HttpResp
}

Expand Down

0 comments on commit 154fc96

Please sign in to comment.