Skip to content

Commit

Permalink
potential increase in code cov for resp body closing
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Dubrick <[email protected]>
  • Loading branch information
Jdubrick committed Dec 1, 2023
1 parent 0529546 commit cdc7ff6
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
43 changes: 43 additions & 0 deletions pkg/apis/recognizer/devfile_recognizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
"reflect"
"runtime"
"testing"
"net/http"
"net/http/httptest"
"io"

"github.com/devfile/alizer/pkg/apis/model"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -217,6 +220,46 @@ func TestGetUrlWithVersions(t *testing.T) {
}
}

func TestResponseBodyClosing(t *testing.T) {
tests := []struct {
name string
mockServerConfig func(w http.ResponseWriter, r *http.Request)
}{
{
name: "Successful response body closing",
mockServerConfig: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("response body content"))

Check failure on line 232 in pkg/apis/recognizer/devfile_recognizer_test.go

View workflow job for this annotation

GitHub Actions / build

Error return value of `w.Write` is not checked (errcheck)
},
},
}

for _, tc := range tests {
t.Run(tc.name, func(tt *testing.T) {
// Setup a mock HTTP server
server := httptest.NewServer(http.HandlerFunc(tc.mockServerConfig))
defer server.Close()

// Perform a request to the mock server
resp, err := http.Get(server.URL)
assert.NoError(t, err, "Unexpected error during HTTP GET")

// Close the response body
func() {
defer func() {
if err := resp.Body.Close(); err != nil {
fmt.Printf("error closing file: %s", err)
}
}()

body, err := io.ReadAll(resp.Body)
assert.NoError(t, err, "Unexpected error reading response body")
assert.Equal(t, "response body content", string(body), "Unexpected response body content")
}()
})
}
}

func TestMatchDevfiles(t *testing.T) {
tests := []struct {
name string
Expand Down
44 changes: 44 additions & 0 deletions test/check_registry/check_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package main
import (
"net/url"
"testing"
"net/http"
"net/http/httptest"
"io"
"fmt"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -76,3 +80,43 @@ func TestGetStarterProjects(t *testing.T) {
})
}
}

func TestResponseBodyClosingStarterProjects(t *testing.T) {
tests := []struct {
name string
mockServerConfig func(w http.ResponseWriter, r *http.Request)
}{
{
name: "Successful response body closing",
mockServerConfig: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("response body content"))

Check failure on line 93 in test/check_registry/check_registry_test.go

View workflow job for this annotation

GitHub Actions / build

Error return value of `w.Write` is not checked (errcheck)
},
},
}

for _, tc := range tests {
t.Run(tc.name, func(tt *testing.T) {
// Setup a mock HTTP server
server := httptest.NewServer(http.HandlerFunc(tc.mockServerConfig))
defer server.Close()

// Perform a request to the mock server
resp, err := http.Get(server.URL)
assert.NoError(t, err, "Unexpected error during HTTP GET")

// Close the response body
func() {
defer func() {
if err := resp.Body.Close(); err != nil {
fmt.Printf("error closing file: %s", err)
}
}()

body, err := io.ReadAll(resp.Body)
assert.NoError(t, err, "Unexpected error reading response body")
assert.Equal(t, "response body content", string(body), "Unexpected response body content")
}()
})
}
}

0 comments on commit cdc7ff6

Please sign in to comment.