Skip to content

Commit

Permalink
assured client testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Michael committed Aug 27, 2017
1 parent fd4e504 commit 0bcebcb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
28 changes: 21 additions & 7 deletions assured/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ func (c *Client) Port() int {
return port
}

// URL returns the url to use to test you stubbed endpoints
func (c *Client) URL() string {
return fmt.Sprintf("http://localhost:%d/when/", c.Port())
}

// Run starts the go-rest-assured service through the client
func (c *Client) Run() {
func (c *Client) Run() Client {
StartApplicationHTTPListener(c.Port(), c.logger, c.ctx, c.errc)
return *c
}

// Close is used to close the running service
Expand All @@ -56,16 +62,24 @@ func (c *Client) Given(call *Call) error {
var req *http.Request
var err error

if call.Method == "" {
return fmt.Errorf("cannot stub call without Method")
}

if call.Path == "" {
return fmt.Errorf("cannot stub call without Path")
}

if call.Response == nil {
req, err = http.NewRequest(call.Method, fmt.Sprintf("given/%s", call.Path), nil)
req, err = http.NewRequest(call.Method, fmt.Sprintf("http://localhost:%d/given/%s", c.Port(), call.Path), nil)
} else {
req, err = http.NewRequest(call.Method, fmt.Sprintf("given/%s", call.Path), bytes.NewReader(call.Response))
req, err = http.NewRequest(call.Method, fmt.Sprintf("http://localhost:%d/given/%s", c.Port(), call.Path), bytes.NewReader(call.Response))
}
if err != nil {
return err
}
if call.StatusCode != 0 {
req.Header.Set("Assert-Status", string(call.StatusCode))
req.Header.Set("Assured-Status", fmt.Sprintf("%d", call.StatusCode))
}

_, err = c.httpClient.Do(req)
Expand All @@ -74,7 +88,7 @@ func (c *Client) Given(call *Call) error {

// Verify returns all of the calls made against a stubbed method and path
func (c *Client) Verify(method, path string) ([]*Call, error) {
req, err := http.NewRequest(method, fmt.Sprintf("verify/%s", path), nil)
req, err := http.NewRequest(method, fmt.Sprintf("http://localhost:%d/verify/%s", c.Port(), path), nil)
if err != nil {
return nil, err
}
Expand All @@ -97,7 +111,7 @@ func (c *Client) Verify(method, path string) ([]*Call, error) {

// Clear assured calls for a Method and Path
func (c *Client) Clear(method, path string) error {
req, err := http.NewRequest(method, fmt.Sprintf("clear/%s", path), nil)
req, err := http.NewRequest(method, fmt.Sprintf("http://localhost:%d/clear/%s", c.Port(), path), nil)
if err != nil {
return err
}
Expand All @@ -107,7 +121,7 @@ func (c *Client) Clear(method, path string) error {

// ClearAll clears all assured calls
func (c *Client) ClearAll() error {
req, err := http.NewRequest(http.MethodDelete, "clear", nil)
req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("http://localhost:%d/clear", c.Port()), nil)
if err != nil {
return err
}
Expand Down
40 changes: 40 additions & 0 deletions assured/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package assured

import (
"bytes"
"io/ioutil"
"net/http"
"testing"

kitlog "github.com/go-kit/kit/log"
"github.com/stretchr/testify/require"
)

func TestClient(t *testing.T) {
httpClient := &http.Client{}
client := NewClient(kitlog.NewLogfmtLogger(ioutil.Discard), nil).Run()
url := client.URL()

require.NoError(t, client.Given(call1))
require.NoError(t, client.Given(call2))

req, err := http.NewRequest(http.MethodGet, url+"test/assured", bytes.NewReader([]byte(`{"calling":"you"}`)))
require.NoError(t, err)

hit1, err := httpClient.Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusOK, hit1.StatusCode)
body, err := ioutil.ReadAll(hit1.Body)
require.NoError(t, err)
require.Equal(t, []byte(`{"assured": true}`), body)

req, err = http.NewRequest(http.MethodGet, url+"test/assured", bytes.NewReader([]byte(`{"calling":"again"}`)))
require.NoError(t, err)

hit2, err := httpClient.Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusConflict, hit2.StatusCode)
body, err = ioutil.ReadAll(hit2.Body)
require.NoError(t, err)
require.Equal(t, []byte("error"), body)
}

0 comments on commit 0bcebcb

Please sign in to comment.