fix(goctl): use rest.Serverless for generated integration tests (#5258)

This commit is contained in:
Kevin Wan
2025-10-25 23:14:54 +08:00
committed by GitHub
parent 4e52d77ad8
commit 98423ca948

View File

@@ -31,20 +31,16 @@ func TestServerIntegration(t *testing.T) {
Port: 0, // Use random available port Port: 0, // Use random available port
}, },
} }
server := rest.MustNewServer(c.RestConf) server := rest.MustNewServer(c.RestConf)
defer server.Stop() defer server.Stop()
ctx := svc.NewServiceContext(c) ctx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, ctx) handler.RegisterHandlers(server, ctx)
// Start server in background // Create serverless wrapper for testing
go func() { serverless, err := rest.NewServerless(server)
server.Start() require.NoError(t, err)
}()
// Wait for server to start
time.Sleep(100 * time.Millisecond)
tests := []struct { tests := []struct {
name string name string
@@ -56,7 +52,7 @@ func TestServerIntegration(t *testing.T) {
}{ }{
{ {
name: "health check", name: "health check",
method: "GET", method: http.MethodGet,
path: "/health", path: "/health",
expectedStatus: http.StatusNotFound, // Adjust based on actual routes expectedStatus: http.StatusNotFound, // Adjust based on actual routes
setup: func() {}, setup: func() {},
@@ -72,7 +68,7 @@ func TestServerIntegration(t *testing.T) {
}, },
{{end}}{{end}}{ {{end}}{{end}}{
name: "not found route", name: "not found route",
method: "GET", method: http.MethodGet,
path: "/nonexistent", path: "/nonexistent",
expectedStatus: http.StatusNotFound, expectedStatus: http.StatusNotFound,
setup: func() {}, setup: func() {},
@@ -87,10 +83,10 @@ func TestServerIntegration(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
server.ServeHTTP(rr, req) serverless.Serve(rr, req)
assert.Equal(t, tt.expectedStatus, rr.Code) assert.Equal(t, tt.expectedStatus, rr.Code)
// TODO: Add response body assertions // TODO: Add response body assertions
t.Logf("Response: %s", rr.Body.String()) t.Logf("Response: %s", rr.Body.String())
}) })
@@ -100,13 +96,13 @@ func TestServerIntegration(t *testing.T) {
func TestServerLifecycle(t *testing.T) { func TestServerLifecycle(t *testing.T) {
c := config.Config{ c := config.Config{
RestConf: rest.RestConf{ RestConf: rest.RestConf{
Host: "127.0.0.1", Host: "127.0.0.1",
Port: 0, Port: 0,
}, },
} }
server := rest.MustNewServer(c.RestConf) server := rest.MustNewServer(c.RestConf)
// Test server can start and stop without errors // Test server can start and stop without errors
ctx := svc.NewServiceContext(c) ctx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, ctx) handler.RegisterHandlers(server, ctx)