Skip to content

Commit

Permalink
added support for delay in mocked response
Browse files Browse the repository at this point in the history
  • Loading branch information
s0s01qp committed Jul 28, 2021
1 parent cbc7087 commit 38a26af
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
3 changes: 3 additions & 0 deletions controllers/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/valyala/fasthttp"
"net/http"
"time"

"github.com/sinhashubham95/moxy/commons"
"github.com/sinhashubham95/moxy/persistence/entities"
Expand All @@ -26,6 +27,8 @@ func Handle(ctx *fasthttp.RequestCtx) {
// try to fetch the mock
err := PersistenceView(&mock)
if err == nil {
// check if we need to delay sending the response
time.Sleep(time.Millisecond * time.Duration(mock.DelayInMillis))
// this means that the mock exists
ctx.SetStatusCode(mock.Status)
// writing the response here is tricky
Expand Down
11 changes: 6 additions & 5 deletions controllers/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ func HandleMock(ctx *fasthttp.RequestCtx) {
(&request).Default()
// now time to save it
err = PersistenceSave(&entities.Mock{
Tag: request.Tag,
Method: request.Method,
Path: request.Path,
Status: request.ResponseStatus,
Body: request.ResponseBody,
Tag: request.Tag,
Method: request.Method,
Path: request.Path,
DelayInMillis: request.ResponseDelayInMillis,
Status: request.ResponseStatus,
Body: request.ResponseBody,
})
if err != nil {
// unable to save the mock configurations
Expand Down
11 changes: 6 additions & 5 deletions models/mockrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (

// MockRequest is the request body for the mock endpoint
type MockRequest struct {
Tag string `json:"tag"`
Method string `json:"method"`
Path string `json:"path"`
ResponseStatus int `json:"responseStatus"`
ResponseBody interface{} `json:"responseBody"`
Tag string `json:"tag"`
Method string `json:"method"`
Path string `json:"path"`
ResponseDelayInMillis int64 `json:"responseDelayInMillis"`
ResponseStatus int `json:"responseStatus"`
ResponseBody interface{} `json:"responseBody"`
}

// Clean is used to clean the request path
Expand Down
11 changes: 6 additions & 5 deletions persistence/entities/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ type MockKey struct {

// Mock is the mock entity
type Mock struct {
Tag string `json:"tag"`
Method string `json:"method"`
Path string `json:"path"`
Status int `json:"status"`
Body interface{} `json:"body"`
Tag string `json:"tag"`
Method string `json:"method"`
Path string `json:"path"`
DelayInMillis int64 `json:"delayInMillis"`
Status int `json:"status"`
Body interface{} `json:"body"`
}

// Name is the mock entity name
Expand Down
13 changes: 7 additions & 6 deletions persistence/entities/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ func TestMockKey(t *testing.T) {

func TestMockEncode(t *testing.T) {
bytes, err := (&entities.Mock{
Tag: "1234",
Method: "GET",
Path: "/naruto",
Status: 200,
Body: "naruto-rocks",
Tag: "1234",
Method: "GET",
Path: "/naruto",
DelayInMillis: 200,
Status: 200,
Body: "naruto-rocks",
}).Encode()
assert.NoError(t, err)
assert.Equal(
t,
[]byte("{\"tag\":\"1234\",\"method\":\"GET\",\"path\":"+
"\"/naruto\",\"status\":200,\"body\":\"naruto-rocks\"}\n"),
"\"/naruto\",\"delayInMillis\":200,\"status\":200,\"body\":\"naruto-rocks\"}\n"),
bytes,
)
}
Expand Down

0 comments on commit 38a26af

Please sign in to comment.