-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
client ID #3
base: master
Are you sure you want to change the base?
client ID #3
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json | ||
language: "en-US" | ||
early_access: false | ||
reviews: | ||
profile: chill | ||
request_changes_workflow: true | ||
high_level_summary: true | ||
poem: true | ||
review_status: true | ||
collapse_walkthrough: false | ||
auto_review: | ||
enabled: true | ||
drafts: true | ||
chat: | ||
auto_reply: true |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,17 @@ import ( | |
"net/url" | ||
) | ||
|
||
// ClientID sets the client ID for requests made using the client. The client ID is | ||
// typically used to differentate between clients in log entries. If no client ID is | ||
// provided a default value will be applied consisting of the string "http-<seq>" | ||
// where <seq> is a number that increments for each client created. | ||
func ClientID(id string) ClientOption { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bito Code Review Agent Run #3867f3 - 07/06/2024, 12:12 am 🔴 High importance
Issue: The 'ClientID' function does not handle the case where the provided 'id' is an empty string. This could lead to clients being created with an empty ID, which might cause issues in logging or client differentiation.
Fix: Add a check to ensure that the 'id' is not an empty string. If it is, assign a default value.
Code suggestion
|
||
return func(c *client) error { | ||
c.id = id | ||
return nil | ||
} | ||
} | ||
|
||
// MaxRetries sets the maximum number of retries for requests made using the client. | ||
// Individual requests may be configured to override this value on a case-by-case basis. | ||
func MaxRetries(n uint) ClientOption { | ||
|
@@ -16,11 +27,11 @@ func MaxRetries(n uint) ClientOption { | |
} | ||
|
||
// URL sets the base URL for requests made using the client. The URL may be specified | ||
// as a string or a *url.URL. | ||
// | ||
// If a string is provided, it will be parsed to ensure it is a valid, absolute URL. | ||
// as any of: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bito Code Review Agent Run #3867f3 - 07/06/2024, 12:12 am 🔴 High importance
Issue: The removal of detailed comments explaining the expected input types and parsing behavior for a URL reduces the clarity and documentation quality of the code. This can lead to confusion for future developers who may not understand the expected input types and how they are handled.
Fix: Restore the detailed comments explaining the expected input types and parsing behavior for a URL. This will ensure that future developers have a clear understanding of how the input should be provided and how it will be processed.
Code suggestion
|
||
// | ||
// If a URL is provided is must be absolute. | ||
// string // a which parses to a valid, absolute URL | ||
// url.URL // a valid, absolute URL | ||
// *url.URL // a valid, absolute URL | ||
Comment on lines
+32
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bito Code Review Agent Run #3867f3 - 07/06/2024, 12:12 am 🔴 High importance
Issue: The new comments added are not clear and do not provide sufficient context or explanation. They seem to be incomplete and may confuse future developers.
Fix: Provide a more detailed and clear explanation of the different URL types that can be used, including examples if possible.
Code suggestion
|
||
func URL(u any) ClientOption { | ||
return func(c *client) error { | ||
switch u := u.(type) { | ||
|
@@ -31,6 +42,12 @@ func URL(u any) ClientOption { | |
} | ||
return URL(url)(c) | ||
|
||
case url.URL: | ||
if !u.IsAbs() { | ||
return fmt.Errorf("http: URL option: %w: URL must be absolute", ErrInvalidURL) | ||
} | ||
c.url = u.String() | ||
|
||
case *url.URL: | ||
if !u.IsAbs() { | ||
return fmt.Errorf("http: URL option: %w: URL must be absolute", ErrInvalidURL) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,19 @@ func TestClientOptions(t *testing.T) { | |
scenario string | ||
exec func(t *testing.T) | ||
}{ | ||
{scenario: "ClientID", | ||
exec: func(t *testing.T) { | ||
// ARRANGE | ||
sut := &client{} | ||
|
||
// ACT | ||
err := ClientID("foo")(sut) | ||
|
||
// ASSERT | ||
test.That(t, err).IsNil() | ||
test.That(t, sut).Equals(&client{id: "foo"}) | ||
}, | ||
}, | ||
Comment on lines
+28
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bito Code Review Agent Run #3867f3 - 07/06/2024, 12:12 am 🔴 High importance
Issue: The test case for 'ClientID' does not cover edge cases such as passing an empty string or a very long string as the client ID. This could lead to potential issues if the 'ClientID' function does not handle these cases properly.
Fix: Add additional test cases to cover edge cases for the 'ClientID' function, such as passing an empty string and a very long string.
Code suggestion
|
||
{scenario: "URL/int", | ||
exec: func(t *testing.T) { | ||
// ARRANGE | ||
|
@@ -96,6 +109,33 @@ func TestClientOptions(t *testing.T) { | |
// ACT | ||
err := URL(url)(client) | ||
|
||
// ASSERT | ||
test.Error(t, err).IsNil() | ||
test.That(t, client.url).Equals("http://example.com") | ||
}, | ||
}, | ||
{scenario: "URL/*URL/relative", | ||
exec: func(t *testing.T) { | ||
// ARRANGE | ||
client := &client{} | ||
url, _ := url.Parse("example.com") | ||
|
||
// ACT | ||
err := URL(*url)(client) | ||
|
||
// ASSERT | ||
test.Error(t, err).Is(ErrInvalidURL) | ||
}, | ||
}, | ||
Comment on lines
+117
to
+129
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bito Code Review Agent Run #3867f3 - 07/06/2024, 12:12 am 🔴 High importance
Issue: The test case for 'URL/*URL/relative' does not handle the potential panic that could occur if 'url.Parse' returns an error. This could lead to a test failure due to an unhandled error.
Fix: Add error handling for the 'url.Parse' function to ensure that the test does not panic if an error occurs.
Code suggestion
|
||
{scenario: "URL/*URL/successful", | ||
exec: func(t *testing.T) { | ||
// ARRANGE | ||
client := &client{} | ||
url, _ := url.Parse("http://example.com") | ||
|
||
// ACT | ||
err := URL(*url)(client) | ||
|
||
// ASSERT | ||
test.Error(t, err).IsNil() | ||
test.That(t, client.url).Equals("http://example.com") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,12 +24,12 @@ func TestNewClient(t *testing.T) { | |
{scenario: "no errors", | ||
exec: func(t *testing.T) { | ||
// ACT | ||
result, err := NewClient("name", func(c *client) error { return nil }) | ||
result, err := NewClient(func(c *client) error { return nil }) | ||
|
||
// ASSERT | ||
test.That(t, err).IsNil() | ||
test.That(t, result).Equals(client{ | ||
name: "name", | ||
id: "http-1", | ||
wrapped: http.DefaultClient, | ||
}) | ||
}, | ||
|
@@ -40,7 +40,7 @@ func TestNewClient(t *testing.T) { | |
opts := []ClientOption{func(c *client) error { return opterr }} | ||
|
||
// ACT | ||
result, err := NewClient("name", opts...) | ||
result, err := NewClient(opts...) | ||
|
||
// ASSERT | ||
test.Error(t, err).Is(ErrInitialisingClient) | ||
|
@@ -140,6 +140,10 @@ func TestNewRequest(t *testing.T) { | |
} | ||
for _, tc := range testcases { | ||
t.Run(tc.scenario, func(t *testing.T) { | ||
// ARRANGE | ||
seq = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bito Code Review Agent Run #3867f3 - 07/06/2024, 12:12 am 🔴 High importance
Issue: The variable 'seq' is being set to 0 without any context or explanation. This could lead to confusion or errors if 'seq' is used elsewhere in the test cases or if its value is expected to be different.
Fix: Provide a clear explanation or comment on why 'seq' is being reset to 0. Ensure that this reset does not interfere with other test cases or the overall test logic.
Code suggestion
|
||
|
||
// ACT | ||
tc.exec(t) | ||
}) | ||
} | ||
|
@@ -705,7 +709,7 @@ func TestConvenienceMethods(t *testing.T) { | |
ioReadAll = func(r io.Reader) ([]byte, error) { return nil, readerr } | ||
|
||
// ACT | ||
result, err := UnmarshalJSON[map[string]string](ctx, response) | ||
result, err := UnmarshalJSON[map[string]string](response) | ||
|
||
// ASSERT | ||
test.Error(t, err).Is(readerr) | ||
|
@@ -718,7 +722,7 @@ func TestConvenienceMethods(t *testing.T) { | |
response := &http.Response{Body: io.NopCloser(bytes.NewReader([]byte("not valid JSON")))} | ||
|
||
// ACT | ||
result, err := UnmarshalJSON[map[string]string](ctx, response) | ||
result, err := UnmarshalJSON[map[string]string](response) | ||
|
||
// ASSERT | ||
test.Error(t, err).Is(ErrInvalidJSON) | ||
|
@@ -731,24 +735,27 @@ func TestConvenienceMethods(t *testing.T) { | |
response := &http.Response{Body: io.NopCloser(bytes.NewReader([]byte(`{"key":"value"}`)))} | ||
|
||
// ACT | ||
result, err := UnmarshalJSON[int](ctx, response) | ||
result, err := UnmarshalJSON[int](response) | ||
|
||
// ASSERT | ||
test.Error(t, err).Is(ErrInvalidJSON) | ||
test.That(t, result).Equals(0) | ||
test.That(t, result).IsNil() | ||
}, | ||
}, | ||
{scenario: "UnmarshalJSON/ok", | ||
exec: func(t *testing.T) { | ||
// ARRANGE | ||
type body struct { | ||
Key string `json:"key"` | ||
} | ||
response := &http.Response{Body: io.NopCloser(bytes.NewReader([]byte(`{"key":"value"}`)))} | ||
|
||
// ACT | ||
result, err := UnmarshalJSON[map[string]string](ctx, response) | ||
result, err := UnmarshalJSON[body](response) | ||
|
||
// ASSERT | ||
test.Error(t, err).Is(nil) | ||
test.That(t, result).Equals(map[string]string{"key": "value"}) | ||
test.That(t, result).Equals(&body{Key: "value"}) | ||
}, | ||
}, | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ type MockClient interface { | |
// methods for configuring request and response expectations and | ||
// verifying that those expectations have been met. | ||
type mockClient struct { | ||
name string | ||
id string | ||
hostname string | ||
expectations []*MockRequest | ||
unexpected []*http.Request | ||
|
@@ -46,7 +46,7 @@ type mockClient struct { | |
// | ||
// # params | ||
// | ||
// name // used to identify the mock client in test failure reports and errors | ||
// id // used to identify the mock client in test failure reports and errors | ||
// wrap // optional function(s) to wrap the client with some other client | ||
// // implementation, if required; nil functions are ignored | ||
// | ||
|
@@ -65,7 +65,7 @@ func NewMockClient(name string, wrap ...func(c interface { | |
Do(*http.Request) (*http.Response, error) | ||
}) (HttpClient, MockClient) { | ||
def := &mockClient{ | ||
name: name, | ||
id: name, | ||
hostname: "mock://hostname", | ||
next: noExpectedRequests, | ||
} | ||
|
@@ -80,7 +80,8 @@ func NewMockClient(name string, wrap ...func(c interface { | |
mock = wrap(mock) | ||
} | ||
|
||
c, _ := NewClient(def.name, | ||
c, _ := NewClient( | ||
ClientID(def.id), | ||
URL(def.hostname), | ||
Using(mock), | ||
) | ||
|
@@ -189,7 +190,7 @@ func (mock mockClient) ExpectationsWereMet() error { | |
} | ||
|
||
if len(errs) > 0 { | ||
return MockExpectationsError{mock.name, errs} | ||
return MockExpectationsError{mock.id, errs} | ||
} | ||
|
||
return nil | ||
|
@@ -207,7 +208,7 @@ func (mock mockClient) ExpectationsWereMet() error { | |
func (mock *mockClient) Expect(method string, path string) *MockRequest { | ||
if mock.next > 0 { | ||
msg := "requests have already been made" | ||
panic(fmt.Errorf("%s: %w: %s", mock.name, ErrCannotChangeExpectations, msg)) | ||
panic(fmt.Errorf("%s: %w: %s", mock.id, ErrCannotChangeExpectations, msg)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bito Code Review Agent Run #3867f3 - 07/06/2024, 12:12 am 🔴 High importance
Issue: The panic message uses 'mock.id' which is a change from 'mock.name'. If 'mock.id' is not properly initialized or if it is an empty string, the error message might not be informative enough to debug the issue.
Fix: Ensure that 'mock.id' is properly initialized and contains a meaningful value before it is used in the panic message. If 'mock.id' can be empty or uninitialized, consider adding a check or a default value to make the error message more informative.
Code suggestion
|
||
} | ||
|
||
fqu, err := url.JoinPath(mock.hostname, path) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,11 +36,11 @@ func TestNewMockClient(t *testing.T) { | |
|
||
// ASSERT | ||
if c, ok := test.IsType[client](t, c); ok { | ||
test.That(t, c.name).Equals("foo") | ||
test.That(t, c.id).Equals("foo") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bito Code Review Agent Run #3867f3 - 07/06/2024, 12:12 am 🔴 High importance
Issue: The test assertion 'test.That(t, c.id).Equals("foo")' does not verify if the 'id' field is correctly set in the 'client' struct. If the 'id' field is not set correctly, the test will fail, but it won't provide any information about the actual value of 'id'.
Fix: Add a message to the assertion to provide more context if the test fails. This will help in debugging by showing the actual value of 'id'.
Code suggestion
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The blugnu/test package used for this test uses a fluent API to assert expected vs actual values. If the test fails, the value of |
||
test.That(t, c.url).Equals("mock://hostname") | ||
} | ||
if m, ok := test.IsType[*mockClient](t, m); ok { | ||
test.That(t, m.name).Equals("foo") | ||
test.That(t, m.id).Equals("foo") | ||
test.That(t, m.hostname).Equals("mock://hostname") | ||
} | ||
test.IsTrue(t, wrappersAreApplied) | ||
|
@@ -198,7 +198,7 @@ func TestMockClient(t *testing.T) { | |
exec: func(t *testing.T) { | ||
// ARRANGE | ||
client := &mockClient{ | ||
name: "foo", | ||
id: "foo", | ||
next: noExpectedRequests, | ||
unexpected: []*http.Request{{Method: http.MethodGet, URL: &url.URL{Scheme: "http", Host: "hostname", Path: "path"}}}, | ||
} | ||
|
@@ -219,7 +219,7 @@ func TestMockClient(t *testing.T) { | |
exec: func(t *testing.T) { | ||
// ARRANGE | ||
client := &mockClient{ | ||
name: "foo", | ||
id: "foo", | ||
next: 0, | ||
expectations: []*MockRequest{{}}, | ||
unexpected: []*http.Request{{Method: http.MethodGet, URL: &url.URL{Scheme: "http", Host: "hostname", Path: "path"}}}, | ||
|
@@ -265,7 +265,7 @@ func TestMockClient(t *testing.T) { | |
// ARRANGE | ||
m := http.MethodPost | ||
client := &mockClient{ | ||
name: "foo", | ||
id: "foo", | ||
expectations: []*MockRequest{ | ||
{ | ||
isExpected: true, | ||
|
@@ -297,7 +297,7 @@ func TestMockClient(t *testing.T) { | |
exec: func(t *testing.T) { | ||
// ARRANGE | ||
client := &mockClient{ | ||
name: "foo", | ||
id: "foo", | ||
expectations: []*MockRequest{ | ||
{ | ||
isExpected: true, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bito Code Review Agent Run #3867f3 - 07/06/2024, 12:12 am
Code suggestion