oto/services/core/internal/httpserver/server_test.go
toki 8ecb101a17 feat(core): OTO Core 초기 서버를 추가한다
독립 Control Plane 마이그레이션에서 core-service 기반을 검증할 수 있도록 health/readiness 엔드포인트와 로컬 실행 진입점을 먼저 마련한다.
2026-06-05 08:24:45 +09:00

90 lines
2.6 KiB
Go

package httpserver
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestHandleHealthz(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/healthz", nil)
rr := httptest.NewRecorder()
handleHealthz(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handleHealthz returned wrong status code: got %v want %v", status, http.StatusOK)
}
expected := "OK"
if body := rr.Body.String(); body != expected {
t.Errorf("handleHealthz returned unexpected body: got %v want %v", body, expected)
}
}
func TestHandleHealthz_MethodNotAllowed(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/healthz", nil)
rr := httptest.NewRecorder()
handleHealthz(rr, req)
if status := rr.Code; status != http.StatusMethodNotAllowed {
t.Errorf("handleHealthz for POST returned wrong status code: got %v want %v", status, http.StatusMethodNotAllowed)
}
}
func TestHandleReadyz(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/readyz", nil)
rr := httptest.NewRecorder()
handleReadyz(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handleReadyz returned wrong status code: got %v want %v", status, http.StatusOK)
}
expected := "OK"
if body := rr.Body.String(); body != expected {
t.Errorf("handleReadyz returned unexpected body: got %v want %v", body, expected)
}
}
func TestHandleReadyz_MethodNotAllowed(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/readyz", nil)
rr := httptest.NewRecorder()
handleReadyz(rr, req)
if status := rr.Code; status != http.StatusMethodNotAllowed {
t.Errorf("handleReadyz for POST returned wrong status code: got %v want %v", status, http.StatusMethodNotAllowed)
}
}
func TestServerMux(t *testing.T) {
server := NewServer(":0")
mux := server.httpServer.Handler.(*http.ServeMux)
// Test healthz routing
reqHealth := httptest.NewRequest(http.MethodGet, "/healthz", nil)
rrHealth := httptest.NewRecorder()
mux.ServeHTTP(rrHealth, reqHealth)
if rrHealth.Code != http.StatusOK {
t.Errorf("mux /healthz failed: got status %v", rrHealth.Code)
}
// Test readyz routing
reqReady := httptest.NewRequest(http.MethodGet, "/readyz", nil)
rrReady := httptest.NewRecorder()
mux.ServeHTTP(rrReady, reqReady)
if rrReady.Code != http.StatusOK {
t.Errorf("mux /readyz failed: got status %v", rrReady.Code)
}
// Test fallback/not found routing
reqNotFound := httptest.NewRequest(http.MethodGet, "/unknown", nil)
rrNotFound := httptest.NewRecorder()
mux.ServeHTTP(rrNotFound, reqNotFound)
if rrNotFound.Code != http.StatusNotFound {
t.Errorf("mux /unknown routing status code: got %v want %v", rrNotFound.Code, http.StatusNotFound)
}
}