Skip to content

Commit

Permalink
test: refactor client test to use the requester interface
Browse files Browse the repository at this point in the history
  • Loading branch information
IronCore864 committed Jan 8, 2025
1 parent 4f96724 commit 710ea03
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
37 changes: 30 additions & 7 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package client_test

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -101,7 +102,12 @@ func (cs *clientSuite) TestNewBaseURLError(c *C) {

func (cs *clientSuite) TestClientDoReportsErrors(c *C) {
cs.err = errors.New("ouchie")
err := cs.cli.Do("GET", "/", nil, nil, nil)
_, err := cs.cli.Requester().Do(context.Background(), &client.RequestOptions{
Type: client.RawRequest,
Method: "GET",
Path: "/",
})
c.Assert(err, NotNil)
c.Check(err, ErrorMatches, "cannot communicate with server: ouchie")
if cs.doCalls < 2 {
c.Fatalf("do did not retry")
Expand All @@ -125,9 +131,17 @@ func (cs *clientSuite) TestContextCancellation(c *C) {

func (cs *clientSuite) TestClientWorks(c *C) {
var v []int
cs.rsp = `[1,2]`
cs.rsp = `[1, 2]`
reqBody := io.NopCloser(strings.NewReader(""))
err := cs.cli.Do("GET", "/this", nil, reqBody, &v)
resp, err := cs.cli.Requester().Do(context.Background(), &client.RequestOptions{
Type: client.RawRequest,
Method: "GET",
Path: "/this",
Body: reqBody,
})
c.Check(err, IsNil)
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&v)
c.Check(err, IsNil)
c.Check(v, DeepEquals, []int{1, 2})
c.Assert(cs.req, NotNil)
Expand All @@ -138,8 +152,11 @@ func (cs *clientSuite) TestClientWorks(c *C) {
}

func (cs *clientSuite) TestClientDefaultsToNoAuthorization(c *C) {
var v string
_ = cs.cli.Do("GET", "/this", nil, nil, &v)
_, _ = cs.cli.Requester().Do(context.Background(), &client.RequestOptions{
Type: client.RawRequest,
Method: "GET",
Path: "/this",
})
c.Assert(cs.req, NotNil)
authorization := cs.req.Header.Get("Authorization")
c.Check(authorization, Equals, "")
Expand Down Expand Up @@ -288,9 +305,15 @@ func (cs *clientSuite) TestUserAgent(c *C) {
c.Assert(err, IsNil)
cli.SetDoer(cs)

resp, err := cli.Requester().Do(context.Background(), &client.RequestOptions{
Type: client.RawRequest,
Method: "GET",
Path: "/",
})
c.Assert(err, IsNil)
var v string
_ = cli.Do("GET", "/", nil, nil, &v)
c.Assert(cs.req, NotNil)
err = resp.DecodeResult(&v)
c.Assert(err, NotNil)
c.Check(cs.req.Header.Get("User-Agent"), Equals, "some-agent/9.87")
}

Expand Down
25 changes: 0 additions & 25 deletions client/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
package client

import (
"context"
"fmt"
"io"
"net/url"
)

var (
Expand All @@ -30,28 +27,6 @@ func (client *Client) SetDoer(d doer) {
client.Requester().(*defaultRequester).doer = d
}

// TODO: Clean up tests to use the new Requester API. Tests do not generate a client.response type
// reply in the body while SyncRequest or AsyncRequest responses assume the JSON body can be
// unmarshalled into client.response.
func (client *Client) Do(method, path string, query url.Values, body io.Reader, v interface{}) error {
resp, err := client.Requester().Do(context.Background(), &RequestOptions{
Type: RawRequest,
Method: method,
Path: path,
Query: query,
Headers: nil,
Body: body,
})
if err != nil {
return err
}
err = decodeInto(resp.Body, v)
if err != nil {
return err
}
return nil
}

func (client *Client) FakeAsyncRequest() (changeId string, err error) {
resp, err := client.doAsync("GET", "/v1/async-test", nil, nil, nil, nil)
if err != nil {
Expand Down

0 comments on commit 710ea03

Please sign in to comment.