49 lines
1.4 KiB
Go
49 lines
1.4 KiB
Go
package httpapi_test
|
|
|
|
import (
|
|
"net/http"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/httpapi"
|
|
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store"
|
|
)
|
|
|
|
func TestInstanceIDOnResponses(t *testing.T) {
|
|
var stopping atomic.Bool
|
|
handler, err := httpapi.NewPostgresHandler(&store.PostgresStore{}, &stopping, 3600)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var instanceID string
|
|
for _, tc := range []struct {
|
|
path string
|
|
stopping bool
|
|
status int
|
|
}{
|
|
{"/health/live", false, http.StatusOK},
|
|
{"/missing", false, http.StatusNotFound},
|
|
{"/api/v1/chats", false, http.StatusUnauthorized},
|
|
{"/health/ready", true, http.StatusServiceUnavailable},
|
|
{"/health/live", true, http.StatusOK},
|
|
} {
|
|
stopping.Store(tc.stopping)
|
|
response := request(handler, http.MethodGet, tc.path, "", "")
|
|
if response.Code != tc.status {
|
|
t.Fatalf("%s: status=%d, want %d", tc.path, response.Code, tc.status)
|
|
}
|
|
got := response.Header().Get("X-Instance-Id")
|
|
if got == "" {
|
|
t.Fatal("missing X-Instance-Id")
|
|
}
|
|
if instanceID != "" && got != instanceID {
|
|
t.Fatalf("instance changed: %q -> %q", instanceID, got)
|
|
}
|
|
instanceID = got
|
|
}
|
|
response := request(newAPI(), http.MethodGet, "/health/live", "", "")
|
|
if got := response.Header().Get("X-Instance-Id"); got != instanceID {
|
|
t.Fatalf("different handlers in one process: %q != %q", got, instanceID)
|
|
}
|
|
}
|