|
| 1 | +// Code scaffolded by goctl. Safe to edit. |
| 2 | +// goctl {{.version}} |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "{{.projectPkg}}/internal/config" |
| 13 | + "{{.projectPkg}}/internal/handler" |
| 14 | + "{{.projectPkg}}/internal/svc" |
| 15 | + |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | + "github.com/zeromicro/go-zero/rest" |
| 19 | +) |
| 20 | + |
| 21 | +func TestMain(m *testing.M) { |
| 22 | + // TODO: Add setup/teardown logic here if needed |
| 23 | + m.Run() |
| 24 | +} |
| 25 | + |
| 26 | +func TestServerIntegration(t *testing.T) { |
| 27 | + // Create test server |
| 28 | + c := config.Config{ |
| 29 | + RestConf: rest.RestConf{ |
| 30 | + Host: "127.0.0.1", |
| 31 | + Port: 0, // Use random available port |
| 32 | + }, |
| 33 | + } |
| 34 | + |
| 35 | + server := rest.MustNewServer(c.RestConf) |
| 36 | + defer server.Stop() |
| 37 | + |
| 38 | + ctx := svc.NewServiceContext(c) |
| 39 | + handler.RegisterHandlers(server, ctx) |
| 40 | + |
| 41 | + // Start server in background |
| 42 | + go func() { |
| 43 | + server.Start() |
| 44 | + }() |
| 45 | + |
| 46 | + // Wait for server to start |
| 47 | + time.Sleep(100 * time.Millisecond) |
| 48 | + |
| 49 | + tests := []struct { |
| 50 | + name string |
| 51 | + method string |
| 52 | + path string |
| 53 | + body string |
| 54 | + expectedStatus int |
| 55 | + setup func() |
| 56 | + }{ |
| 57 | + { |
| 58 | + name: "health check", |
| 59 | + method: "GET", |
| 60 | + path: "/health", |
| 61 | + expectedStatus: http.StatusNotFound, // Adjust based on actual routes |
| 62 | + setup: func() {}, |
| 63 | + }, |
| 64 | + {{if .hasRoutes}}{{range .routes}}{ |
| 65 | + name: "{{.Method}} {{.Path}}", |
| 66 | + method: "{{.Method}}", |
| 67 | + path: "{{.Path}}", |
| 68 | + expectedStatus: http.StatusOK, // TODO: Adjust expected status |
| 69 | + setup: func() { |
| 70 | + // TODO: Add setup logic for this endpoint |
| 71 | + }, |
| 72 | + }, |
| 73 | + {{end}}{{end}}{ |
| 74 | + name: "not found route", |
| 75 | + method: "GET", |
| 76 | + path: "/nonexistent", |
| 77 | + expectedStatus: http.StatusNotFound, |
| 78 | + setup: func() {}, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + for _, tt := range tests { |
| 83 | + t.Run(tt.name, func(t *testing.T) { |
| 84 | + tt.setup() |
| 85 | +
|
| 86 | + req, err := http.NewRequest(tt.method, tt.path, nil) |
| 87 | + require.NoError(t, err) |
| 88 | +
|
| 89 | + rr := httptest.NewRecorder() |
| 90 | + server.ServeHTTP(rr, req) |
| 91 | +
|
| 92 | + assert.Equal(t, tt.expectedStatus, rr.Code) |
| 93 | + |
| 94 | + // TODO: Add response body assertions |
| 95 | + t.Logf("Response: %s", rr.Body.String()) |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestServerLifecycle(t *testing.T) { |
| 101 | + c := config.Config{ |
| 102 | + RestConf: rest.RestConf{ |
| 103 | + Host: "127.0.0.1", |
| 104 | + Port: 0, |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + server := rest.MustNewServer(c.RestConf) |
| 109 | + |
| 110 | + // Test server can start and stop without errors |
| 111 | + ctx := svc.NewServiceContext(c) |
| 112 | + handler.RegisterHandlers(server, ctx) |
| 113 | + |
| 114 | + // In a real integration test, you might start the server in a goroutine |
| 115 | + // and test actual HTTP requests, but for scaffolding we keep it simple |
| 116 | + server.Stop() |
| 117 | + |
| 118 | + // TODO: Add more lifecycle tests as needed |
| 119 | + assert.True(t, true, "Server lifecycle test passed") |
| 120 | +} |
0 commit comments