Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion pkg/gofr/http/responder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package http

import (
"bytes"
"encoding/json"
"errors"
"net/http"
Expand Down Expand Up @@ -53,9 +54,18 @@ func (r Responder) Respond(data any, err error) {
r.w.Header().Set("Content-Type", "application/json")
}

var buf bytes.Buffer
encoder := json.NewEncoder(&buf)

if err := encoder.Encode(resp); err != nil {
// JSON encoding failed (e.g. NaN, Inf, unsupported types)
http.Error(r.w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

r.w.WriteHeader(statusCode)
_, _ = r.w.Write(buf.Bytes())

_ = json.NewEncoder(r.w).Encode(resp)
}

// handleSpecialResponseTypes handles special response types that bypass JSON encoding.
Expand Down
8 changes: 6 additions & 2 deletions pkg/gofr/http_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ func createTempKeyFile(t *testing.T) string {
t.Fatalf("could not create temp key file: %v", err)
}

defer f.Close()
t.Cleanup(func() {
_ = f.Close()
})

return f.Name()
}
Expand All @@ -230,7 +232,9 @@ func createTempCertFile(t *testing.T) string {
t.Fatalf("could not create temp cert file: %v", err)
}

defer f.Close()
t.Cleanup(func() {
_ = f.Close()
})

return f.Name()
}
10 changes: 5 additions & 5 deletions pkg/gofr/logging/remotelogger/dynamic_level_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func Test_fetchAndUpdateLogLevel_InvalidResponse(t *testing.T) {

_, _ = w.Write([]byte(body))
}))
defer mockServer.Close()
t.Cleanup(mockServer.Close)

remoteService := service.NewHTTPService(mockServer.URL, logger, nil)

Expand All @@ -99,7 +99,7 @@ func Test_fetchAndUpdateLogLevel_InvalidLogLevel(t *testing.T) {
}`
_, _ = w.Write([]byte(body))
}))
defer mockServer.Close()
t.Cleanup(mockServer.Close)

remoteService2 := service.NewHTTPService(mockServer.URL, logger, nil)

Expand All @@ -120,7 +120,7 @@ func TestDynamicLoggerSuccess(t *testing.T) {
_, _ = w.Write([]byte(body))
}))

defer mockServer.Close()
t.Cleanup(mockServer.Close)

log := testutil.StdoutOutputForFunc(func() {
// Create a new remote logger with the mock server URL
Expand Down Expand Up @@ -306,7 +306,7 @@ func TestRemoteLogger_ConcurrentLevelAccess(t *testing.T) {
fmt.Fprintf(w, `{"data":{"serviceName":"test-service","logLevel":"%s"}}`, lvl)
}))

defer mockServer.Close()
t.Cleanup(mockServer.Close)

rl := &remoteLogger{
remoteURL: mockServer.URL,
Expand Down Expand Up @@ -336,7 +336,7 @@ func TestLogLevelChangeToFatal_NoExit(t *testing.T) {

_, _ = w.Write([]byte(body))
}))
defer mockServer.Close()
t.Cleanup(mockServer.Close)

// This test succeeds if it completes without the process exiting
log := testutil.StdoutOutputForFunc(func() {
Expand Down
12 changes: 6 additions & 6 deletions pkg/gofr/migration/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func TestRedis_Get(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
t.Cleanup(ctrl.Finish)

mockCmd := NewMockRedis(ctrl)
mockCmd.EXPECT().Get(t.Context(), "test_key").Return(&goRedis.StringCmd{})
Expand All @@ -27,7 +27,7 @@ func TestRedis_Get(t *testing.T) {

func TestRedis_Set(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
t.Cleanup(ctrl.Finish)

mockCmd := NewMockRedis(ctrl)
mockCmd.EXPECT().Set(t.Context(), "test_key", "test_value", time.Duration(0)).Return(&goRedis.StatusCmd{})
Expand All @@ -40,7 +40,7 @@ func TestRedis_Set(t *testing.T) {

func TestRedis_Del(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
t.Cleanup(ctrl.Finish)

mockCmd := NewMockRedis(ctrl)
mockCmd.EXPECT().Del(t.Context(), "test_key").Return(&goRedis.IntCmd{})
Expand All @@ -53,7 +53,7 @@ func TestRedis_Del(t *testing.T) {

func TestRedis_Rename(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
t.Cleanup(ctrl.Finish)

mockCmd := NewMockRedis(ctrl)
mockCmd.EXPECT().Rename(t.Context(), "test_key", "test_new_key").Return(&goRedis.StatusCmd{})
Expand All @@ -66,7 +66,7 @@ func TestRedis_Rename(t *testing.T) {

func TestRedisMigrator_GetLastMigration(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
t.Cleanup(ctrl.Finish)

c, mocks := container.NewMockContainer(t)
mockMigrator := NewMockmigrator(ctrl)
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestRedisMigrator_GetLastMigration(t *testing.T) {

func TestRedisMigrator_beginTransaction(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
t.Cleanup(ctrl.Finish)

c, mocks := container.NewMockContainer(t)
mockMigrator := NewMockmigrator(ctrl)
Expand Down
2 changes: 1 addition & 1 deletion pkg/gofr/migration/scylla_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func TestScyllaBeginTransaction(t *testing.T) {

func TestScyllaMigrator_Rollback(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
t.Cleanup(ctrl.Finish)

mockContainer := &container.Container{
Logger: &panicLogger{},
Expand Down
2 changes: 1 addition & 1 deletion pkg/gofr/migration/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func TestBeginTransaction_ReplaceSQLTx(t *testing.T) {

func TestCheckAndCreateMigrationTable_ErrorCreatingTable(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
t.Cleanup(ctrl.Finish)

mockContainer, mocks := container.NewMockContainer(t)
mocks.SQL.ExpectExec(createSQLGoFrMigrationsTable).WillReturnError(errCreateTable)
Expand Down