package handler import ( "io" "net/http" "net/http/httptest" "strings" "testing" ) func TestPublicGridAddressesReturnsRawResponse(t *testing.T) { client, seenPath := newTestClient(t, `{"code":"01","message":"操作成功","data":[{"regionCode":"441625000000","regionName":"测试网格"}]}`) h := NewPublicHandler(client) req := httptest.NewRequest(http.MethodGet, "/api/dictionaries/grid-addresses?parentCode=441625&pageNo=1&operateUser=712041", nil) rec := httptest.NewRecorder() h.GridAddresses(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d", rec.Code) } if *seenPath != "/osi/api/auto/wgdzcx/query" { t.Fatalf("osi path = %q", *seenPath) } body, _ := io.ReadAll(rec.Body) if !strings.Contains(string(body), `"regionCode"`) { t.Fatalf("body missing grid address data: %s", body) } } func TestPublicDoctorsReturnsRawResponse(t *testing.T) { client, seenPath := newTestClient(t, `{"code":"01","message":"操作成功","data":[{"personId":"doc-001","personName":"测试医生"}]}`) h := NewPublicHandler(client) req := httptest.NewRequest(http.MethodGet, "/api/dictionaries/doctors?manaUnitId=441625001&operateUser=712041", nil) rec := httptest.NewRecorder() h.Doctors(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d", rec.Code) } if *seenPath != "/osi/api/auto/zryscx/query" { t.Fatalf("osi path = %q", *seenPath) } body, _ := io.ReadAll(rec.Body) if !strings.Contains(string(body), `"personId"`) { t.Fatalf("body missing doctor data: %s", body) } } func TestPublicDrugsReturnsRawResponse(t *testing.T) { client, seenPath := newTestClient(t, `{"code":"01","message":"操作成功","data":[{"ypmc":"测试药品","ypdw":"盒"}]}`) h := NewPublicHandler(client) req := httptest.NewRequest(http.MethodGet, "/api/dictionaries/drugs?ypmc=测试&pageNo=1&pym=CS", nil) rec := httptest.NewRecorder() h.Drugs(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d", rec.Code) } if *seenPath != "/osi/api/auto/ypmlcx/query" { t.Fatalf("osi path = %q", *seenPath) } body, _ := io.ReadAll(rec.Body) if !strings.Contains(string(body), `"ypmc"`) { t.Fatalf("body missing drug data: %s", body) } } func TestPublicOrgsReturnsRawResponse(t *testing.T) { client, seenPath := newTestClient(t, `{"code":"01","message":"操作成功","data":[{"organizCode":"441625001","organizName":"测试机构"}]}`) h := NewPublicHandler(client) req := httptest.NewRequest(http.MethodGet, "/api/dictionaries/orgs?organizCode=441625001&parentId=441625", nil) rec := httptest.NewRecorder() h.Orgs(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d", rec.Code) } if *seenPath != "/osi/api/auto/cxjg/query" { t.Fatalf("osi path = %q", *seenPath) } body, _ := io.ReadAll(rec.Body) if !strings.Contains(string(body), `"organizCode"`) { t.Fatalf("body missing org data: %s", body) } } func TestPublicDrugsRequiresPageNo(t *testing.T) { h := NewPublicHandler(nil) // 缺 pageNo 在触达 client 前返回 400(药品目录分页必填) req := httptest.NewRequest(http.MethodGet, "/api/dictionaries/drugs?ypmc=测试", nil) rec := httptest.NewRecorder() h.Drugs(rec, req) if rec.Code != http.StatusBadRequest { t.Fatalf("status = %d, want 400 (pageNo required)", rec.Code) } } func TestPublicDictionaryRejectsInvalidPageNo(t *testing.T) { h := NewPublicHandler(nil) req := httptest.NewRequest(http.MethodGet, "/api/dictionaries/drugs?pageNo=bad", nil) rec := httptest.NewRecorder() h.Drugs(rec, req) if rec.Code != http.StatusBadRequest { t.Fatalf("status = %d, want 400", rec.Code) } }