Skip to content

Commit

Permalink
sanitize path on client
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Michael committed Dec 12, 2017
1 parent b23211b commit fec602d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions assured/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"net"
"net/http"
"strings"

kitlog "github.com/go-kit/kit/log"
)
Expand Down Expand Up @@ -72,6 +73,9 @@ func (c *Client) Given(calls ...Call) error {
return fmt.Errorf("cannot stub call without Method")
}

// Sanitize Path
call.Path = strings.Trim(call.Path, "/")

if call.Response == nil {
req, err = http.NewRequest(call.Method, fmt.Sprintf("http://localhost:%d/given/%s", c.Port, call.Path), nil)
} else {
Expand Down
14 changes: 14 additions & 0 deletions assured/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,17 @@ func TestClientVerifyBodyFailure(t *testing.T) {
require.Equal(t, `json: cannot unmarshal string into Go value of type []assured.Call`, err.Error())
require.Nil(t, calls)
}

func TestClientPathSanitization(t *testing.T) {
httpClient := &http.Client{}
client := NewDefaultClient()

require.NoError(t, client.Given(Call{Method: "GET", Path: "///yoyo/path///", StatusCode: http.StatusAccepted}))

req, err := http.NewRequest(http.MethodGet, client.URL()+"/yoyo/path", nil)
require.NoError(t, err)

resp, err := httpClient.Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusAccepted, resp.StatusCode)
}

0 comments on commit fec602d

Please sign in to comment.