Skip to content

Commit

Permalink
Merge pull request #6 from senseyeio/add-wait-response-behaviour
Browse files Browse the repository at this point in the history
added wait response behavior
  • Loading branch information
smotes committed Jan 18, 2019
2 parents 7f3e038 + 3e62c16 commit 88c4c62
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
30 changes: 28 additions & 2 deletions imposter.go
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
)

const behaviorsKey = "_behaviors"

func parseHybridAddress(s string) (ip net.IP, err error) {
parts := strings.Split(s, ":")
ipStr := strings.Join(parts[0:len(parts)-1], ":")
Expand Down Expand Up @@ -309,6 +311,17 @@ type Response struct {
Type string
// Value is the value of the Response; either of type HTTPResponse or TCPResponse.
Value interface{}
// Behaviors is an optional field allowing the user to define response behavior.
Behaviors *Behaviors
}

// Behaviors defines the possible response behaviors for a stub.
// Currently supported values are:
// wait - Adds latency to a response by waiting a specified number of milliseconds before sending the response.
// See more information on stub response behaviors in mountebank at:
// http://www.mbtest.org/docs/api/behaviors.
type Behaviors struct {
Wait int `json:"wait,omitempty"`
}

func getResponseSubTypeDTO(v interface{}) (interface{}, error) {
Expand Down Expand Up @@ -342,6 +355,15 @@ func (r Response) toDTO() (responseDTO, error) {
}

dto[r.Type] = b

if r.Behaviors != nil {
behaviors, err := json.Marshal(r.Behaviors)
if err != nil {
return dto, err
}
dto[behaviorsKey] = behaviors
}

return dto, nil
}

Expand All @@ -356,8 +378,12 @@ type responseDTO map[string]json.RawMessage
// network protocol proto - currently either type HTTPResponse or TCPResponse.
func (dto responseDTO) unmarshalProto(proto string) (resp Response, err error) {
if len(dto) != 1 {
err = errors.New("unexpected Predicate JSON structure")
return
if len(dto) == 2 {
if _, ok := dto[behaviorsKey]; !ok {
err = errors.New("unexpected Predicate JSON structure")
return
}
}
}

for key, b := range dto {
Expand Down
6 changes: 6 additions & 0 deletions imposter_test.go
Expand Up @@ -111,6 +111,9 @@ func TestImposter_MarshalJSON(t *testing.T) {
},
Body: `{"test":true}`,
},
Behaviors: &mbgo.Behaviors{
Wait: 500,
},
},
},
},
Expand Down Expand Up @@ -149,6 +152,9 @@ func TestImposter_MarshalJSON(t *testing.T) {
},
"body": `{"test":true}`,
},
"_behaviors": map[string]interface{}{
"wait": 500,
},
},
},
},
Expand Down

0 comments on commit 88c4c62

Please sign in to comment.