diff --git a/README-ru.md b/README-ru.md index 15c4b3d..4ce6af5 100644 --- a/README-ru.md +++ b/README-ru.md @@ -1493,6 +1493,22 @@ Example: ... ``` +##### dropRequest + +Стратегия, которая по умолчанию на любой запрос сбрасывает соединение. Используется для эмуляции проблем с сетью. + +Не имеет параметров. + +Пример: + +```yaml + ... + mocks: + service1: + strategy: dropRequest + ... +``` + #### Подсчет количества вызовов Вы можете указать, сколько раз должен быть вызван мок или отдельный ресурс мока (используя `uriVary`). Если фактическое количество вызовов будет отличаться от ожидаемого, тест будет считаться проваленным. diff --git a/README.md b/README.md index ae7edf8..63a81e3 100644 --- a/README.md +++ b/README.md @@ -1495,6 +1495,22 @@ Example: ... ``` +##### dropRequest + +The strategy that by default drops the connection on any request. Used to emulate the network problems. + +No parameters. + +Example: + +```yaml + ... + mocks: + service1: + strategy: dropRequest + ... +``` + #### Calls count You can define, how many times each mock or mock resource must be called (using `uriVary`). If the actual number of calls is different from expected, the test will be considered failed. diff --git a/gonkey.json b/gonkey.json index b7a0dcb..cbd625b 100644 --- a/gonkey.json +++ b/gonkey.json @@ -218,6 +218,10 @@ { "const": "sequence", "title": "With this strategy for each consequent request you will get a reply defined by a consequent nested strategy." + }, + { + "const": "dropRequest", + "title": "The strategy that by default drops the connection on any request. Used to emulate the network problems." } ] }, diff --git a/mocks/loader.go b/mocks/loader.go index cb78c64..6a7ac86 100644 --- a/mocks/loader.go +++ b/mocks/loader.go @@ -117,6 +117,8 @@ func (l *Loader) loadStrategy(path, strategyName string, definition map[interfac case "basedOnRequest": *ak = append(*ak, "basePath", "uris") return l.loadBasedOnRequestStrategy(path, definition) + case "dropRequest": + return l.loadDropRequestStrategy(path, definition) default: return nil, fmt.Errorf("unknown strategy: %s", strategyName) } @@ -204,6 +206,10 @@ func (l *Loader) loadConstantStrategy(path string, def map[interface{}]interface return NewConstantReplyWithCode([]byte(body), statusCode, headers), nil } +func (l *Loader) loadDropRequestStrategy(path string, def map[interface{}]interface{}) (ReplyStrategy, error) { + return NewDropRequestReply(), nil +} + func (l *Loader) loadTemplateStrategy(path string, def map[interface{}]interface{}) (ReplyStrategy, error) { c, ok := def["body"] if !ok { diff --git a/mocks/reply_strategy.go b/mocks/reply_strategy.go index 4a08981..74366ac 100644 --- a/mocks/reply_strategy.go +++ b/mocks/reply_strategy.go @@ -62,6 +62,26 @@ func (s *constantReply) HandleRequest(w http.ResponseWriter, r *http.Request) [] return nil } +type dropRequestReply struct { +} + +func NewDropRequestReply() ReplyStrategy { + return &dropRequestReply{} +} + +func (s *dropRequestReply) HandleRequest(w http.ResponseWriter, r *http.Request) []error { + hj, ok := w.(http.Hijacker) + if !ok { + return []error{fmt.Errorf("Gonkey internal error during drop request: webserver doesn't support hijacking\n")} + } + conn, _, err := hj.Hijack() + if err != nil { + return []error{fmt.Errorf("Gonkey internal error during connection hijacking: %s\n", err)} + } + conn.Close() + return nil +} + type failReply struct{} func (s *failReply) HandleRequest(w http.ResponseWriter, r *http.Request) []error {