- Add pkg/greet/api_v1.go with ApiV1Greet interface and handler - Implement Greeter interface for dependency injection - Return JSON responses with proper Content-Type headers - Move greet service instantiation to server package - Separate v1 route registration into registerApiV1Routes function - Health endpoint at /api level, greet endpoints at /api/v1/greet - Simplify server entrypoint by removing external dependencies
25 lines
516 B
Go
25 lines
516 B
Go
package greet
|
|
|
|
import "testing"
|
|
|
|
func TestService_Greet(t *testing.T) {
|
|
service := NewService()
|
|
tests := []struct {
|
|
name string
|
|
expected string
|
|
}{
|
|
{"", "Hello world!"},
|
|
{"John", "Hello John!"},
|
|
{"Alice", "Hello Alice!"},
|
|
{" ", "Hello !"}, // spaces are not considered empty
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := service.Greet(tt.name)
|
|
if result != tt.expected {
|
|
t.Errorf("Greet(%q) = %q, want %q", tt.name, result, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
} |