|
1 | 1 | package linodego |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "context" |
4 | 6 | "fmt" |
| 7 | + "reflect" |
| 8 | + "strings" |
5 | 9 | "testing" |
6 | 10 |
|
7 | 11 | "github.com/google/go-cmp/cmp" |
| 12 | + "github.com/jarcoal/httpmock" |
| 13 | + "github.com/linode/linodego/internal/testutil" |
8 | 14 | ) |
9 | 15 |
|
10 | 16 | func TestClient_SetAPIVersion(t *testing.T) { |
@@ -139,3 +145,56 @@ api_version = v4beta |
139 | 145 | [cool] |
140 | 146 | token = blah |
141 | 147 | ` |
| 148 | + |
| 149 | +func TestDebugLogSanitization(t *testing.T) { |
| 150 | + type instanceResponse struct { |
| 151 | + ID int `json:"id"` |
| 152 | + Region string `json:"region"` |
| 153 | + Label string `json:"label"` |
| 154 | + } |
| 155 | + |
| 156 | + var testResp = instanceResponse{ |
| 157 | + ID: 100, |
| 158 | + Region: "test-central", |
| 159 | + Label: "this-is-a-test-linode", |
| 160 | + } |
| 161 | + var lgr bytes.Buffer |
| 162 | + |
| 163 | + plainTextToken := "NOTANAPIKEY" |
| 164 | + |
| 165 | + mockClient := testutil.CreateMockClient(t, NewClient) |
| 166 | + logger := testutil.CreateLogger() |
| 167 | + mockClient.SetLogger(logger) |
| 168 | + logger.L.SetOutput(&lgr) |
| 169 | + |
| 170 | + mockClient.SetDebug(true) |
| 171 | + if !mockClient.resty.Debug { |
| 172 | + t.Fatal("debug should be enabled") |
| 173 | + } |
| 174 | + mockClient.SetHeader("Authorization", fmt.Sprintf("Bearer %s", plainTextToken)) |
| 175 | + |
| 176 | + if mockClient.resty.Header.Get("Authorization") != fmt.Sprintf("Bearer %s", plainTextToken) { |
| 177 | + t.Fatal("token not found in auth header") |
| 178 | + } |
| 179 | + |
| 180 | + httpmock.RegisterRegexpResponder("GET", testutil.MockRequestURL("/linode/instances"), |
| 181 | + httpmock.NewJsonResponderOrPanic(200, &testResp)) |
| 182 | + |
| 183 | + result, err := doGETRequest[instanceResponse]( |
| 184 | + context.Background(), |
| 185 | + mockClient, |
| 186 | + "/linode/instances", |
| 187 | + ) |
| 188 | + if err != nil { |
| 189 | + t.Fatal(err) |
| 190 | + } |
| 191 | + |
| 192 | + logInfo := lgr.String() |
| 193 | + if !strings.Contains(logInfo, "Bearer *******************************") { |
| 194 | + t.Fatal("sanitized bearer token was expected") |
| 195 | + } |
| 196 | + |
| 197 | + if !reflect.DeepEqual(*result, testResp) { |
| 198 | + t.Fatalf("actual response does not equal desired response: %s", cmp.Diff(result, testResponse)) |
| 199 | + } |
| 200 | +} |
0 commit comments