diff --git a/mockns1/testcase.go b/mockns1/testcase.go index d6c3d56..06ef323 100644 --- a/mockns1/testcase.go +++ b/mockns1/testcase.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/stretchr/testify/assert" + api "gopkg.in/ns1/ns1-go.v2/rest" ) type testCase struct { @@ -29,6 +30,7 @@ func (s *Service) AddTestCase( method, uri string, returnStatus int, requestHeaders, responseHeaders http.Header, requestBody, responseBody interface{}, + params ...api.Param, ) error { s.stopTimer() defer s.startTimer() @@ -36,6 +38,15 @@ func (s *Service) AddTestCase( if !strings.HasPrefix(uri, "/v1/") { uri = "/v1/" + uri } + + if len(params) > 0 { + uri = fmt.Sprintf("%s?%s=%s", uri, params[0].Key, params[0].Value) + + for _, p := range params[1:] { + uri = fmt.Sprintf("%s&%s=%s", uri, p.Key, p.Value) + } + } + uri = strings.Replace(uri, "//", "/", -1) tc := &testCase{ diff --git a/rest/client.go b/rest/client.go index e8846f2..d047142 100644 --- a/rest/client.go +++ b/rest/client.go @@ -187,10 +187,22 @@ func SetDDIAPI() func(*Client) { return func(c *Client) { c.DDI = true } } +// Param is a container struct which holds a `Key` and `Value` field corresponding to the values of a URL parameter. +type Param struct { + Key, Value string +} + // Do satisfies the Doer interface. resp will be nil if a non-HTTP error // occurs, otherwise it is available for inspection when the error reflects a -// non-2XX response. -func (c Client) Do(req *http.Request, v interface{}) (*http.Response, error) { +// non-2XX response. It accepts a variadic number of optional URL parameters to +// supply to the request. URL parameters are of type `rest.Param`. +func (c Client) Do(req *http.Request, v interface{}, params ...Param) (*http.Response, error) { + q := req.URL.Query() + for _, p := range params { + q.Set(p.Key, p.Value) + } + req.URL.RawQuery = q.Encode() + resp, err := c.httpClient.Do(req) if err != nil { return nil, err @@ -229,9 +241,11 @@ type NextFunc func(v *interface{}, uri string) (*http.Response, error) // DoWithPagination Does, and follows Link headers for pagination. The returned // Response is from the last URI visited - either the last page, or one that // responded with a non-2XX status. If a non-HTTP error occurs, resp will be -// nil. -func (c Client) DoWithPagination(req *http.Request, v interface{}, f NextFunc) (*http.Response, error) { - resp, err := c.Do(req, v) +// nil. It accepts a variadic number of optional URL parameters to supply to +// the underlying `.Do()` method request(s). URL parameters are of type +// `rest.Param`. +func (c Client) DoWithPagination(req *http.Request, v interface{}, f NextFunc, params ...Param) (*http.Response, error) { + resp, err := c.Do(req, v, params...) if err != nil { return resp, err }