|
1 | 1 | /****************************************************************************
|
2 |
| - * Copyright 2019,2021-2022 Optimizely, Inc. and contributors * |
| 2 | + * Copyright 2019,2021-2023 Optimizely, Inc. and contributors * |
3 | 3 | * *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); *
|
5 | 5 | * you may not use this file except in compliance with the License. *
|
@@ -190,12 +190,41 @@ func TestPostObj(t *testing.T) {
|
190 | 190 | assert.NotNil(t, err)
|
191 | 191 | }
|
192 | 192 |
|
| 193 | +type mockLogger struct { |
| 194 | + Errors []error |
| 195 | +} |
| 196 | + |
| 197 | +func (m *mockLogger) Debug(message string) {} |
| 198 | +func (m *mockLogger) Info(message string) {} |
| 199 | +func (m *mockLogger) Warning(message string) {} |
| 200 | +func (m *mockLogger) Error(message string, err interface{}) { |
| 201 | + if err, ok := err.(error); ok { |
| 202 | + m.Errors = append(m.Errors, err) |
| 203 | + } |
| 204 | +} |
| 205 | + |
193 | 206 | func TestGetBad(t *testing.T) {
|
| 207 | + // Using a mockLogger to ensure we're logging the expected error message |
| 208 | + mLogger := &mockLogger{} |
| 209 | + httpreq := NewHTTPRequester(mLogger) |
194 | 210 |
|
195 |
| - httpreq := NewHTTPRequester(logging.GetLogger("", "")) |
196 |
| - _, _, _, err := httpreq.Get("blah12345/good") |
197 |
| - _, ok := err.(*url.Error) |
| 211 | + badURL := "http://ww.bad-url.fake/blah12345" |
| 212 | + _, _, _, err := httpreq.Get(badURL) |
| 213 | + returnedErr, ok := err.(*url.Error) |
198 | 214 | assert.True(t, ok, "url error")
|
| 215 | + |
| 216 | + // Check to make sure we have some log for bad url |
| 217 | + assert.NotNil(t, mLogger.Errors) |
| 218 | + // If we didn't get the expected error, we need to stop before we do the rest |
| 219 | + // of the checks that depend on that error. |
| 220 | + if !assert.Len(t, mLogger.Errors, 1, "logged error") { |
| 221 | + t.FailNow() |
| 222 | + } |
| 223 | + // Check to make sure the error that was logged is the same as what was returned |
| 224 | + loggedErr, ok := mLogger.Errors[0].(*url.Error) |
| 225 | + assert.True(t, ok, "is URL error") |
| 226 | + assert.Equal(t, returnedErr, loggedErr, "expected same error") |
| 227 | + assert.Equal(t, badURL, loggedErr.URL, "expected the URL we requested") |
199 | 228 | }
|
200 | 229 |
|
201 | 230 | func TestGetBadWithResponse(t *testing.T) {
|
|
0 commit comments