feat(handler): 体检 HTTP 查询端点(T-302)
- GET /api/health-check/last(最近一次)+ /api/health-check/list(年度已检未检名单) - 回写平台完整响应 Result.Raw,抽 writeRawOrError 共用 - last 按 idCard/phrid/empiId;list 按 checkYear(必填)+idCard+checkType - 单测:路径/原样回写/缺参 400(脱敏假 OSI 后端);默认仅绑本机 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"chis_osi/osi"
|
||||
)
|
||||
|
||||
func newTestClient(t *testing.T, body string) (*osi.Client, *string) {
|
||||
t.Helper()
|
||||
var seenPath string
|
||||
osiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
seenPath = r.URL.Path
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(body))
|
||||
}))
|
||||
t.Cleanup(osiServer.Close)
|
||||
|
||||
transport, err := osi.NewTransport(osi.TransportConfig{Timeout: time.Second})
|
||||
if err != nil {
|
||||
t.Fatalf("NewTransport: %v", err)
|
||||
}
|
||||
client := osi.NewClient(osi.ClientConfig{BaseURL: osiServer.URL, UserName: "u", Ask: "k", Transport: transport})
|
||||
return client, &seenPath
|
||||
}
|
||||
|
||||
func TestHealthCheckLastReturnsRawResponse(t *testing.T) {
|
||||
client, seenPath := newTestClient(t, `{"code":"01","message":"操作成功","data":{"checkId":"CHK-1","idcard":"TEST-1"}}`)
|
||||
h := NewHealthCheckHandler(client)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/health-check/last?idCard=TEST-1", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
h.Last(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d", rec.Code)
|
||||
}
|
||||
if *seenPath != "/osi/api/auto/jktjlscx/query" {
|
||||
t.Fatalf("osi path = %q", *seenPath)
|
||||
}
|
||||
body, _ := io.ReadAll(rec.Body)
|
||||
if !strings.Contains(string(body), `"CHK-1"`) {
|
||||
t.Fatalf("body missing content: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthCheckLastRequiresIdentifier(t *testing.T) {
|
||||
h := NewHealthCheckHandler(nil) // 无标识符在触达 client 前返回 400
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/health-check/last", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
h.Last(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want 400", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthCheckListRequiresCheckYear(t *testing.T) {
|
||||
h := NewHealthCheckHandler(nil) // 缺 checkYear 在触达 client 前返回 400
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/health-check/list?idCard=TEST-1", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
h.List(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want 400", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthCheckListReturnsRosterRaw(t *testing.T) {
|
||||
client, seenPath := newTestClient(t, `{"code":"01","message":"操作成功","data":[{"idCard":"TEST-1","checkType":"0"}]}`)
|
||||
h := NewHealthCheckHandler(client)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/health-check/list?checkYear=2025&idCard=TEST-1", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
h.List(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d", rec.Code)
|
||||
}
|
||||
if *seenPath != "/osi/api/auto/jktjlist/query" {
|
||||
t.Fatalf("osi path = %q", *seenPath)
|
||||
}
|
||||
body, _ := io.ReadAll(rec.Body)
|
||||
if !strings.Contains(string(body), `"checkType"`) {
|
||||
t.Fatalf("body missing roster: %s", body)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user