Skip to content

Commit 09ecbe8

Browse files
committed
add Reset api method
1 parent 44beebf commit 09ecbe8

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ docker run -it --rm -p 8080:8080 rodolpheche/wiremock
1313
```
1414
```go
1515
wiremockClient := NewClient("http://0.0.0.0:8080")
16-
defer wiremockClient.Clear()
16+
defer wiremockClient.Reset()
1717
wiremockClient.StubFor(Post(URLPathEqualTo("/example")).
1818
WithQueryParam("firstName", EqualTo("Jhon")).
1919
WithQueryParam("lastName", NotMatching("Black")).
@@ -24,4 +24,6 @@ wiremockClient.StubFor(Post(URLPathEqualTo("/example")).
2424
map[string]string{"Content-Type": "application/json"},
2525
400,
2626
))
27+
28+
// testing code...
2729
```

client.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func NewClient(url string) *Client {
2020
return &Client{url: url}
2121
}
2222

23-
// StubFor sends http request with StubRule to wiremock server.
23+
// StubFor creates a new stub mapping.
2424
func (c *Client) StubFor(stubRule *StubRule) error {
2525
requestBody, err := json.Marshal(stubRule)
2626
if err != nil {
@@ -44,7 +44,7 @@ func (c *Client) StubFor(stubRule *StubRule) error {
4444
return nil
4545
}
4646

47-
// Clear sends http request to wiremock server for delete all mappings.
47+
// Clear deletes all stub mappings.
4848
func (c *Client) Clear() error {
4949
req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("%s/%s", c.url, wiremockAdminURN), nil)
5050
if err != nil {
@@ -62,3 +62,22 @@ func (c *Client) Clear() error {
6262

6363
return nil
6464
}
65+
66+
// Reset restores stub mappings to the defaults defined back in the backing store.
67+
func (c *Client) Reset() error {
68+
res, err := http.Post(fmt.Sprintf("%s/%s/reset", c.url, wiremockAdminURN), "application/json", nil)
69+
if err != nil {
70+
return fmt.Errorf("reset request error: %s", err.Error())
71+
}
72+
73+
if res.StatusCode != http.StatusOK {
74+
bodyBytes, err := ioutil.ReadAll(res.Body)
75+
if err != nil {
76+
return fmt.Errorf("read response error: %s", err.Error())
77+
}
78+
79+
return fmt.Errorf("bad response status: %d, response: %s", res.StatusCode, string(bodyBytes))
80+
}
81+
82+
return nil
83+
}

stub_rule.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55
"net/http"
66
)
77

8-
// ParamMatcherInterface is pair ParamMatchingStrategy key and string value
8+
// ParamMatcherInterface is pair ParamMatchingStrategy and string matched value
99
type ParamMatcherInterface interface {
1010
Strategy() ParamMatchingStrategy
1111
Value() string
1212
}
1313

14-
// URLMatcherInterface is pair URLMatchingStrategy key and string value
14+
// URLMatcherInterface is pair URLMatchingStrategy and string matched value
1515
type URLMatcherInterface interface {
1616
Strategy() URLMatchingStrategy
1717
Value() string

0 commit comments

Comments
 (0)