forked from go-chi/httplog
-
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.
Ensure the httplog.Options from httplog.Logger are present in middlew…
…are.LogEntry This fixes go-chi#35 as the httplog.Options#JSON now causes the log entry to not be pretty printed to stderr if set to true.
- Loading branch information
1 parent
4a0d794
commit 4d696fb
Showing
2 changed files
with
47 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package httplog | ||
|
||
import ( | ||
"log/slog" | ||
"net/http" | ||
"net/http/httptest" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/go-chi/chi/v5" | ||
"github.com/go-chi/chi/v5/middleware" | ||
) | ||
|
||
// Test that ensures the Logger options match the options of a LogEntry | ||
func Test_requestLogger_NewLogEntry_Options(t *testing.T) { | ||
opts := Options{ | ||
Concise: true, | ||
JSON: true, | ||
} | ||
logger := &Logger{ | ||
Logger: slog.Default(), | ||
Options: opts, | ||
} | ||
|
||
r := chi.NewMux() | ||
r.Use(Handler(logger)) | ||
|
||
var entryFromRequest middleware.LogEntry | ||
r.Get("/", func(w http.ResponseWriter, r *http.Request) { | ||
entryFromRequest = middleware.GetLogEntry(r) | ||
w.Write([]byte("OK")) | ||
}) | ||
|
||
req := httptest.NewRequest("GET", "/", nil) | ||
w := httptest.NewRecorder() | ||
|
||
r.ServeHTTP(w, req) | ||
|
||
rEntry, ok := entryFromRequest.(*RequestLoggerEntry) | ||
if !ok { | ||
t.Errorf("expected log entry to be a RequestLoggerEntry") | ||
} | ||
if !reflect.DeepEqual(opts, rEntry.Options) { | ||
t.Errorf("entry = %v, want %v", entryFromRequest, opts) | ||
} | ||
} |