2026-07-08 00:05:22 +08:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"chis_osi/osi"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestHealthRecordFindReturnsRawPlatformResponse(t *testing.T) {
|
|
|
|
|
osiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
_, _ = w.Write([]byte(`{"code":"01","message":"操作成功","data":[{"healthRecord":{"phrId":"phr-001","adressNumber":"下围村1号"}}]}`))
|
|
|
|
|
}))
|
|
|
|
|
defer 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: "dyytgw", Ask: "secret", Transport: transport})
|
|
|
|
|
h := NewHealthRecordHandler(client)
|
|
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/health-record/find?idCard=440100199001011234", nil)
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
h.Find(rec, req)
|
|
|
|
|
|
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
|
t.Fatalf("status = %d", rec.Code)
|
|
|
|
|
}
|
|
|
|
|
body, _ := io.ReadAll(rec.Body)
|
|
|
|
|
// 完整回写:未建模字段 adressNumber 也应原样出现
|
|
|
|
|
if !strings.Contains(string(body), `"phr-001"`) || !strings.Contains(string(body), "adressNumber") {
|
|
|
|
|
t.Fatalf("body missing full archive content: %s", body)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-09 20:38:27 +08:00
|
|
|
func TestHealthRecordCrowdReturnsRawPlatformResponse(t *testing.T) {
|
|
|
|
|
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(`{"code":"01","message":"操作成功","data":{"personSign":"LAO","idCard":"440100199001011234","phrId":"PHR001"}}`))
|
|
|
|
|
}))
|
|
|
|
|
defer 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: "dyytgw", Ask: "secret", Transport: transport})
|
|
|
|
|
h := NewHealthRecordHandler(client)
|
|
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/health-record/crowd?idCard=440100199001011234", nil)
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
h.Crowd(rec, req)
|
|
|
|
|
|
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
|
t.Fatalf("status = %d", rec.Code)
|
|
|
|
|
}
|
|
|
|
|
if seenPath != "/osi/api/auto/jkda/findrqbj" {
|
|
|
|
|
t.Fatalf("upstream path = %q", seenPath)
|
|
|
|
|
}
|
|
|
|
|
body, _ := io.ReadAll(rec.Body)
|
|
|
|
|
if !strings.Contains(string(body), `"personSign":"LAO"`) || !strings.Contains(string(body), `"phrId":"PHR001"`) {
|
|
|
|
|
t.Fatalf("body missing crowd fields: %s", body)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-08 00:05:22 +08:00
|
|
|
func TestHealthRecordFindRequiresIdentifier(t *testing.T) {
|
|
|
|
|
h := NewHealthRecordHandler(nil) // 无标识符时在触达 client 前就返回 400
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/health-record/find", nil)
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
h.Find(rec, req)
|
|
|
|
|
|
|
|
|
|
if rec.Code != http.StatusBadRequest {
|
|
|
|
|
t.Fatalf("status = %d, want 400", rec.Code)
|
|
|
|
|
}
|
|
|
|
|
}
|