From 0e330c1279f5dc5e13ed451abb9b2021afc9bedb Mon Sep 17 00:00:00 2001 From: "gaurav.marwal" Date: Sun, 28 Apr 2024 15:32:29 +0530 Subject: [PATCH] Fix lint issues in internal/senddata --- .../contentprocessor/contentprocessor.go | 4 +++- .../contentprocessor/contentprocessor_test.go | 19 ++++++++++++++----- workhorse/internal/senddata/injecter.go | 8 +++++++- workhorse/internal/senddata/senddata.go | 1 + workhorse/internal/senddata/writer_test.go | 4 ++-- 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/workhorse/internal/senddata/contentprocessor/contentprocessor.go b/workhorse/internal/senddata/contentprocessor/contentprocessor.go index f604a481fa264d..48e8da28bc7d5d 100644 --- a/workhorse/internal/senddata/contentprocessor/contentprocessor.go +++ b/workhorse/internal/senddata/contentprocessor/contentprocessor.go @@ -1,3 +1,4 @@ +// Package contentprocessor provides content header processing for HTTP responses package contentprocessor import ( @@ -122,7 +123,7 @@ func (cd *contentDisposition) isUnbuffered() bool { } func (cd *contentDisposition) Flush() { - cd.FlushError() + _ = cd.FlushError() } // FlushError lets http.ResponseController to be used to flush the underlying http.ResponseWriter. @@ -131,6 +132,7 @@ func (cd *contentDisposition) FlushError() error { if err != nil { return err } + return http.NewResponseController(cd.rw).Flush() } diff --git a/workhorse/internal/senddata/contentprocessor/contentprocessor_test.go b/workhorse/internal/senddata/contentprocessor/contentprocessor_test.go index 28a8d905ff2e21..1a4cee3b396ed3 100644 --- a/workhorse/internal/senddata/contentprocessor/contentprocessor_test.go +++ b/workhorse/internal/senddata/contentprocessor/contentprocessor_test.go @@ -12,8 +12,10 @@ import ( "github.com/stretchr/testify/require" ) +const testData = "Hello world!" + func TestFailSetContentTypeAndDisposition(t *testing.T) { - testCaseBody := "Hello world!" + testCaseBody := testData h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _, err := io.WriteString(w, testCaseBody) @@ -21,15 +23,17 @@ func TestFailSetContentTypeAndDisposition(t *testing.T) { }) resp := makeRequest(t, h, testCaseBody, "") + defer func() { _ = resp.Body.Close() }() require.Equal(t, "", resp.Header.Get(headers.ContentDispositionHeader)) require.Equal(t, "", resp.Header.Get(headers.ContentTypeHeader)) } func TestSuccessSetContentTypeAndDispositionFeatureEnabled(t *testing.T) { - testCaseBody := "Hello world!" + testCaseBody := testData resp := makeRequest(t, nil, testCaseBody, "") + defer func() { _ = resp.Body.Close() }() require.Equal(t, "inline", resp.Header.Get(headers.ContentDispositionHeader)) require.Equal(t, "text/plain; charset=utf-8", resp.Header.Get(headers.ContentTypeHeader)) @@ -46,7 +50,7 @@ func TestSetProperContentTypeAndDisposition(t *testing.T) { desc: "text type", contentType: "text/plain; charset=utf-8", contentDisposition: "inline", - body: "Hello world!", + body: testData, }, { desc: "HTML type", @@ -179,6 +183,7 @@ func TestSetProperContentTypeAndDisposition(t *testing.T) { for _, tc := range testCases { t.Run(tc.desc, func(t *testing.T) { resp := makeRequest(t, nil, tc.body, tc.contentDisposition) + defer func() { _ = resp.Body.Close() }() require.Equal(t, tc.contentType, resp.Header.Get(headers.ContentTypeHeader)) require.Equal(t, tc.contentDisposition, resp.Header.Get(headers.ContentDispositionHeader)) @@ -218,6 +223,7 @@ func TestFailOverrideContentType(t *testing.T) { }) resp := makeRequest(t, h, tc.body, "") + defer func() { _ = resp.Body.Close() }() require.Equal(t, tc.responseContentType, resp.Header.Get(headers.ContentTypeHeader)) }) @@ -225,7 +231,7 @@ func TestFailOverrideContentType(t *testing.T) { } func TestSuccessOverrideContentDispositionFromInlineToAttachment(t *testing.T) { - testCaseBody := "Hello world!" + testCaseBody := testData h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { // We are pretending to be upstream or an inner layer of the ResponseWriter chain @@ -236,6 +242,7 @@ func TestSuccessOverrideContentDispositionFromInlineToAttachment(t *testing.T) { }) resp := makeRequest(t, h, testCaseBody, "") + defer func() { _ = resp.Body.Close() }() require.Equal(t, "attachment", resp.Header.Get(headers.ContentDispositionHeader)) } @@ -252,6 +259,7 @@ func TestInlineContentDispositionForPdfFiles(t *testing.T) { }) resp := makeRequest(t, h, testCaseBody, "") + defer func() { _ = resp.Body.Close() }() require.Equal(t, "inline", resp.Header.Get(headers.ContentDispositionHeader)) } @@ -268,6 +276,7 @@ func TestFailOverrideContentDispositionFromAttachmentToInline(t *testing.T) { }) resp := makeRequest(t, h, testCaseBody, "") + defer func() { _ = resp.Body.Close() }() require.Equal(t, "attachment", resp.Header.Get(headers.ContentDispositionHeader)) } @@ -295,7 +304,7 @@ func TestWriteHeadersCalledOnce(t *testing.T) { rw := &contentDisposition{rw: recorder} rw.WriteHeader(400) require.Equal(t, 400, rw.status) - require.Equal(t, true, rw.sentStatus) + require.True(t, rw.sentStatus) rw.WriteHeader(200) require.Equal(t, 400, rw.status) diff --git a/workhorse/internal/senddata/injecter.go b/workhorse/internal/senddata/injecter.go index d5739d2a053288..0544a52b7381e1 100644 --- a/workhorse/internal/senddata/injecter.go +++ b/workhorse/internal/senddata/injecter.go @@ -1,3 +1,4 @@ +// Package senddata provides functionality for injecting data into HTTP responses package senddata import ( @@ -7,29 +8,34 @@ import ( "strings" ) +// Injecter defines the interface for data injection type Injecter interface { Match(string) bool Inject(http.ResponseWriter, *http.Request, string) Name() string } +// Prefix represents a prefix for injection type Prefix string +// Match checks if the given string starts with the prefix func (p Prefix) Match(s string) bool { return strings.HasPrefix(s, string(p)) } +// Unpack decodes and unmarshals the sendData string func (p Prefix) Unpack(result interface{}, sendData string) error { jsonBytes, err := base64.URLEncoding.DecodeString(strings.TrimPrefix(sendData, string(p))) if err != nil { return err } - if err := json.Unmarshal([]byte(jsonBytes), result); err != nil { + if err := json.Unmarshal(jsonBytes, result); err != nil { return err } return nil } +// Name returns the name of the prefix without the colon suffix func (p Prefix) Name() string { return strings.TrimSuffix(string(p), ":") } diff --git a/workhorse/internal/senddata/senddata.go b/workhorse/internal/senddata/senddata.go index f0d1da021799dd..39b51ad2263141 100644 --- a/workhorse/internal/senddata/senddata.go +++ b/workhorse/internal/senddata/senddata.go @@ -37,6 +37,7 @@ type sendDataResponseWriter struct { injecters []Injecter } +// SendData intercepts HTTP responses and allows injecting content before sending them func SendData(h http.Handler, injecters ...Injecter) http.Handler { return contentprocessor.SetContentHeaders(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { s := sendDataResponseWriter{ diff --git a/workhorse/internal/senddata/writer_test.go b/workhorse/internal/senddata/writer_test.go index b0c808c6158844..61959d18009269 100644 --- a/workhorse/internal/senddata/writer_test.go +++ b/workhorse/internal/senddata/writer_test.go @@ -41,7 +41,7 @@ func TestWriter(t *testing.T) { n, err := rw.Write([]byte(upstreamResponse)) require.NoError(t, err) - require.Equal(t, len(upstreamResponse), n, "bytes written") + require.Len(t, upstreamResponse, n, "bytes written") recorder.Flush() @@ -62,7 +62,7 @@ const ( type testInjecter struct{} -func (ti *testInjecter) Inject(w http.ResponseWriter, r *http.Request, sendData string) { +func (ti *testInjecter) Inject(w http.ResponseWriter, _ *http.Request, _ string) { io.WriteString(w, testInjecterData) } -- GitLab