60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"go.uber.org/mock/gomock"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"git.urec56.ru/urec/chat_back_go/config"
|
|
"git.urec56.ru/urec/chat_back_go/internal/logger"
|
|
)
|
|
|
|
func TestMiddleware_Log(t *testing.T) {
|
|
testTable := []struct {
|
|
name string
|
|
requestMethod string
|
|
requestUrl string
|
|
requestID any
|
|
}{
|
|
{
|
|
name: "ok_1",
|
|
requestMethod: http.MethodGet,
|
|
requestUrl: "/users",
|
|
requestID: 1,
|
|
},
|
|
{
|
|
name: "ok_2",
|
|
requestMethod: http.MethodPost,
|
|
requestUrl: "/chat",
|
|
requestID: 10031,
|
|
},
|
|
{
|
|
name: "ok_3",
|
|
requestMethod: http.MethodPost,
|
|
requestUrl: "/chat",
|
|
},
|
|
}
|
|
|
|
for _, tc := range testTable {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
c := gomock.NewController(t)
|
|
defer c.Finish()
|
|
|
|
log := logger.NewLogger(config.Config{Mode: "TEST"})
|
|
|
|
req := httptest.NewRequest(tc.requestMethod, tc.requestUrl, nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
ctx := req.Context()
|
|
ctx = context.WithValue(ctx, RequestIDKey, tc.requestID)
|
|
req = req.WithContext(ctx)
|
|
|
|
m := &Middleware{l: log}
|
|
|
|
server := m.Log(func(w http.ResponseWriter, r *http.Request) {})
|
|
server.ServeHTTP(w, req)
|
|
})
|
|
}
|
|
}
|