120 lines
2.9 KiB
Go
120 lines
2.9 KiB
Go
//go:build integration
|
|
|
|
package tests
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
"go-grpc-gateway-template/internal/config"
|
|
"go-grpc-gateway-template/internal/health"
|
|
"go-grpc-gateway-template/internal/server"
|
|
helloworldpb "go-grpc-gateway-template/proto/helloworld"
|
|
)
|
|
|
|
func TestIntegration(t *testing.T) {
|
|
// Create test configuration
|
|
cfg := &config.Config{
|
|
Server: config.ServerConfig{
|
|
GRPCPort: "18080",
|
|
HTTPPort: "18090",
|
|
},
|
|
Log: config.LogConfig{
|
|
Level: "info",
|
|
Format: "text",
|
|
},
|
|
Metrics: config.MetricsConfig{
|
|
Enabled: false,
|
|
},
|
|
}
|
|
|
|
// Start server
|
|
srv := server.New(cfg)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
go func() {
|
|
if err := srv.Start(ctx); err != nil {
|
|
t.Errorf("Server start error: %v", err)
|
|
}
|
|
}()
|
|
|
|
// Wait for server to start
|
|
time.Sleep(2 * time.Second)
|
|
defer func() {
|
|
cancel()
|
|
srv.Shutdown(context.Background())
|
|
}()
|
|
|
|
t.Run("Health Check", func(t *testing.T) {
|
|
resp, err := http.Get("http://localhost:18090/health")
|
|
if err != nil {
|
|
t.Fatalf("Health check failed: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Errorf("Expected status 200, got %d", resp.StatusCode)
|
|
}
|
|
|
|
var healthResp health.Response
|
|
if err := json.NewDecoder(resp.Body).Decode(&healthResp); err != nil {
|
|
t.Fatalf("Failed to decode health response: %v", err)
|
|
}
|
|
|
|
if healthResp.Status != "ok" {
|
|
t.Errorf("Expected status 'ok', got %s", healthResp.Status)
|
|
}
|
|
})
|
|
|
|
t.Run("gRPC API", func(t *testing.T) {
|
|
conn, err := grpc.Dial("localhost:18080", grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
if err != nil {
|
|
t.Fatalf("Failed to connect to gRPC server: %v", err)
|
|
}
|
|
defer conn.Close()
|
|
|
|
client := helloworldpb.NewGreeterClient(conn)
|
|
resp, err := client.SayHello(context.Background(), &helloworldpb.HelloRequest{
|
|
Name: "Integration Test",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("gRPC call failed: %v", err)
|
|
}
|
|
|
|
expectedMessage := "Hello Integration Test"
|
|
if resp.Message != expectedMessage {
|
|
t.Errorf("Expected message %s, got %s", expectedMessage, resp.Message)
|
|
}
|
|
})
|
|
|
|
t.Run("HTTP Gateway", func(t *testing.T) {
|
|
reqBody := strings.NewReader(`{"name": "HTTP Test"}`)
|
|
resp, err := http.Post("http://localhost:18090/v1/example/echo", "application/json", reqBody)
|
|
if err != nil {
|
|
t.Fatalf("HTTP request failed: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Errorf("Expected status 200, got %d", resp.StatusCode)
|
|
}
|
|
|
|
var result map[string]interface{}
|
|
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
|
t.Fatalf("Failed to decode response: %v", err)
|
|
}
|
|
|
|
expectedMessage := "Hello HTTP Test"
|
|
if result["message"] != expectedMessage {
|
|
t.Errorf("Expected message %s, got %v", expectedMessage, result["message"])
|
|
}
|
|
})
|
|
} |