-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RequestSendError And CanceledError to HTTP ClientHandler
Adds wrapping the HTTP client handler's response with an RequestSendError for all HTTP request send errors, and CanceledError if the context passed in to the handler was canceled and the HTTP client encountered an error.
- Loading branch information
Showing
3 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package http | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
smithy "github.com/awslabs/smithy-go" | ||
) | ||
|
||
func TestClientHandler_Handle(t *testing.T) { | ||
cases := map[string]struct { | ||
Context context.Context | ||
Client ClientDo | ||
ExpectErr func(error) error | ||
}{ | ||
"no error": { | ||
Context: context.Background(), | ||
Client: ClientDoFunc(func(*http.Request) (*http.Response, error) { | ||
return &http.Response{}, nil | ||
}), | ||
}, | ||
"send error": { | ||
Context: context.Background(), | ||
Client: ClientDoFunc(func(*http.Request) (*http.Response, error) { | ||
return nil, fmt.Errorf("some error") | ||
}), | ||
ExpectErr: func(err error) error { | ||
var sendError *RequestSendError | ||
if !errors.As(err, &sendError) { | ||
return fmt.Errorf("expect error to be %T, %v", sendError, err) | ||
} | ||
|
||
var cancelError *smithy.CanceledError | ||
if errors.As(err, &cancelError) { | ||
return fmt.Errorf("expect error to not be %T, %v", cancelError, err) | ||
} | ||
|
||
return nil | ||
}, | ||
}, | ||
"canceled error": { | ||
Context: func() context.Context { | ||
ctx, fn := context.WithCancel(context.Background()) | ||
fn() | ||
return ctx | ||
}(), | ||
Client: ClientDoFunc(func(*http.Request) (*http.Response, error) { | ||
return nil, fmt.Errorf("some error") | ||
}), | ||
ExpectErr: func(err error) error { | ||
var sendError *RequestSendError | ||
if errors.As(err, &sendError) { | ||
return fmt.Errorf("expect error to not be %T, %v", sendError, err) | ||
} | ||
|
||
var cancelError *smithy.CanceledError | ||
if !errors.As(err, &cancelError) { | ||
return fmt.Errorf("expect error to be %T, %v", cancelError, err) | ||
} | ||
|
||
return nil | ||
}, | ||
}, | ||
} | ||
|
||
for name, c := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
handler := NewClientHandler(c.Client) | ||
resp, _, err := handler.Handle(c.Context, NewStackRequest()) | ||
|
||
if c.ExpectErr != nil { | ||
if err == nil { | ||
t.Fatalf("expect error, got none") | ||
} | ||
if err = c.ExpectErr(err); err != nil { | ||
t.Fatalf("expect error match failed, %v", err) | ||
} | ||
return | ||
} | ||
if err != nil { | ||
t.Fatalf("expect no error, got %v", err) | ||
} | ||
|
||
if _, ok := resp.(*Response); !ok { | ||
t.Fatalf("expect Response type, got %T", resp) | ||
} | ||
}) | ||
} | ||
|
||
} |