52 lines
1.6 KiB
Go
52 lines
1.6 KiB
Go
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)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
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)
|
||
|
|
}
|
||
|
|
}
|