Skip to content

Commit

Permalink
Merge pull request #195 from Alexey19/drop_request_strategy
Browse files Browse the repository at this point in the history
new: introduce dropRequest strategy
  • Loading branch information
fetinin authored Jan 17, 2023
2 parents e529fc4 + 82930f2 commit 87cad3e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README-ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,22 @@ Example:
...
```

##### dropRequest

Стратегия, которая по умолчанию на любой запрос сбрасывает соединение. Используется для эмуляции проблем с сетью.

Не имеет параметров.

Пример:

```yaml
...
mocks:
service1:
strategy: dropRequest
...
```

#### Подсчет количества вызовов

Вы можете указать, сколько раз должен быть вызван мок или отдельный ресурс мока (используя `uriVary`). Если фактическое количество вызовов будет отличаться от ожидаемого, тест будет считаться проваленным.
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions gonkey.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
]
},
Expand Down
6 changes: 6 additions & 0 deletions mocks/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions mocks/reply_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 87cad3e

Please sign in to comment.