-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
212 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,54 @@ | ||
package api | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/sirupsen/logrus" | ||
"github.com/sirupsen/logrus/hooks/test" | ||
"github.com/stretchr/testify/assert" | ||
"go-service-template/internal/infrastructure" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestLoggingMiddleware(t *testing.T) { | ||
// Creating a test logger and replacing the infrastructure log with it | ||
testLogger, hook := test.NewNullLogger() | ||
infrastructure.SetLog(testLogger) // You'll need to create this method to set the logger in the infrastructure package | ||
|
||
// Creating a sample next handler | ||
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
_, _ = w.Write([]byte("next handler")) | ||
var _ = Describe("LoggingMiddleware", func() { | ||
It("should log the request and call the next handler", func() { | ||
// Creating a test logger and replacing the infrastructure log with it | ||
testLogger, hook := test.NewNullLogger() | ||
infrastructure.SetLog(testLogger) // You'll need to create this method to set the logger in the infrastructure package | ||
|
||
// Creating a sample next handler | ||
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
_, _ = w.Write([]byte("next handler")) | ||
}) | ||
|
||
// Creating a request to pass to the handler | ||
req, err := http.NewRequest("GET", "/test", nil) | ||
req.RequestURI = "/test" // Explicitly setting the RequestURI field | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
// Creating a ResponseRecorder to record the response | ||
rr := httptest.NewRecorder() | ||
|
||
// Creating the middleware handler | ||
handler := LoggingMiddleware(nextHandler) | ||
|
||
// Serve the request | ||
handler.ServeHTTP(rr, req) | ||
|
||
// Checking if next handler was called | ||
Expect(rr.Body.String()).To(Equal("next handler")) | ||
|
||
// Checking if the log entry was made | ||
Expect(len(hook.Entries)).To(Equal(1)) | ||
entry := hook.LastEntry() | ||
Expect(entry.Level).To(Equal(logrus.InfoLevel)) | ||
Expect(entry.Message).To(Equal("Handled request")) | ||
Expect(entry.Data["method"]).To(Equal("GET")) | ||
Expect(entry.Data["uri"]).To(Equal("/test")) // Checking for the expected URI | ||
Expect(entry.Data["remote"]).To(Equal(req.RemoteAddr)) | ||
|
||
// Since we don't control the exact time duration, we'll just check if it exists | ||
_, durationExists := entry.Data["duration"] | ||
Expect(durationExists).To(BeTrue(), "Duration should be logged") | ||
}) | ||
|
||
// Creating a request to pass to the handler | ||
req, err := http.NewRequest("GET", "/test", nil) | ||
req.RequestURI = "/test" // Explicitly setting the RequestURI field | ||
assert.NoError(t, err) | ||
|
||
// Creating a ResponseRecorder to record the response | ||
rr := httptest.NewRecorder() | ||
|
||
// Creating the middleware handler | ||
handler := LoggingMiddleware(nextHandler) | ||
|
||
// Serve the request | ||
handler.ServeHTTP(rr, req) | ||
|
||
// Checking if next handler was called | ||
assert.Equal(t, "next handler", rr.Body.String()) | ||
|
||
// Checking if the log entry was made | ||
assert.Equal(t, 1, len(hook.Entries)) | ||
entry := hook.LastEntry() | ||
assert.Equal(t, logrus.InfoLevel, entry.Level) | ||
assert.Equal(t, "Handled request", entry.Message) | ||
assert.Equal(t, "GET", entry.Data["method"]) | ||
assert.Equal(t, "/test", entry.Data["uri"]) // Checking for the expected URI | ||
assert.Equal(t, req.RemoteAddr, entry.Data["remote"]) | ||
|
||
// Since we don't control the exact time duration, we'll just check if it exists | ||
_, durationExists := entry.Data["duration"] | ||
assert.True(t, durationExists, "Duration should be logged") | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package api | ||
|
||
import ( | ||
"go-service-template/internal/infrastructure" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestInfrastructure(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
infrastructure.GetLog() | ||
RunSpecs(t, "API Suite") | ||
} |
Oops, something went wrong.