forked from durmaze/gobank
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds return values to the Mountebank Client
- Loading branch information
Showing
3 changed files
with
138 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,76 @@ | ||
package mountebank | ||
|
||
import ( | ||
"strconv" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"strconv" | ||
|
||
. "github.com/durmaze/gobank/builders" | ||
"github.com/parnurzeal/gorequest" | ||
) | ||
|
||
type Client struct{ | ||
type Client struct { | ||
mountebankUri string | ||
impostersUri string | ||
impostersUri string | ||
} | ||
|
||
func NewClient(mountebankUri string) *Client { | ||
return &Client{ | ||
mountebankUri: mountebankUri, | ||
impostersUri: mountebankUri + "/imposters", | ||
impostersUri: mountebankUri + "/imposters", | ||
} | ||
} | ||
|
||
func (c *Client) CreateImposter(imposter Imposter) { | ||
gorequest.New().Post(c.impostersUri).Send(imposter).End() | ||
func (c *Client) CreateImposter(imposter Imposter) (map[string]interface{}, error) { | ||
resp, body, errs := gorequest.New().Post(c.impostersUri).Send(imposter).EndBytes() | ||
|
||
if len(errs) > 0 { | ||
return nil, errs[0] | ||
} | ||
|
||
if resp.StatusCode == http.StatusCreated { | ||
var created map[string]interface{} | ||
json.Unmarshal(body, &created) | ||
|
||
return created, nil | ||
} | ||
|
||
return nil, errors.New("Cannot create the imposter") | ||
} | ||
|
||
func (c *Client) DeleteImposter(imposter Imposter) { | ||
imposterUri := c.impostersUri + "/" + strconv.Itoa(imposter.Port) | ||
func (c *Client) DeleteImposter(port int) (map[string]interface{}, error) { | ||
imposterUri := c.impostersUri + "/" + strconv.Itoa(port) | ||
|
||
resp, body, errs := gorequest.New().Delete(imposterUri).EndBytes() | ||
|
||
if len(errs) > 0 { | ||
return nil, errs[0] | ||
} | ||
|
||
if resp.StatusCode == http.StatusOK { | ||
var deleted map[string]interface{} | ||
json.Unmarshal(body, &deleted) | ||
|
||
return deleted, nil | ||
} | ||
|
||
gorequest.New().Delete(imposterUri).End() | ||
return nil, errors.New("Cannot delete the imposter") | ||
} | ||
|
||
func (c *Client) DeleteAllImposters() { | ||
gorequest.New().Delete(c.impostersUri).End() | ||
} | ||
func (c *Client) DeleteAllImposters() (map[string]interface{}, error) { | ||
resp, body, errs := gorequest.New().Delete(c.impostersUri).EndBytes() | ||
|
||
if len(errs) > 0 { | ||
return nil, errs[0] | ||
} | ||
|
||
if resp.StatusCode == http.StatusOK { | ||
var allDeleted map[string]interface{} | ||
json.Unmarshal(body, &allDeleted) | ||
|
||
return allDeleted, nil | ||
} | ||
|
||
return nil, errors.New("Cannot delete all of the imposters") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters