-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: reorder logger & recoverer middleware & add request-logger tests
- Loading branch information
1 parent
afce418
commit 579d7c6
Showing
4 changed files
with
68 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package observability | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/require" | ||
"github.com/supabase/auth/internal/conf" | ||
) | ||
|
||
const apiTestConfig = "../../hack/test.env" | ||
|
||
func TestLogger(t *testing.T) { | ||
logger := logrus.StandardLogger() | ||
var logBuffer bytes.Buffer | ||
config, err := conf.LoadGlobal(apiTestConfig) | ||
require.NoError(t, err) | ||
|
||
config.Logging.Level = "info" | ||
require.NoError(t, ConfigureLogging(&config.Logging)) | ||
|
||
// add request id header | ||
config.API.RequestIDHeader = "X-Request-ID" | ||
addRequestIdHandler := AddRequestID(config) | ||
// logrus should write to the buffer so we can check if the logs are output correctly | ||
logger.SetOutput(&logBuffer) | ||
logHandler := NewStructuredLogger(logger, config)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
})) | ||
w := httptest.NewRecorder() | ||
req, err := http.NewRequest(http.MethodPost, "http://example.com/path", nil) | ||
req.Header.Add("X-Request-ID", "test-request-id") | ||
require.NoError(t, err) | ||
addRequestIdHandler(logHandler).ServeHTTP(w, req) | ||
require.Equal(t, http.StatusOK, w.Code) | ||
|
||
var logs map[string]interface{} | ||
require.NoError(t, json.NewDecoder(&logBuffer).Decode(&logs)) | ||
require.Equal(t, "api", logs["component"]) | ||
require.Equal(t, http.MethodPost, logs["method"]) | ||
require.Equal(t, "/path", logs["path"]) | ||
require.Equal(t, "test-request-id", logs["request_id"]) | ||
require.NotNil(t, logs["time"]) | ||
} |