Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added wait response behavior #6

Merged
merged 2 commits into from Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 (milliseconds)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be good to leave a more detailed description of this behaviour. Consider just copying the description from the official docs:

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