fix code smells & fix some tests

This commit is contained in:
Andrea Raponi 2024-11-04 18:07:04 +01:00
parent 6161f22107
commit 45e2b87e3d
13 changed files with 173 additions and 21 deletions

View File

@ -6,6 +6,8 @@ import (
"dito/logging" "dito/logging"
"log/slog" "log/slog"
"testing" "testing"
"github.com/redis/go-redis/v9"
) )
// TestLoggerUpdate tests if the logger updates correctly when the log level changes. // TestLoggerUpdate tests if the logger updates correctly when the log level changes.
@ -21,7 +23,11 @@ func TestLoggerUpdate(t *testing.T) {
config.UpdateConfig(initialConfig) config.UpdateConfig(initialConfig)
// Create a new Dito instance with the initial logger // Create a new Dito instance with the initial logger
dito := NewDito(nil, logging.InitializeLogger(initialConfig.Logging.Level)) // Create a mock Redis client and HTTP transport config for testing
mockRedisClient := &redis.Client{}
mockHTTPTransportConfig := &config.HTTPTransportConfig{}
dito := NewDito(mockRedisClient, mockHTTPTransportConfig, logging.InitializeLogger(initialConfig.Logging.Level))
// Check if the initial logger level is set to "info" // Check if the initial logger level is set to "info"
if dito.Logger.Handler().Enabled(context.Background(), slog.LevelDebug) { if dito.Logger.Handler().Enabled(context.Background(), slog.LevelDebug) {

View File

@ -42,10 +42,10 @@ transport:
# List of location configurations for proxying requests. # List of location configurations for proxying requests.
locations: locations:
- path: "^/test-ws$" # Il path per cui vuoi abilitare il WebSocket - path: "^/test-ws$"
target_url: "wss://echo.websocket.org" target_url: "wss://echo.websocket.org"
enable_websocket: true # Abilita il supporto WebSocket per questa location enable_websocket: true n
replace_path: true # Sostituisce il path con il target URL replace_path: true
- path: "^/dito$" # Regex pattern to match the request path. - path: "^/dito$" # Regex pattern to match the request path.
target_url: https://httpbin.org/get target_url: https://httpbin.org/get

BIN
bin/dito

Binary file not shown.

View File

@ -12,7 +12,6 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"github.com/redis/go-redis/v9"
"log" "log"
"log/slog" "log/slog"
"net/http" "net/http"
@ -22,6 +21,8 @@ import (
"runtime" "runtime"
"syscall" "syscall"
"time" "time"
"github.com/redis/go-redis/v9"
) )
// main is the entry point of the application. // main is the entry point of the application.
@ -122,7 +123,7 @@ func StartServer(dito *app.Dito) {
// Attempt to gracefully shut down the server. // Attempt to gracefully shut down the server.
if err := server.Shutdown(ctx); err != nil { if err := server.Shutdown(ctx); err != nil {
dito.Logger.Error("Server forced to shutdown: ", err) dito.Logger.Error("Server forced to shutdown", "error", err)
} else { } else {
dito.Logger.Info("Server shut down gracefully.") dito.Logger.Info("Server shut down gracefully.")
} }
@ -135,7 +136,7 @@ func StartServer(dito *app.Dito) {
// Start the HTTP server. // Start the HTTP server.
if err := server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) { if err := server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
dito.Logger.Error("Server failed to start: ", err) dito.Logger.Error("Server failed to start", "error", err)
log.Fatal(err) log.Fatal(err)
} }
@ -150,7 +151,7 @@ func startProfiling(logger *slog.Logger) {
go func() { go func() {
logger.Info("Starting pprof on :6060") logger.Info("Starting pprof on :6060")
if err := http.ListenAndServe("localhost:6060", nil); err != nil { if err := http.ListenAndServe("localhost:6060", nil); err != nil {
logger.Error("pprof failed:", err) logger.Error("pprof failed", "error", err)
} }
}() }()

View File

@ -80,7 +80,7 @@ func ServeProxy(dito *app.Dito, locationIndex int, lrw http.ResponseWriter, r *h
targetURL, err := url.Parse(location.TargetURL) targetURL, err := url.Parse(location.TargetURL)
if err != nil { if err != nil {
dito.Logger.Error("Error parsing the target URL: ", err) dito.Logger.Error("Error parsing the target URL: ", "error", err)
http.Error(lrw, InternalServerErrorMessage, http.StatusInternalServerError) http.Error(lrw, InternalServerErrorMessage, http.StatusInternalServerError)
return return
} }

View File

@ -6,13 +6,15 @@ import (
"dito/config" "dito/config"
"dito/handlers" "dito/handlers"
"dito/logging" "dito/logging"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"io" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"regexp" "regexp"
"testing" "testing"
"time"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
) )
// setupTestConfig initializes a sample configuration for testing. // setupTestConfig initializes a sample configuration for testing.
@ -56,7 +58,29 @@ func setupDito() *app.Dito {
logger := logging.InitializeLogger("info") logger := logging.InitializeLogger("info")
// Create a new Dito instance. // Create a new Dito instance.
return app.NewDito(redisClient, logger) // Create a sample HTTPTransportConfig.
httpTransportConfig := &config.HTTPTransportConfig{
IdleConnTimeout: 90 * time.Second,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 10,
MaxConnsPerHost: 100,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
DisableCompression: false,
ForceHTTP2: true,
DialTimeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
CertFile: "testdata/test_cert.pem",
KeyFile: "testdata/test_key.pem",
CaFile: "testdata/test_ca.pem",
}
dito := app.NewDito(redisClient, httpTransportConfig, logger)
if dito == nil {
panic("Failed to initialize Dito instance")
}
return dito
} }
func TestDynamicProxyHandler(t *testing.T) { func TestDynamicProxyHandler(t *testing.T) {
@ -71,7 +95,9 @@ func TestDynamicProxyHandler(t *testing.T) {
// Create a ResponseRecorder to capture the response. // Create a ResponseRecorder to capture the response.
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
// Create a dummy request body. if dito == nil {
t.Fatal("Dito instance is nil")
}
req.Body = io.NopCloser(bytes.NewBufferString("Test body")) req.Body = io.NopCloser(bytes.NewBufferString("Test body"))
// Call the handler. // Call the handler.

22
handlers/testdata/client_cert.pem vendored Normal file
View File

@ -0,0 +1,22 @@
-----BEGIN CERTIFICATE-----
MIIDujCCAqKgAwIBAgIUcmdRANmvrDIZy4t8MBj36sODNbkwDQYJKoZIhvcNAQEL
BQAwbDELMAkGA1UEBhMCSVQxDjAMBgNVBAgMBUl0YWx5MQ0wCwYDVQQHDARSb21l
MRQwEgYDVQQKDAtZb3VyQ29tcGFueTEWMBQGA1UECwwNSVQ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EN+1uRzzXdZ/vhn4a+6olRbR3gr/OhXL0QRGuMsRCcBdPaiGRkQ==
-----END CERTIFICATE-----

28
handlers/testdata/client_key.pem vendored Normal file
View File

@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCgkrWaf/Q0qGj7
roz+GfYtjWDvpJ1y+dYZvbGP4ld2lllZstjpY33cWqpCJNFFzrzwt60IOJcZq5Gx
D9xms4ALxOB893zQqwpaOjLbrj8KCIsLAiMRmv5O8/kG5uQIZVW9luLlK5fnp+47
tw3QF8tMcpUpqzj/jwWhy7tJvLBbrXJFNLyE7SlDnjFnxBFquQEMfqdlTwkWOm5n
xHHIAVF58CfXtM/S1pG66UOUamHXJCtU5sWwJ3wn+Wd1O3w9F/sZxYXaR3k9w54V
rK5c8bsCiEDn0j6gz9WsJ218udUixMwNcPZidXe29RUit7e9rsyRSrZQV5Cm2p36
ikUfrRkLAgMBAAECggEACs/mJfZnF+NqvxIeHbJ006HMVnnNcLAzw1b1E/AU0Jmu
GYg3HkjPPd1MnDIrdaZgLY571B/8K91tuXzzOJnuNAMSB9Tuu1ERD0mrVZ4YuF27
5kYpQ1m3tWxGWlMfJRUuhIFNwbDbESoX56VBt/KqNnl7fStLP4k+j71bdpfogVVQ
rgv8JP5d1XAdvR/l+U/j3OqHTBWyhAjleMPhVSEwATMzASh9b73/cM8wFF/DxCwu
2WHYbjUUoAip4gp0tjSdKHCA2fYFFDe/koSVoP4nEZFh8s/3wu8wbF06UC1cRJPP
U6bij3YKSy/HISlyva2EefTiH8FGA6ztAF4nGwze9QKBgQDBJcutcvtGg9gZCRYY
8EOwBFcFM4FnAgnRUPLLqqx3sMtQpBlk7u7I7ST6/e+vpKOYR4PeEYVnH7CCjjA0
UueuoYukjaShOzM2M8l24egGm2mZn3sd75g4QlFL7Is8gGSAfhobhmHZ7VJBLYeW
HcI9p7o/Er/9s+T73MQWhJsF9wKBgQDU00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AYTAklUMQ4wDAYDVQQIDAVJdGFseTENMAsGA1UEBwwEUm9tZTEU
MBIGA1UECgwLWW91ckNvbXBhbnkxFjAUBgNVBAsMDUlUIERlcGFydG1lbnQxEDAO
BgNVBAMMB1Rlc3QgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC9
fgucEf299EyQVH21JvTqkWodhXk85kchUR7n7UHmanU0/NL9q0nnGCZpTzm4FYK2
rlhCGLUbIQzZG+MhmBXAVo2W6ji23LBVT/TRX7/MwGeltREdrXb8P9EkxzFajyAR
+50drR9wU2BljKbwTExzjUWsFN0yjNWC94eD1sDRTXv/K86MNfqGda3IL+HDAOMG
7YJJLR5Zzq+4LqLXauGfcpNsl6/beZiNlxhb3uZbjr6elGzhEFGywVIXleWPFXhZ
ssIq2qig4yqX2PYyTbaCC3ZhFVB23+gKdv+k4raG6TsaDOKHEA3k0dyFWG2NUoqz
6bVoeJEr4H/n8SuF1OX5AgMBAAGjUzBRMB0GA1UdDgQWBBTLP/FlZN+sOc93zW1m
l7RATyTnKDAfBgNVHSMEGDAWgBTLP/FlZN+sOc93zW1ml7RATyTnKDAPBgNVHRMB
Af8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQCDh1A+PE2fZ6VdxInwZzhlzQQm
43EZgKNBj67baFR2LLlqnJwmLJ+Q8VblHSjmdpkFYbF0wGniTI2eL3t/wAEroUxi
nwpBdzklZDkU5fjP/rKvDJffZelFuSNLexD7znKyp60+E/UtIHCy3qJepVdlUDKj
0hJe086H9m+2JDfVSpaadqVwiCZMLDe5LheFyiw1nNBY0t42ENHbK7Fu1bbjUh7a
EpkeGXDMMAHND0MDqFteYsa5AcULcFUAkTE3+jRpDyWXeQyq4xXYjF0xNDMw20ed
mGsOV4b1M74TrTY/aS4qU7UzbgU8kchVfrVVu+eqbcOLPTED1r5h3oPuHaH1
-----END CERTIFICATE-----

17
handlers/testdata/test_cert.pem vendored Normal file
View File

@ -0,0 +1,17 @@
-----BEGIN CERTIFICATE-----
MIICrzCCAZcCFHEjMjahJ2kOSszF6i6nRWFJBCy/MA0GCSqGSIb3DQEBCwUAMBIx
EDAOBgNVBAMMB1Rlc3QgQ0EwHhcNMjQxMDA3MTQ0NDQzWhcNMjUxMDA3MTQ0NDQz
WjAWMRQwEgYDVQQDDAtUZXN0IENsaWVudDCCASIwDQYJKoZIhvcNAQEBBQADggEP
ADCCAQoCggEBAIpTcC3WbWrHoY8MNuOJ7FnQjGlMtpYxmUByfAOStYrvGU3QYJaM
tzZ2wKvpRcPthkBZiG/atYiouN54TrnA72wK/uV2ZzWKxWLxfGnIxUokSyaaUTnR
g6Oaog3dQaCkf+u3hMA7f4jElEj49x1Q92rJTgkyJfKmry/G2j/p1LI3W9lfFa8g
Yhf3hdZj0QIQnrDdYSpWJ2aCinMFpft4X7Mj9KjfVdbISIKZhWDOU2TBh/Z2fZG1
q1lYcBvdgb9niPIh0ZOdYv/ZEL8bgheGNbtWp5NofXiaC90oPL8pkNY1IzMhjzUi
fcCgADEbvHPILZMLyWWetZrxVMDh97X8dCMCAwEAATANBgkqhkiG9w0BAQsFAAOC
AQEAGLKZymIlINJWZDxYUVyfLzsGSqbQHf2jWo8QMPk1p9bHK/a9mj2klNmeIuF1
eFg7jh5DPaIgKuqHKieuQ5qOnx3XVeQTQI5wM9ygwBuhqbiwKLAsCLp6teheRbTY
JMZI/91L/DsXZtFq9GsTCw1dkMi3rw8aj4PKLUdCJEcEbpiN78LSLh5VJYXgH/WI
a14SVseZOt0NFlGk8o1UzahduJ24PtLcC0NXPvUUkJP8wrFOgkVF8mYr4kb3k28n
BCFb6kcSbn7o9svtTxhJHLF+dsHN1L7tUG6WhnNv5uM/n2qvEyxjLxRIIdh/Bv0M
paCRZDQXjI8oep7JDJXmfWz2PQ==
-----END CERTIFICATE-----

28
handlers/testdata/test_key.pem vendored Normal file
View File

@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEF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DWMb
syRdmdxfGwKBgHGqPT2NuyDokQDb+eL2kc3eJTIzZl+HFBWOVgpuf+zO/BenHRQa
mTrlDZz7GUh4wuwZuwLPdA6/dfTVwlzJsiaM5huM9AoavRVssclj2wEEVjZXwsB5
vXIug7eiC6MHW9ZOK5Cu4JwjnTR7sQ8bwb9AeWwc/2GRxxb7Y6Xk3rhRAoGAEMMB
6FcNidDAAqHqJVdiM/OlBl7xuHjBNmKluhyWJHHhRY4QHyGpEYmZLRo2ZauMur9Z
cWTw2Ahy8OZuQ6+EckHmdDz+4PogFl4ZruLjZ5A7dF7NuUb0LIL54jBwZ4Q9QRQt
Kf+3DRFHKC2Z3csLvxbrLzfGL5JqaUUflF3DT+0CgYAmmtFUh479ekFEykFQ8ykn
AoO/8l1lJ3ulwzfvJVDmzteIuLwqCq5a6BOMqBZR01rPFnqCkgVJ3bQQ3wq6dyxq
0znKjn4QUZxq+bqLQXtgJJcwq52Mg5iK34w+213WhX6Dd5z+DzsocMY47MVJuWFQ
x+NEmDXAZ3rx2PPy+QWqTw==
-----END PRIVATE KEY-----

View File

@ -3,14 +3,15 @@ package logging
import ( import (
"dito/writer" "dito/writer"
"fmt" "fmt"
"github.com/fatih/color"
"github.com/gorilla/websocket"
"github.com/lmittmann/tint"
"log/slog" "log/slog"
"net/http" "net/http"
"os" "os"
"strings" "strings"
"time" "time"
"github.com/fatih/color"
"github.com/gorilla/websocket"
"github.com/lmittmann/tint"
) )
var logger *slog.Logger var logger *slog.Logger
@ -134,9 +135,9 @@ func LogWebSocketMessageOLD(messageType int, message []byte, err error, duration
} }
if err != nil { if err != nil {
logger.Error(fmt.Sprintf("WebSocket %s Message Error: %v", "error", messageTypeStr, err)) logger.Error(fmt.Sprintf("WebSocket %s Message Error: %v", messageTypeStr, err))
} else { } else {
logger.Info(fmt.Sprintf("WebSocket %s Message: %s (%.6f seconds)", "error", messageTypeStr, string(message), duration.Seconds())) logger.Info(fmt.Sprintf("WebSocket %s Message: %s (%.6f seconds)", messageTypeStr, string(message), duration.Seconds()))
} }
} }

View File

@ -2,10 +2,11 @@ package logging
import ( import (
"bytes" "bytes"
"github.com/lmittmann/tint"
"log/slog" "log/slog"
"os" "os"
"github.com/lmittmann/tint"
"net/http" "net/http"
"testing" "testing"
"time" "time"
@ -22,7 +23,7 @@ func TestLogRequestVerbose(t *testing.T) {
duration := 2 * time.Second duration := 2 * time.Second
body := []byte("request body") body := []byte("request body")
LogRequestVerbose(req, &body, &headers, statusCode, duration) LogRequestVerbose(req, body, headers, statusCode, duration)
} }
// TestLogRequestCompact tests the LogRequestCompact function. // TestLogRequestCompact tests the LogRequestCompact function.
@ -36,7 +37,7 @@ func TestLogRequestCompact(t *testing.T) {
duration := 2 * time.Second duration := 2 * time.Second
body := []byte("request body") body := []byte("request body")
LogRequestCompact(req, &body, &headers, statusCode, duration) LogRequestCompact(req, body, headers, statusCode, duration)
} }
// InitializeLogger initializes a new logger with the specified log level. // InitializeLogger initializes a new logger with the specified log level.