@@ -181,7 +181,7 @@ func TestFetchUserInfoShouldReturnCorrectUserInfoResponse(t *testing.T) {
181181 })
182182 defer patchesForGetAccessToken .Reset ()
183183
184- patchesCoreFetchUserInfo := gomonkey .ApplyFunc (core .FetchUserInfo , func (userInfoEndpoint , accessToken string ) (core.UserInfoResponse , error ) {
184+ patchesCoreFetchUserInfo := gomonkey .ApplyFunc (core .FetchUserInfoWithClient , func (client * http. Client , userInfoEndpoint , accessToken string ) (core.UserInfoResponse , error ) {
185185 return testUserInfoResponse , nil
186186 })
187187 defer patchesCoreFetchUserInfo .Reset ()
@@ -254,3 +254,85 @@ func TestFetchUserInfoShouldReturnErrorIfGetAccessTokenFailed(t *testing.T) {
254254
255255 assert .Equal (t , testGetAccessTokenErr , fetchUserInfoErr )
256256}
257+
258+ func TestNewLogtoClientShouldUseDefaultHttpClientWhenNoOptionsProvided (t * testing.T ) {
259+ logtoClient := NewLogtoClient (& LogtoConfig {}, & TestStorage {})
260+
261+ assert .NotNil (t , logtoClient .httpClient )
262+ // The default HTTP client should be a basic http.Client
263+ assert .IsType (t , & http.Client {}, logtoClient .httpClient )
264+ }
265+
266+ func TestNewLogtoClientShouldUseCustomHttpClientWhenWithHttpClientOptionProvided (t * testing.T ) {
267+ customClient := & http.Client {
268+ Timeout : 10 * time .Second ,
269+ }
270+
271+ logtoClient := NewLogtoClient (& LogtoConfig {}, & TestStorage {}, WithHttpClient (customClient ))
272+
273+ assert .NotNil (t , logtoClient .httpClient )
274+ assert .Equal (t , customClient , logtoClient .httpClient )
275+ assert .Equal (t , 10 * time .Second , logtoClient .httpClient .Timeout )
276+ }
277+
278+ func TestNewLogtoClientShouldApplyMultipleOptions (t * testing.T ) {
279+ customClient := & http.Client {
280+ Timeout : 5 * time .Second ,
281+ }
282+
283+ // Create a mock second option for demonstration
284+ var appliedSecondOption bool
285+ mockSecondOption := func (client * LogtoClient ) {
286+ appliedSecondOption = true
287+ }
288+
289+ logtoClient := NewLogtoClient (& LogtoConfig {}, & TestStorage {},
290+ WithHttpClient (customClient ),
291+ mockSecondOption ,
292+ )
293+
294+ // Verify first option (WithHttpClient) was applied
295+ assert .NotNil (t , logtoClient .httpClient )
296+ assert .Equal (t , customClient , logtoClient .httpClient )
297+ assert .Equal (t , 5 * time .Second , logtoClient .httpClient .Timeout )
298+
299+ // Verify second option was applied
300+ assert .True (t , appliedSecondOption , "Second option should have been applied" )
301+ }
302+
303+ func TestWithHttpClientShouldReturnValidOption (t * testing.T ) {
304+ customClient := & http.Client {
305+ Timeout : 30 * time .Second ,
306+ }
307+
308+ option := WithHttpClient (customClient )
309+
310+ assert .NotNil (t , option )
311+
312+ // Test that the option function works correctly
313+ logtoClient := & LogtoClient {
314+ httpClient : & http.Client {}, // Default client
315+ }
316+
317+ // Apply the option
318+ option (logtoClient )
319+
320+ assert .Equal (t , customClient , logtoClient .httpClient )
321+ assert .Equal (t , 30 * time .Second , logtoClient .httpClient .Timeout )
322+ }
323+
324+ func TestLogtoClientShouldUseCustomHttpClientForFetchUserInfo (t * testing.T ) {
325+ customClient := & http.Client {
326+ Timeout : 15 * time .Second ,
327+ }
328+
329+ logtoClient := NewLogtoClient (& LogtoConfig {}, & TestStorage {}, WithHttpClient (customClient ))
330+
331+ // Verify that the client is using the custom HTTP client
332+ assert .Equal (t , customClient , logtoClient .httpClient )
333+ assert .Equal (t , 15 * time .Second , logtoClient .httpClient .Timeout )
334+
335+ // This test verifies that the LogtoClient is configured with the custom HTTP client
336+ // The actual FetchUserInfo will use this client when making HTTP requests
337+ assert .NotNil (t , logtoClient .httpClient )
338+ }
0 commit comments