Skip to content
4 changes: 2 additions & 2 deletions pkg/utils/requester.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2019,2022 Optimizely, Inc. and contributors *
* Copyright 2019,2022-2023 Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand Down Expand Up @@ -156,7 +156,7 @@ func (r HTTPRequester) Do(url, method string, body io.Reader, headers []Header)
single := func(request *http.Request) (response []byte, responseHeaders http.Header, code int, e error) {
resp, doErr := r.client.Do(request)
if doErr != nil {
r.logger.Error(fmt.Sprintf("failed to send request %v", request), e)
r.logger.Error(fmt.Sprintf("failed to send request %v", request), doErr)
return nil, http.Header{}, 0, doErr
}
defer func() {
Expand Down
38 changes: 34 additions & 4 deletions pkg/utils/requester_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2019,2021-2022 Optimizely, Inc. and contributors *
* Copyright 2019,2021-2023 Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand Down Expand Up @@ -190,12 +190,42 @@ func TestPostObj(t *testing.T) {
assert.NotNil(t, err)
}

// mockLogger exists to check that the logged error is what we expected
type mockLogger struct {
Errors []error
}

func (t *mockLogger) Debug(message string) {}
func (t *mockLogger) Info(message string) {}
func (t *mockLogger) Warning(message string) {}
func (t *mockLogger) Error(message string, err interface{}) {
if err, ok := err.(error); ok {
t.Errors = append(t.Errors, err)
}
}

func TestGetBad(t *testing.T) {
// Using a mockLogger to ensure we're logging the expected error message
mlog := &mockLogger{}
httpreq := NewHTTPRequester(mlog)

httpreq := NewHTTPRequester(logging.GetLogger("", ""))
_, _, _, err := httpreq.Get("blah12345/good")
_, ok := err.(*url.Error)
badURL := "blah12345/good"
_, _, _, err := httpreq.Get(badURL)
returnedErr, ok := err.(*url.Error)
assert.True(t, ok, "url error")

// Check to make sure we have some log for bad url
assert.NotNil(t, mlog.Errors)
// If we didn't get the expected error, we need to stop before we do the rest
// of the checks that depend on that error.
if !assert.Len(t, mlog.Errors, 1, "logged error") {
t.FailNow()
}
// Check to make sure the error that was logged is the same as what was returned
loggedErr, ok := mlog.Errors[0].(*url.Error)
assert.True(t, ok, "is URL error")
assert.Equal(t, returnedErr, loggedErr, "expected same error")
assert.Equal(t, badURL, loggedErr.URL, "expected the URL we requested")
}

func TestGetBadWithResponse(t *testing.T) {
Expand Down