client: minor test improvements for requests

- use assert.Check() where possible to not fail early
- improve checks for error-types
- rename "testURL" var to be more descriptive, and use a const

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-07-13 11:40:00 +02:00
parent f7f0a17ea2
commit 1370b3c679
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -21,7 +21,7 @@ import (
// TestSetHostHeader should set fake host for local communications, set real host // TestSetHostHeader should set fake host for local communications, set real host
// for normal communications. // for normal communications.
func TestSetHostHeader(t *testing.T) { func TestSetHostHeader(t *testing.T) {
const testURL = "/test" const testEndpoint = "/test"
testCases := []struct { testCases := []struct {
host string host string
expectedHost string expectedHost string
@ -57,8 +57,8 @@ func TestSetHostHeader(t *testing.T) {
client := &Client{ client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, testURL) { if !strings.HasPrefix(req.URL.Path, testEndpoint) {
return nil, fmt.Errorf("expected URL %q, got %q", testURL, req.URL) return nil, fmt.Errorf("expected URL %q, got %q", testEndpoint, req.URL)
} }
if req.Host != tc.expectedHost { if req.Host != tc.expectedHost {
return nil, fmt.Errorf("wxpected host %q, got %q", tc.expectedHost, req.Host) return nil, fmt.Errorf("wxpected host %q, got %q", tc.expectedHost, req.Host)
@ -77,7 +77,7 @@ func TestSetHostHeader(t *testing.T) {
basePath: hostURL.Path, basePath: hostURL.Path,
} }
_, err = client.sendRequest(context.Background(), http.MethodGet, testURL, nil, nil, nil) _, err = client.sendRequest(context.Background(), http.MethodGet, testEndpoint, nil, nil, nil)
assert.Check(t, err) assert.Check(t, err)
}) })
} }
@ -98,9 +98,11 @@ func TestInfiniteError(t *testing.T) {
infinitR := rand.New(rand.NewSource(42)) infinitR := rand.New(rand.NewSource(42))
client := &Client{ client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
resp := &http.Response{StatusCode: http.StatusInternalServerError} resp := &http.Response{
resp.Header = http.Header{} StatusCode: http.StatusInternalServerError,
resp.Body = io.NopCloser(infinitR) Header: http.Header{},
Body: io.NopCloser(infinitR),
}
return resp, nil return resp, nil
}), }),
} }
@ -110,32 +112,30 @@ func TestInfiniteError(t *testing.T) {
} }
func TestCanceledContext(t *testing.T) { func TestCanceledContext(t *testing.T) {
testURL := "/test" const testEndpoint = "/test"
client := &Client{ client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
assert.Equal(t, req.Context().Err(), context.Canceled) assert.Check(t, is.ErrorType(req.Context().Err(), context.Canceled))
return nil, context.Canceled
return &http.Response{}, context.Canceled
}), }),
} }
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
cancel() cancel()
_, err := client.sendRequest(ctx, http.MethodGet, testURL, nil, nil, nil) _, err := client.sendRequest(ctx, http.MethodGet, testEndpoint, nil, nil, nil)
assert.Equal(t, true, errdefs.IsCancelled(err)) assert.Check(t, is.ErrorType(err, errdefs.IsCancelled))
assert.Equal(t, true, errors.Is(err, context.Canceled)) assert.Check(t, errors.Is(err, context.Canceled))
} }
func TestDeadlineExceededContext(t *testing.T) { func TestDeadlineExceededContext(t *testing.T) {
testURL := "/test" const testEndpoint = "/test"
client := &Client{ client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
assert.Equal(t, req.Context().Err(), context.DeadlineExceeded) assert.Check(t, is.ErrorType(req.Context().Err(), context.DeadlineExceeded))
return nil, context.DeadlineExceeded
return &http.Response{}, context.DeadlineExceeded
}), }),
} }
@ -144,7 +144,7 @@ func TestDeadlineExceededContext(t *testing.T) {
<-ctx.Done() <-ctx.Done()
_, err := client.sendRequest(ctx, http.MethodGet, testURL, nil, nil, nil) _, err := client.sendRequest(ctx, http.MethodGet, testEndpoint, nil, nil, nil)
assert.Equal(t, true, errdefs.IsDeadline(err)) assert.Check(t, is.ErrorType(err, errdefs.IsDeadline))
assert.Equal(t, true, errors.Is(err, context.DeadlineExceeded)) assert.Check(t, errors.Is(err, context.DeadlineExceeded))
} }