Skip to content

Commit c68f70a

Browse files
Change update logging for http assert (#290)
## What - update logging for http assert ## References <!-- Add identifier for issue tickets, links to other PRs, etc. --> ## Checklist <!-- Remove this section if not applicable to your changes --> - [ ] Tests
1 parent 9ae6037 commit c68f70a

File tree

18 files changed

+92
-55
lines changed

18 files changed

+92
-55
lines changed

pkg/auth/auth_client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@ const tokenRefreshMargin = 10 * time.Second
3131
type KeycloakConfig struct {
3232
ClientID string
3333
Username string
34-
Password string
34+
Password string //nolint:gosec
3535
AuthURL string
3636
KeycloakRealm string
3737
}
3838

3939
type tokenInfo struct {
40-
AccessToken string
40+
AccessToken string //nolint:gosec
4141
ExpiresAt time.Time
4242
}
4343

4444
type authResponse struct {
45-
AccessToken string `json:"access_token"`
46-
ExpiresIn int `json:"expires_in"` // in seconds
45+
AccessToken string `json:"access_token"` //nolint:gosec
46+
ExpiresIn int `json:"expires_in"` // in seconds
4747
}
4848

4949
// KeycloakClient can be used to authenticate against a Keycloak server.
@@ -121,7 +121,7 @@ func (c *KeycloakClient) requestToken(ctx context.Context) (*authResponse, error
121121
}
122122
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
123123

124-
resp, err := c.httpClient.Do(req)
124+
resp, err := c.httpClient.Do(req) //nolint:gosec
125125
if err != nil {
126126
return nil, fmt.Errorf("failed to execute authentication request: %w", err)
127127
}

pkg/auth/auth_client_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ func TestKeycloakClient_GetToken(t *testing.T) {
3939
for name, tt := range tests {
4040
t.Run(name, func(t *testing.T) {
4141
var serverCallCount atomic.Int32
42-
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
42+
mockServer := httptest.NewServer(http.HandlerFunc(func(
43+
w http.ResponseWriter, r *http.Request,
44+
) {
4345
serverCallCount.Add(1)
4446
w.WriteHeader(tt.responseCode)
4547
_, err := w.Write([]byte(tt.responseBody))
@@ -108,7 +110,9 @@ func TestKeycloakClient_GetToken_Refresh(t *testing.T) {
108110
fakeClock := NewFakeClock(time.Now())
109111

110112
var serverCallCount atomic.Int32
111-
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
113+
mockServer := httptest.NewServer(http.HandlerFunc(func(
114+
w http.ResponseWriter, r *http.Request,
115+
) {
112116
serverCallCount.Add(1)
113117
w.WriteHeader(200)
114118
_, err := w.Write(kcMockResponse)

pkg/dbcrypt/dbcipher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Config struct {
2525
Version string
2626

2727
// Contains the password used to derive encryption key
28-
Password string
28+
Password string //nolint:gosec
2929

3030
// Contains the salt for increasing password entropy
3131
PasswordSalt string

pkg/httpassert/response.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package httpassert
66

77
import (
88
"encoding/json"
9-
"fmt"
109
"os"
1110
"strings"
1211

@@ -63,7 +62,11 @@ func (r *responseImpl) Header(name string, value any) Response {
6362
}
6463

6564
func (r *responseImpl) StatusCode(expected int) Response {
66-
require.Equal(r.t, expected, r.response.Code)
65+
if assert.Equal(r.t, expected, r.response.Code) {
66+
return r
67+
}
68+
r.Log()
69+
r.t.FailNow()
6770
return r
6871
}
6972

@@ -210,9 +213,15 @@ func (r *responseImpl) GetJsonBodyObject(target any) Response {
210213
}
211214

212215
func (r *responseImpl) Log() Response {
213-
fmt.Printf("Response: %d\nHeaders: %v\nBody: %s\n",
216+
r.t.Logf("Request\nMethod: %s\nURL: %s\nContent-Type: %s\nHeaders: %v\nBody: %s\n",
217+
r.request.method,
218+
r.request.url,
219+
r.request.contentType,
220+
r.request.headers,
221+
r.request.body)
222+
r.t.Logf("Response\nCode: %d\nHeaders: %v\nBody: %s\n",
214223
r.response.Code,
215224
r.response.Header(),
216-
r.response.Body.String())
225+
r.response.Body)
217226
return r
218227
}

pkg/notifications/notification.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (c *Client) RegisterOrigins(ctx context.Context, serviceID string, origins
121121
req.Header.Set("Authorization", "Bearer "+token)
122122
req.Header.Set("Content-Type", "application/json")
123123

124-
response, err := c.httpClient.Do(req)
124+
response, err := c.httpClient.Do(req) //nolint:gosec
125125
if err != nil {
126126
return fmt.Errorf("failed to execute request: %w", err)
127127
}

pkg/notifications/notification_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ func TestClient_CreateNotification(t *testing.T) {
113113
mockAuthServer := setupMockAuthServer(t, tt.serverErrors.authenticationFail)
114114
defer mockAuthServer.Close()
115115

116-
mockNotificationServer := setupMockNotificationServer(t, &serverCallCount, tt.wantNotificationSent, tt.serverErrors)
116+
mockNotificationServer := setupMockNotificationServer(t, &serverCallCount,
117+
tt.wantNotificationSent, tt.serverErrors)
117118
defer mockNotificationServer.Close()
118119

119120
config := Config{
@@ -158,7 +159,9 @@ func setupMockAuthServer(t *testing.T, failAuth bool) *httptest.Server {
158159
}))
159160
}
160161

161-
func setupMockNotificationServer(t *testing.T, serverCallCount *atomic.Int32, wantNotification notificationModel, errors serverErrors) *httptest.Server {
162+
func setupMockNotificationServer(t *testing.T, serverCallCount *atomic.Int32,
163+
wantNotification notificationModel, errors serverErrors,
164+
) *httptest.Server {
162165
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
163166
serverCallCount.Add(1)
164167

pkg/openSearch/openSearchQuery/sort_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ func TestSorting(t *testing.T) {
7070
}
7171
termsAggregation := esquery.TermsAgg("vulnerabilityWithAssetCountAgg", "vulnerabilityTest.oid.keyword").
7272
Aggs(subAggs...)
73-
termsAggregation, err = AddOrder(termsAggregation, testCases[name].SortingRequest, sortFieldMapping)
73+
termsAggregation, err = AddOrder(termsAggregation,
74+
testCases[name].SortingRequest, sortFieldMapping)
7475
resultingJson, queryErr := esquery.Search().Query(q.Build()).Aggs(termsAggregation).Size(0).MarshalJSON()
7576
assert.Nil(t, queryErr)
7677
assert.JSONEq(t, testCases[name].ExpectedQueryJson, string(resultingJson))

pkg/openSearch/osquery/boolQueryBuilder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ func (q *BoolQueryBuilder) AddFilterRequest(request *filter.Request) error {
151151
if handler, ok := operatorMapping[field.Operator]; ok {
152152
value := field.Value
153153

154-
if field.Operator == filter.CompareOperatorExists || field.Operator == filter.CompareOperatorDoesNotExist {
154+
if field.Operator == filter.CompareOperatorExists ||
155+
field.Operator == filter.CompareOperatorDoesNotExist {
155156
value = "" // exists operator does not need a value, but for more consistent handling just pass a dummy value
156157
}
157158
if value == nil {

pkg/openSearch/osquery/boolQueryBuilder_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,8 @@ func TestBoolQueryBuilder_AddFilterRequest(t *testing.T) {
703703
t.Logf("sending search request to OpenSearch: %s", string(requestBody))
704704
gotDocuments, totalResults, err := client.ListDocuments(alias, requestBody)
705705
require.NoError(t, err, "listing documents failed")
706-
require.Len(t, gotDocuments, int(totalResults), "test requires results to be returned on a single page") // catch paging misconfiguration
706+
require.Len(t, gotDocuments, int(totalResults),
707+
"test requires results to be returned on a single page") // catch paging misconfiguration
707708

708709
assert.ElementsMatch(t, tt.wantDocuments, gotDocuments)
709710
}
@@ -751,7 +752,9 @@ func TestBoolQueryBuilder_AddTermFilter(t *testing.T) {
751752

752753
// index setup needed only once, as test cases are only reading data
753754
_, alias := tester.NewTestTypeIndexAlias(t, "arbitrary")
754-
tester.CreateDocuments(t, alias, ostesting.ToAnySlice(allDocs), []string{"id0", "id1", "id2", "id3", "id4"})
755+
tester.CreateDocuments(t, alias, ostesting.ToAnySlice(allDocs), []string{
756+
"id0", "id1", "id2", "id3", "id4",
757+
})
755758
client, err := newTestOSClient(tester.OSClient())
756759
require.NoError(t, err)
757760

@@ -939,7 +942,8 @@ func TestBoolQueryBuilder_AddTermFilter(t *testing.T) {
939942
t.Logf("sending search request to OpenSearch: %s", string(requestBody))
940943
gotDocuments, totalResults, err := client.ListDocuments(alias, requestBody)
941944
require.NoError(t, err, "listing documents failed")
942-
require.Len(t, gotDocuments, int(totalResults), "test requires results to be returned on a single page") // catch paging misconfiguration
945+
require.Len(t, gotDocuments, int(totalResults),
946+
"test requires results to be returned on a single page") // catch paging misconfiguration
943947

944948
assert.ElementsMatch(t, tt.wantDocuments, gotDocuments)
945949
})

pkg/openSearch/ostesting/test_type.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ type TestType struct {
2020
KeywordOmitEmpty string `json:"keywordOmitEmpty,omitempty"`
2121
}
2222

23-
var (
24-
// testTypeMapping is an index mapping for testType
25-
testTypeMapping string = `{
23+
// testTypeMapping is an index mapping for testType
24+
var testTypeMapping string = `{
2625
"mappings": {
2726
"properties": {
2827
"id": {
@@ -64,4 +63,3 @@ var (
6463
}
6564
}
6665
}`
67-
)

0 commit comments

Comments
 (0)