Compare commits

...

2 Commits

3 changed files with 103 additions and 47 deletions

View File

@@ -36,21 +36,10 @@ func setupCachingTestContext(t *testing.T) *testContext {
staticDir := t.TempDir() staticDir := t.TempDir()
router := server.NewRouter(server.RouterConfig{ router := server.NewRouter(newRouterConfigBuilder().
AuthHandler: authHandler, withIndividualHandlers(authHandler, postHandler, voteHandler, userHandler, apiHandler, authService).
PostHandler: postHandler, withStaticDir(staticDir).
VoteHandler: voteHandler, build())
UserHandler: userHandler,
APIHandler: apiHandler,
AuthService: authService,
PageHandler: nil,
StaticDir: staticDir,
Debug: false,
DisableCache: false,
DisableCompression: false,
DBMonitor: middleware.NewInMemoryDBMonitor(),
RateLimitConfig: testutils.AppTestConfig.RateLimit,
})
return &testContext{ return &testContext{
Router: router, Router: router,

View File

@@ -11,6 +11,9 @@ import (
"testing" "testing"
"time" "time"
"golang.org/x/crypto/bcrypt"
"goyco/internal/config"
"goyco/internal/database" "goyco/internal/database"
"goyco/internal/handlers" "goyco/internal/handlers"
"goyco/internal/middleware" "goyco/internal/middleware"
@@ -18,8 +21,6 @@ import (
"goyco/internal/server" "goyco/internal/server"
"goyco/internal/services" "goyco/internal/services"
"goyco/internal/testutils" "goyco/internal/testutils"
"golang.org/x/crypto/bcrypt"
) )
type testContext struct { type testContext struct {
@@ -74,24 +75,100 @@ func setupCommonHandlers(t *testing.T, suite *testutils.ServiceSuite, useMonitor
} }
} }
func buildRouterConfig(h *commonHandlers, staticDir string, pageHandler *handlers.PageHandler) server.RouterConfig { type routerConfigBuilder struct {
return server.RouterConfig{ authHandler *handlers.AuthHandler
AuthHandler: h.AuthHandler, postHandler *handlers.PostHandler
PostHandler: h.PostHandler, voteHandler *handlers.VoteHandler
VoteHandler: h.VoteHandler, userHandler *handlers.UserHandler
UserHandler: h.UserHandler, apiHandler *handlers.APIHandler
APIHandler: h.APIHandler, authService middleware.TokenVerifier
AuthService: h.AuthService, pageHandler *handlers.PageHandler
PageHandler: pageHandler, staticDir string
StaticDir: staticDir, debug bool
Debug: false, disableCache bool
DisableCache: false, disableCompression bool
DisableCompression: false, dbMonitor middleware.DBMonitor
DBMonitor: middleware.NewInMemoryDBMonitor(), rateLimitConfig config.RateLimitConfig
RateLimitConfig: testutils.AppTestConfig.RateLimit, }
func newRouterConfigBuilder() *routerConfigBuilder {
return &routerConfigBuilder{
debug: false,
disableCache: false,
disableCompression: false,
dbMonitor: middleware.NewInMemoryDBMonitor(),
rateLimitConfig: testutils.AppTestConfig.RateLimit,
} }
} }
func (b *routerConfigBuilder) withHandlers(h *commonHandlers) *routerConfigBuilder {
b.authHandler = h.AuthHandler
b.postHandler = h.PostHandler
b.voteHandler = h.VoteHandler
b.userHandler = h.UserHandler
b.apiHandler = h.APIHandler
b.authService = h.AuthService
return b
}
func (b *routerConfigBuilder) withIndividualHandlers(
authHandler *handlers.AuthHandler,
postHandler *handlers.PostHandler,
voteHandler *handlers.VoteHandler,
userHandler *handlers.UserHandler,
apiHandler *handlers.APIHandler,
authService middleware.TokenVerifier,
) *routerConfigBuilder {
b.authHandler = authHandler
b.postHandler = postHandler
b.voteHandler = voteHandler
b.userHandler = userHandler
b.apiHandler = apiHandler
b.authService = authService
return b
}
func (b *routerConfigBuilder) withPageHandler(pageHandler *handlers.PageHandler) *routerConfigBuilder {
b.pageHandler = pageHandler
return b
}
func (b *routerConfigBuilder) withStaticDir(staticDir string) *routerConfigBuilder {
b.staticDir = staticDir
return b
}
func (b *routerConfigBuilder) withRateLimitConfig(rateLimitConfig config.RateLimitConfig) *routerConfigBuilder {
b.rateLimitConfig = rateLimitConfig
return b
}
func (b *routerConfigBuilder) build() server.RouterConfig {
return server.RouterConfig{
AuthHandler: b.authHandler,
PostHandler: b.postHandler,
VoteHandler: b.voteHandler,
UserHandler: b.userHandler,
APIHandler: b.apiHandler,
AuthService: b.authService,
PageHandler: b.pageHandler,
StaticDir: b.staticDir,
Debug: b.debug,
DisableCache: b.disableCache,
DisableCompression: b.disableCompression,
DBMonitor: b.dbMonitor,
RateLimitConfig: b.rateLimitConfig,
}
}
func buildRouterConfig(h *commonHandlers, staticDir string, pageHandler *handlers.PageHandler) server.RouterConfig {
return newRouterConfigBuilder().
withHandlers(h).
withPageHandler(pageHandler).
withStaticDir(staticDir).
build()
}
func setupTestContext(t *testing.T) *testContext { func setupTestContext(t *testing.T) *testContext {
t.Helper() t.Helper()
middleware.StopAllRateLimiters() middleware.StopAllRateLimiters()

View File

@@ -35,21 +35,11 @@ func setupRateLimitRouter(t *testing.T, rateLimitConfig config.RateLimitConfig)
staticDir := t.TempDir() staticDir := t.TempDir()
router := server.NewRouter(server.RouterConfig{ router := server.NewRouter(newRouterConfigBuilder().
AuthHandler: authHandler, withIndividualHandlers(authHandler, postHandler, voteHandler, userHandler, apiHandler, authService).
PostHandler: postHandler, withStaticDir(staticDir).
VoteHandler: voteHandler, withRateLimitConfig(rateLimitConfig).
UserHandler: userHandler, build())
APIHandler: apiHandler,
AuthService: authService,
PageHandler: nil,
StaticDir: staticDir,
Debug: false,
DisableCache: false,
DisableCompression: false,
DBMonitor: middleware.NewInMemoryDBMonitor(),
RateLimitConfig: rateLimitConfig,
})
return router, suite return router, suite
} }