summaryrefslogtreecommitdiffstats
path: root/logging/http/http_test.go
diff options
context:
space:
mode:
authorMarc Pervaz Boocha <mboocha@sudomsg.com>2025-08-02 20:55:11 +0530
committerMarc Pervaz Boocha <mboocha@sudomsg.com>2025-08-02 20:55:11 +0530
commitce6cf13c2d67c3368251d1eea5593198f5021330 (patch)
treed4c2347fd45fce395bf22a30e423697494d4284f /logging/http/http_test.go
downloadkit-ce6cf13c2d67c3368251d1eea5593198f5021330.tar
kit-ce6cf13c2d67c3368251d1eea5593198f5021330.tar.gz
kit-ce6cf13c2d67c3368251d1eea5593198f5021330.tar.bz2
kit-ce6cf13c2d67c3368251d1eea5593198f5021330.tar.lz
kit-ce6cf13c2d67c3368251d1eea5593198f5021330.tar.xz
kit-ce6cf13c2d67c3368251d1eea5593198f5021330.tar.zst
kit-ce6cf13c2d67c3368251d1eea5593198f5021330.zip
Initial Commitv0.1.0
Diffstat (limited to 'logging/http/http_test.go')
-rw-r--r--logging/http/http_test.go113
1 files changed, 113 insertions, 0 deletions
diff --git a/logging/http/http_test.go b/logging/http/http_test.go
new file mode 100644
index 0000000..26e5974
--- /dev/null
+++ b/logging/http/http_test.go
@@ -0,0 +1,113 @@
+package http_test
+
+import (
+ "log/slog"
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "testing"
+
+ httpHandler "go.sudomsg.com/kit/logging/http"
+ "go.sudomsg.com/kit/logging/test"
+)
+
+func TestNew(t *testing.T) {
+ t.Parallel()
+
+ tests := []struct {
+ name string
+ method string
+ url string
+ status int
+ handler http.HandlerFunc
+ }{
+ {
+ name: "No Status",
+ method: http.MethodGet,
+ url: "http://example.com/",
+ status: http.StatusOK,
+ handler: func(w http.ResponseWriter, r *http.Request) {
+ w.Write([]byte{})
+ },
+ },
+ {
+ name: "NotFound",
+ method: http.MethodGet,
+ url: "http://example.com/",
+ status: http.StatusNotFound,
+ handler: http.NotFound,
+ },
+ {
+ name: "Query",
+ method: http.MethodGet,
+ url: "http://example.com/a?b",
+ status: http.StatusNotFound,
+ handler: http.NotFound,
+ },
+ {
+ name: "Flush",
+ method: http.MethodGet,
+ url: "http://example.com/",
+ status: http.StatusOK,
+ handler: func(w http.ResponseWriter, r *http.Request) {
+ rc := http.NewResponseController(w)
+ w.Write([]byte{})
+ rc.Flush()
+ },
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ t.Parallel()
+
+ log := test.NewMockLogHandler(t)
+
+ logger := slog.New(log)
+
+ handler := httpHandler.New(tt.handler, logger)
+
+ r := httptest.NewRequest(tt.method, tt.url, nil)
+ w := httptest.NewRecorder()
+
+ handler.ServeHTTP(w, r)
+
+ records := log.Records()
+
+ if len(records) != 1 {
+ t.Fatalf("expected 1 log record, got %d", len(records))
+ }
+
+ record := records[0]
+
+ attrs := map[string]slog.Value{}
+ record.Attrs(func(a slog.Attr) bool {
+ attrs[a.Key] = a.Value
+ return true
+ })
+
+ method := attrs["method"]
+ if got := method.String(); got != tt.method {
+ t.Fatalf("expected %v log record, got %v", tt.method, got)
+ }
+ status := attrs["status"]
+ if got := int(status.Int64()); got != tt.status {
+ t.Fatalf("expected %v log record, got %v", tt.status, got)
+ }
+
+ url, err := url.Parse(tt.url)
+ if err != nil {
+ t.Fatalf("Unexpected error: %v", err)
+ }
+
+ host := attrs["host"]
+ if got := host.String(); got != url.Host {
+ t.Fatalf("expected %v log record, got %v", url.Host, got)
+ }
+ path := attrs["path"]
+ if got := path.String(); got != url.RequestURI() {
+ t.Fatalf("expected %v log record, got %v", url.RequestURI(), got)
+ }
+ })
+ }
+}