diff --git a/stub/duration.go b/stub/duration.go new file mode 100644 index 00000000..e9762eab --- /dev/null +++ b/stub/duration.go @@ -0,0 +1,37 @@ +package stub + +import ( + "encoding/json" + "errors" + "time" +) + +type Duration struct { + time.Duration +} + +func (d Duration) MarshalJSON() ([]byte, error) { + return json.Marshal(d.String()) +} + +func (d *Duration) UnmarshalJSON(b []byte) error { + var v interface{} + if err := json.Unmarshal(b, &v); err != nil { + return err + } + switch value := v.(type) { + case float64: + d.Duration = time.Duration(value) + return nil + case string: + var err error + d.Duration, err = time.ParseDuration(value) + if err != nil { + return err + } + return nil + default: + return errors.New("invalid duration") + } +} + diff --git a/stub/storage.go b/stub/storage.go index 38785024..d890769f 100644 --- a/stub/storage.go +++ b/stub/storage.go @@ -8,6 +8,7 @@ import ( "reflect" "regexp" "sync" + "time" "github.com/lithammer/fuzzysearch/fuzzy" ) @@ -71,6 +72,7 @@ func findStub(stub *findStubPayload) (*Output, error) { if expect := stubrange.Input.Equals; expect != nil { closestMatch = append(closestMatch, closeMatch{"equals", expect}) if equals(stub.Data, expect) { + time.Sleep(stubrange.Output.Delay.Duration) return &stubrange.Output, nil } } @@ -78,6 +80,7 @@ func findStub(stub *findStubPayload) (*Output, error) { if expect := stubrange.Input.Contains; expect != nil { closestMatch = append(closestMatch, closeMatch{"contains", expect}) if contains(stubrange.Input.Contains, stub.Data) { + time.Sleep(stubrange.Output.Delay.Duration) return &stubrange.Output, nil } } @@ -85,6 +88,7 @@ func findStub(stub *findStubPayload) (*Output, error) { if expect := stubrange.Input.Matches; expect != nil { closestMatch = append(closestMatch, closeMatch{"matches", expect}) if matches(stubrange.Input.Matches, stub.Data) { + time.Sleep(stubrange.Output.Delay.Duration) return &stubrange.Output, nil } } diff --git a/stub/stub.go b/stub/stub.go index 819b317f..514b1fcf 100644 --- a/stub/stub.go +++ b/stub/stub.go @@ -3,12 +3,11 @@ package stub import ( "encoding/json" "fmt" + "github.com/go-chi/chi" "io/ioutil" "log" "net/http" "strings" - - "github.com/go-chi/chi" ) type Options struct { @@ -60,6 +59,7 @@ type Input struct { } type Output struct { + Delay Duration `json:"delay"` Data map[string]interface{} `json:"data"` Error string `json:"error"` } diff --git a/stub/stub_test.go b/stub/stub_test.go index 9871f6ef..f79f4ef8 100644 --- a/stub/stub_test.go +++ b/stub/stub_test.go @@ -31,9 +31,11 @@ func TestStub(t *testing.T) { } }, "output":{ + "delay": "1s", "data":{ "Hello":"World" } + } }` read := bytes.NewReader([]byte(payload)) @@ -48,7 +50,7 @@ func TestStub(t *testing.T) { return httptest.NewRequest("GET", "/", nil) }, handler: listStub, - expect: "{\"Testing\":{\"TestMethod\":[{\"Input\":{\"equals\":{\"Hola\":\"Mundo\"},\"contains\":null,\"matches\":null},\"Output\":{\"data\":{\"Hello\":\"World\"},\"error\":\"\"}}]}}\n", + expect: "{\"Testing\":{\"TestMethod\":[{\"Input\":{\"equals\":{\"Hola\":\"Mundo\"},\"contains\":null,\"matches\":null},\"Output\":{\"delay\":\"1s\",\"data\":{\"Hello\":\"World\"},\"error\":\"\"}}]}}\n", }, { name: "find stub equals", @@ -57,7 +59,7 @@ func TestStub(t *testing.T) { return httptest.NewRequest("POST", "/find", bytes.NewReader([]byte(payload))) }, handler: handleFindStub, - expect: "{\"data\":{\"Hello\":\"World\"},\"error\":\"\"}\n", + expect: "{\"delay\":\"1s\",\"data\":{\"Hello\":\"World\"},\"error\":\"\"}\n", }, { name: "add stub contains", @@ -97,7 +99,7 @@ func TestStub(t *testing.T) { return httptest.NewRequest("GET", "/find", bytes.NewReader([]byte(payload))) }, handler: handleFindStub, - expect: "{\"data\":{\"hello\":\"world\"},\"error\":\"\"}\n", + expect: "{\"delay\":\"0s\",\"data\":{\"hello\":\"world\"},\"error\":\"\"}\n", }, { name: "add stub matches regex", mock: func() *http.Request { @@ -132,7 +134,7 @@ func TestStub(t *testing.T) { return httptest.NewRequest("GET", "/find", bytes.NewReader([]byte(payload))) }, handler: handleFindStub, - expect: "{\"data\":{\"reply\":\"OK\"},\"error\":\"\"}\n", + expect: "{\"delay\":\"0s\",\"data\":{\"reply\":\"OK\"},\"error\":\"\"}\n", }, { name: "error find stub contains", mock: func() *http.Request {