refactored hws to improve database operability
This commit is contained in:
@@ -18,7 +18,7 @@ func Test_Server_Addr(t *testing.T) {
|
||||
Port: 8080,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
|
||||
addr := server.Addr()
|
||||
assert.Equal(t, "192.168.1.1:8080", addr)
|
||||
}
|
||||
@@ -26,7 +26,7 @@ func Test_Server_Addr(t *testing.T) {
|
||||
func Test_Server_Handler(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
server := createTestServer(t, &buf)
|
||||
|
||||
|
||||
// Add routes first
|
||||
handler := testHandler
|
||||
err := server.AddRoutes(hws.Route{
|
||||
@@ -35,16 +35,16 @@ func Test_Server_Handler(t *testing.T) {
|
||||
Handler: handler,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
|
||||
// Get the handler
|
||||
h := server.Handler()
|
||||
require.NotNil(t, h)
|
||||
|
||||
|
||||
// Test the handler directly with httptest
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
h.ServeHTTP(rr, req)
|
||||
|
||||
|
||||
assert.Equal(t, 200, rr.Code)
|
||||
assert.Equal(t, "hello world", rr.Body.String())
|
||||
}
|
||||
@@ -52,7 +52,7 @@ func Test_Server_Handler(t *testing.T) {
|
||||
func Test_LoggerIgnorePaths_Integration(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
server := createTestServer(t, &buf)
|
||||
|
||||
|
||||
// Add routes
|
||||
err := server.AddRoutes(hws.Route{
|
||||
Path: "/test",
|
||||
@@ -64,28 +64,28 @@ func Test_LoggerIgnorePaths_Integration(t *testing.T) {
|
||||
Handler: testHandler,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
|
||||
// Set paths to ignore
|
||||
server.LoggerIgnorePaths("/ignore", "/healthz")
|
||||
|
||||
|
||||
err = server.AddMiddleware()
|
||||
require.NoError(t, err)
|
||||
|
||||
|
||||
// Test that ignored path doesn't generate logs
|
||||
buf.Reset()
|
||||
req := httptest.NewRequest("GET", "/ignore", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(rr, req)
|
||||
|
||||
|
||||
// Buffer should be empty for ignored path
|
||||
assert.Empty(t, buf.String())
|
||||
|
||||
|
||||
// Test that non-ignored path generates logs
|
||||
buf.Reset()
|
||||
req = httptest.NewRequest("GET", "/test", nil)
|
||||
rr = httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(rr, req)
|
||||
|
||||
|
||||
// Buffer should have logs for non-ignored path
|
||||
assert.NotEmpty(t, buf.String())
|
||||
}
|
||||
@@ -93,12 +93,12 @@ func Test_LoggerIgnorePaths_Integration(t *testing.T) {
|
||||
func Test_WrappedWriter(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
server := createTestServer(t, &buf)
|
||||
|
||||
|
||||
// Add routes with different status codes
|
||||
err := server.AddRoutes(
|
||||
hws.Route{
|
||||
Path: "/ok",
|
||||
Method: hws.MethodGET,
|
||||
Path: "/ok",
|
||||
Method: hws.MethodGET,
|
||||
Handler: testHandler,
|
||||
},
|
||||
hws.Route{
|
||||
@@ -111,16 +111,16 @@ func Test_WrappedWriter(t *testing.T) {
|
||||
},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
|
||||
err = server.AddMiddleware()
|
||||
require.NoError(t, err)
|
||||
|
||||
|
||||
// Test OK status
|
||||
req := httptest.NewRequest("GET", "/ok", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(rr, req)
|
||||
assert.Equal(t, 200, rr.Code)
|
||||
|
||||
|
||||
// Test Created status
|
||||
req = httptest.NewRequest("POST", "/created", nil)
|
||||
rr = httptest.NewRecorder()
|
||||
@@ -149,7 +149,7 @@ func Test_Start_Errors(t *testing.T) {
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
err = server.Start(nil)
|
||||
err = server.Start(t.Context())
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "Context cannot be nil")
|
||||
})
|
||||
@@ -163,10 +163,10 @@ func Test_Shutdown_Errors(t *testing.T) {
|
||||
startTestServer(t, server)
|
||||
<-server.Ready()
|
||||
|
||||
err := server.Shutdown(nil)
|
||||
err := server.Shutdown(t.Context())
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "Context cannot be nil")
|
||||
|
||||
|
||||
// Clean up
|
||||
server.Shutdown(t.Context())
|
||||
})
|
||||
@@ -199,7 +199,7 @@ func Test_WaitUntilReady_ContextCancelled(t *testing.T) {
|
||||
|
||||
// Start should return with context error since timeout is so short
|
||||
err = server.Start(ctx)
|
||||
|
||||
|
||||
// The error could be nil if server started very quickly, or context.DeadlineExceeded
|
||||
// This tests the ctx.Err() path in waitUntilReady
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user