附近小店
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ocr_test.go 21 KiB

há 1 mês
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. package weapp
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "net/http/httptest"
  7. "os"
  8. "path"
  9. "testing"
  10. )
  11. func TestBankCardByURL(t *testing.T) {
  12. server := http.NewServeMux()
  13. server.HandleFunc(apiBankcard, func(w http.ResponseWriter, r *http.Request) {
  14. if r.Method != "POST" {
  15. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  16. }
  17. path := r.URL.EscapedPath()
  18. if path != apiBankcard {
  19. t.Fatalf("Except to path '%s',get '%s'", apiBankcard, path)
  20. }
  21. if err := r.ParseForm(); err != nil {
  22. t.Fatal(err)
  23. }
  24. queries := []string{"type", "access_token", "img_url"}
  25. for _, v := range queries {
  26. content := r.Form.Get(v)
  27. if content == "" {
  28. t.Fatalf("Params [%s] can not be empty", v)
  29. }
  30. }
  31. w.WriteHeader(http.StatusOK)
  32. w.Header().Set("Content-Type", "application/json")
  33. raw := `{
  34. "errcode": 0,
  35. "errmsg": "ok",
  36. "id": "622213XXXXXXXXX"
  37. }`
  38. if _, err := w.Write([]byte(raw)); err != nil {
  39. t.Fatal(err)
  40. }
  41. })
  42. server.HandleFunc("/mediaurl", func(w http.ResponseWriter, r *http.Request) {
  43. filename := testIMGName
  44. file, err := os.Open(filename)
  45. if err != nil {
  46. t.Fatal((err))
  47. }
  48. defer file.Close()
  49. ext := path.Ext(filename)
  50. ext = ext[1:len(ext)]
  51. w.Header().Set("Content-Type", "image/"+ext)
  52. w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", path.Base(filename)))
  53. w.WriteHeader(http.StatusOK)
  54. if _, err := io.Copy(w, file); err != nil {
  55. t.Fatal(err)
  56. }
  57. })
  58. ts := httptest.NewServer(server)
  59. defer ts.Close()
  60. _, err := bankCardByURL(ts.URL+apiBankcard, "mock-access-token", ts.URL+"/mediaurl", RecognizeModePhoto)
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. _, err = bankCardByURL(ts.URL+apiBankcard, "mock-access-token", ts.URL+"/mediaurl", RecognizeModeScan)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. }
  69. func TestBankCard(t *testing.T) {
  70. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  71. if r.Method != "POST" {
  72. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  73. }
  74. path := r.URL.EscapedPath()
  75. if path != apiBankcard {
  76. t.Fatalf("Except to path '%s',get '%s'", apiBankcard, path)
  77. }
  78. if err := r.ParseForm(); err != nil {
  79. t.Fatal(err)
  80. }
  81. queries := []string{"type", "access_token"}
  82. for _, v := range queries {
  83. content := r.Form.Get(v)
  84. if content == "" {
  85. t.Fatalf("Params [%s] can not be empty", v)
  86. }
  87. }
  88. if _, _, err := r.FormFile("img"); err != nil {
  89. t.Fatal(err)
  90. }
  91. w.WriteHeader(http.StatusOK)
  92. w.Header().Set("Content-Type", "application/json")
  93. raw := `{
  94. "errcode": 0,
  95. "errmsg": "ok",
  96. "id": "622213XXXXXXXXX"
  97. }`
  98. if _, err := w.Write([]byte(raw)); err != nil {
  99. t.Fatal(err)
  100. }
  101. }))
  102. defer ts.Close()
  103. _, err := bankCard(ts.URL+apiBankcard, "mock-access-token", testIMGName, RecognizeModePhoto)
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. _, err = bankCard(ts.URL+apiBankcard, "mock-access-token", testIMGName, RecognizeModeScan)
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. }
  112. func TestDriverLicenseByURL(t *testing.T) {
  113. server := http.NewServeMux()
  114. server.HandleFunc(apiDrivingLicense, func(w http.ResponseWriter, r *http.Request) {
  115. if r.Method != "POST" {
  116. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  117. }
  118. path := r.URL.EscapedPath()
  119. if path != apiDrivingLicense {
  120. t.Fatalf("Except to path '%s',get '%s'", apiDrivingLicense, path)
  121. }
  122. if err := r.ParseForm(); err != nil {
  123. t.Fatal(err)
  124. }
  125. queries := []string{"access_token", "img_url"}
  126. for _, v := range queries {
  127. content := r.Form.Get(v)
  128. if content == "" {
  129. t.Fatalf("Params [%s] can not be empty", v)
  130. }
  131. }
  132. w.WriteHeader(http.StatusOK)
  133. w.Header().Set("Content-Type", "application/json")
  134. raw := `{
  135. "errcode": 0,
  136. "errmsg": "ok",
  137. "id_num": "660601xxxxxxxx1234",
  138. "name": "张三",
  139. "sex": "男",
  140. "nationality": "中国",
  141. "address": "广东省东莞市xxxxx号",
  142. "birth_date": "1990-12-21",
  143. "issue_date": "2012-12-21",
  144. "car_class": "C1",
  145. "valid_from": "2018-07-06",
  146. "valid_to": "2020-07-01",
  147. "official_seal": "xx市公安局公安交通管理局"
  148. }`
  149. if _, err := w.Write([]byte(raw)); err != nil {
  150. t.Fatal(err)
  151. }
  152. })
  153. server.HandleFunc("/mediaurl", func(w http.ResponseWriter, r *http.Request) {
  154. filename := testIMGName
  155. file, err := os.Open(filename)
  156. if err != nil {
  157. t.Fatal((err))
  158. }
  159. defer file.Close()
  160. ext := path.Ext(filename)
  161. ext = ext[1:len(ext)]
  162. w.Header().Set("Content-Type", "image/"+ext)
  163. w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", path.Base(filename)))
  164. w.WriteHeader(http.StatusOK)
  165. if _, err := io.Copy(w, file); err != nil {
  166. t.Fatal(err)
  167. }
  168. })
  169. ts := httptest.NewServer(server)
  170. defer ts.Close()
  171. _, err := driverLicenseByURL(ts.URL+apiDrivingLicense, "mock-access-token", ts.URL+"/mediaurl")
  172. if err != nil {
  173. t.Fatal(err)
  174. }
  175. }
  176. func TestDriverLicense(t *testing.T) {
  177. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  178. if r.Method != "POST" {
  179. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  180. }
  181. path := r.URL.EscapedPath()
  182. if path != apiDrivingLicense {
  183. t.Fatalf("Except to path '%s',get '%s'", apiDrivingLicense, path)
  184. }
  185. if err := r.ParseForm(); err != nil {
  186. t.Fatal(err)
  187. }
  188. if r.Form.Get("access_token") == "" {
  189. t.Fatalf("access_token can not be empty")
  190. }
  191. if _, _, err := r.FormFile("img"); err != nil {
  192. t.Fatal(err)
  193. }
  194. w.WriteHeader(http.StatusOK)
  195. w.Header().Set("Content-Type", "application/json")
  196. raw := `{
  197. "errcode": 0,
  198. "errmsg": "ok",
  199. "id_num": "660601xxxxxxxx1234",
  200. "name": "张三",
  201. "sex": "男",
  202. "nationality": "中国",
  203. "address": "广东省东莞市xxxxx号",
  204. "birth_date": "1990-12-21",
  205. "issue_date": "2012-12-21",
  206. "car_class": "C1",
  207. "valid_from": "2018-07-06",
  208. "valid_to": "2020-07-01",
  209. "official_seal": "xx市公安局公安交通管理局"
  210. }`
  211. if _, err := w.Write([]byte(raw)); err != nil {
  212. t.Fatal(err)
  213. }
  214. }))
  215. defer ts.Close()
  216. _, err := driverLicense(ts.URL+apiDrivingLicense, "mock-access-token", testIMGName)
  217. if err != nil {
  218. t.Fatal(err)
  219. }
  220. }
  221. func TestBusinessLicenseByURL(t *testing.T) {
  222. server := http.NewServeMux()
  223. server.HandleFunc("/cv/ocr/bizlicense", func(w http.ResponseWriter, r *http.Request) {
  224. if r.Method != "POST" {
  225. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  226. }
  227. if r.URL.EscapedPath() != "/cv/ocr/bizlicense" {
  228. t.Error("Invalid request path")
  229. }
  230. if err := r.ParseForm(); err != nil {
  231. t.Fatal(err)
  232. }
  233. queries := []string{"access_token", "img_url"}
  234. for _, v := range queries {
  235. content := r.Form.Get(v)
  236. if content == "" {
  237. t.Fatalf("Params [%s] can not be empty", v)
  238. }
  239. }
  240. w.WriteHeader(http.StatusOK)
  241. w.Header().Set("Content-Type", "application/json")
  242. raw := `{
  243. "errcode": 0,
  244. "errmsg": "ok",
  245. "reg_num": "123123",
  246. "serial": "123123",
  247. "legal_representative": "张三",
  248. "enterprise_name": "XX饮食店",
  249. "type_of_organization": "个人经营",
  250. "address": "XX市XX区XX路XX号",
  251. "type_of_enterprise": "xxx",
  252. "business_scope": "中型餐馆(不含凉菜、不含裱花蛋糕,不含生食海产品)。",
  253. "registered_capital": "200万",
  254. "paid_in_capital": "200万",
  255. "valid_period": "2019年1月1日",
  256. "registered_date": "2018年1月1日",
  257. "cert_position": {
  258. "pos": {
  259. "left_top": {
  260. "x": 155,
  261. "y": 191
  262. },
  263. "right_top": {
  264. "x": 725,
  265. "y": 157
  266. },
  267. "right_bottom": {
  268. "x": 743,
  269. "y": 512
  270. },
  271. "left_bottom": {
  272. "x": 164,
  273. "y": 525
  274. }
  275. }
  276. },
  277. "img_size": {
  278. "w": 966,
  279. "h": 728
  280. }
  281. }`
  282. if _, err := w.Write([]byte(raw)); err != nil {
  283. t.Fatal(err)
  284. }
  285. })
  286. server.HandleFunc("/mediaurl", func(w http.ResponseWriter, r *http.Request) {
  287. filename := testIMGName
  288. file, err := os.Open(filename)
  289. if err != nil {
  290. t.Fatal((err))
  291. }
  292. defer file.Close()
  293. ext := path.Ext(filename)
  294. ext = ext[1:len(ext)]
  295. w.Header().Set("Content-Type", "image/"+ext)
  296. w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", path.Base(filename)))
  297. w.WriteHeader(http.StatusOK)
  298. if _, err := io.Copy(w, file); err != nil {
  299. t.Fatal(err)
  300. }
  301. })
  302. ts := httptest.NewServer(server)
  303. defer ts.Close()
  304. _, err := businessLicenseByURL(ts.URL+apiBusinessLicense, "mock-access-token", ts.URL+"/mediaurl")
  305. if err != nil {
  306. t.Fatal(err)
  307. }
  308. }
  309. func TestBusinessLicense(t *testing.T) {
  310. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  311. if r.Method != "POST" {
  312. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  313. }
  314. if r.URL.EscapedPath() != "/cv/ocr/bizlicense" {
  315. t.Error("Invalid request path")
  316. }
  317. if err := r.ParseForm(); err != nil {
  318. t.Fatal(err)
  319. }
  320. if r.Form.Get("access_token") == "" {
  321. t.Fatalf("access_token can not be empty")
  322. }
  323. if _, _, err := r.FormFile("img"); err != nil {
  324. t.Fatal(err)
  325. }
  326. w.WriteHeader(http.StatusOK)
  327. w.Header().Set("Content-Type", "application/json")
  328. raw := `{
  329. "errcode": 0,
  330. "errmsg": "ok",
  331. "reg_num": "123123",
  332. "serial": "123123",
  333. "legal_representative": "张三",
  334. "enterprise_name": "XX饮食店",
  335. "type_of_organization": "个人经营",
  336. "address": "XX市XX区XX路XX号",
  337. "type_of_enterprise": "xxx",
  338. "business_scope": "中型餐馆(不含凉菜、不含裱花蛋糕,不含生食海产品)。",
  339. "registered_capital": "200万",
  340. "paid_in_capital": "200万",
  341. "valid_period": "2019年1月1日",
  342. "registered_date": "2018年1月1日",
  343. "cert_position": {
  344. "pos": {
  345. "left_top": {
  346. "x": 155,
  347. "y": 191
  348. },
  349. "right_top": {
  350. "x": 725,
  351. "y": 157
  352. },
  353. "right_bottom": {
  354. "x": 743,
  355. "y": 512
  356. },
  357. "left_bottom": {
  358. "x": 164,
  359. "y": 525
  360. }
  361. }
  362. },
  363. "img_size": {
  364. "w": 966,
  365. "h": 728
  366. }
  367. }`
  368. if _, err := w.Write([]byte(raw)); err != nil {
  369. t.Fatal(err)
  370. }
  371. }))
  372. defer ts.Close()
  373. _, err := businessLicense(ts.URL+apiBusinessLicense, "mock-access-token", testIMGName)
  374. if err != nil {
  375. t.Fatal(err)
  376. }
  377. }
  378. func TestPrintedTextByURL(t *testing.T) {
  379. server := http.NewServeMux()
  380. server.HandleFunc("/cv/ocr/comm", func(w http.ResponseWriter, r *http.Request) {
  381. if r.Method != "POST" {
  382. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  383. }
  384. if r.URL.EscapedPath() != "/cv/ocr/comm" {
  385. t.Error("Invalid request path")
  386. }
  387. if err := r.ParseForm(); err != nil {
  388. t.Fatal(err)
  389. }
  390. queries := []string{"access_token", "img_url"}
  391. for _, v := range queries {
  392. content := r.Form.Get(v)
  393. if content == "" {
  394. t.Fatalf("Params [%s] can not be empty", v)
  395. }
  396. }
  397. w.WriteHeader(http.StatusOK)
  398. w.Header().Set("Content-Type", "application/json")
  399. raw := `{
  400. "errcode": 0,
  401. "errmsg": "ok",
  402. "items": [
  403. {
  404. "text": "腾讯",
  405. "pos": {
  406. "left_top": {
  407. "x": 575,
  408. "y": 519
  409. },
  410. "right_top": {
  411. "x": 744,
  412. "y": 519
  413. },
  414. "right_bottom": {
  415. "x": 744,
  416. "y": 532
  417. },
  418. "left_bottom": {
  419. "x": 573,
  420. "y": 532
  421. }
  422. }
  423. },
  424. {
  425. "text": "微信团队",
  426. "pos": {
  427. "left_top": {
  428. "x": 670,
  429. "y": 516
  430. },
  431. "right_top": {
  432. "x": 762,
  433. "y": 517
  434. },
  435. "right_bottom": {
  436. "x": 762,
  437. "y": 532
  438. },
  439. "left_bottom": {
  440. "x": 670,
  441. "y": 531
  442. }
  443. }
  444. }
  445. ],
  446. "img_size": {
  447. "w": 1280,
  448. "h": 720
  449. }
  450. }`
  451. if _, err := w.Write([]byte(raw)); err != nil {
  452. t.Fatal(err)
  453. }
  454. })
  455. server.HandleFunc("/mediaurl", func(w http.ResponseWriter, r *http.Request) {
  456. filename := testIMGName
  457. file, err := os.Open(filename)
  458. if err != nil {
  459. t.Fatal((err))
  460. }
  461. defer file.Close()
  462. ext := path.Ext(filename)
  463. ext = ext[1:len(ext)]
  464. w.Header().Set("Content-Type", "image/"+ext)
  465. w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", path.Base(filename)))
  466. w.WriteHeader(http.StatusOK)
  467. if _, err := io.Copy(w, file); err != nil {
  468. t.Fatal(err)
  469. }
  470. })
  471. ts := httptest.NewServer(server)
  472. defer ts.Close()
  473. _, err := printedTextByURL(ts.URL+apiPrintedText, "mock-access-token", ts.URL+"/mediaurl")
  474. if err != nil {
  475. t.Fatal(err)
  476. }
  477. }
  478. func TestPrintedText(t *testing.T) {
  479. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  480. if r.Method != "POST" {
  481. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  482. }
  483. if r.URL.EscapedPath() != "/cv/ocr/comm" {
  484. t.Error("Invalid request path")
  485. }
  486. if err := r.ParseForm(); err != nil {
  487. t.Fatal(err)
  488. }
  489. if r.Form.Get("access_token") == "" {
  490. t.Fatalf("access_token can not be empty")
  491. }
  492. if _, _, err := r.FormFile("img"); err != nil {
  493. t.Fatal(err)
  494. }
  495. w.WriteHeader(http.StatusOK)
  496. w.Header().Set("Content-Type", "application/json")
  497. raw := `{
  498. "errcode": 0,
  499. "errmsg": "ok",
  500. "items": [
  501. {
  502. "text": "腾讯",
  503. "pos": {
  504. "left_top": {
  505. "x": 575,
  506. "y": 519
  507. },
  508. "right_top": {
  509. "x": 744,
  510. "y": 519
  511. },
  512. "right_bottom": {
  513. "x": 744,
  514. "y": 532
  515. },
  516. "left_bottom": {
  517. "x": 573,
  518. "y": 532
  519. }
  520. }
  521. },
  522. {
  523. "text": "微信团队",
  524. "pos": {
  525. "left_top": {
  526. "x": 670,
  527. "y": 516
  528. },
  529. "right_top": {
  530. "x": 762,
  531. "y": 517
  532. },
  533. "right_bottom": {
  534. "x": 762,
  535. "y": 532
  536. },
  537. "left_bottom": {
  538. "x": 670,
  539. "y": 531
  540. }
  541. }
  542. }
  543. ],
  544. "img_size": {
  545. "w": 1280,
  546. "h": 720
  547. }
  548. }`
  549. if _, err := w.Write([]byte(raw)); err != nil {
  550. t.Fatal(err)
  551. }
  552. }))
  553. defer ts.Close()
  554. _, err := printedText(ts.URL+apiPrintedText, "mock-access-token", testIMGName)
  555. if err != nil {
  556. t.Fatal(err)
  557. }
  558. }
  559. func TestIDCardByURL(t *testing.T) {
  560. server := http.NewServeMux()
  561. server.HandleFunc(apiIDCard, func(w http.ResponseWriter, r *http.Request) {
  562. if r.Method != "POST" {
  563. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  564. }
  565. path := r.URL.EscapedPath()
  566. if path != apiIDCard {
  567. t.Fatalf("Except to path '%s',get '%s'", apiIDCard, path)
  568. }
  569. if err := r.ParseForm(); err != nil {
  570. t.Fatal(err)
  571. }
  572. queries := []string{"type", "access_token", "img_url"}
  573. for _, v := range queries {
  574. content := r.Form.Get(v)
  575. if content == "" {
  576. t.Fatalf("Params [%s] can not be empty", v)
  577. }
  578. }
  579. w.WriteHeader(http.StatusOK)
  580. w.Header().Set("Content-Type", "application/json")
  581. raw := `{
  582. "errcode": 0,
  583. "errmsg": "ok",
  584. "type": "Front",
  585. "id": "44XXXXXXXXXXXXXXX1"
  586. }`
  587. if _, err := w.Write([]byte(raw)); err != nil {
  588. t.Fatal(err)
  589. }
  590. })
  591. server.HandleFunc("/mediaurl", func(w http.ResponseWriter, r *http.Request) {
  592. filename := testIMGName
  593. file, err := os.Open(filename)
  594. if err != nil {
  595. t.Fatal((err))
  596. }
  597. defer file.Close()
  598. ext := path.Ext(filename)
  599. ext = ext[1:len(ext)]
  600. w.Header().Set("Content-Type", "image/"+ext)
  601. w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", path.Base(filename)))
  602. w.WriteHeader(http.StatusOK)
  603. if _, err := io.Copy(w, file); err != nil {
  604. t.Fatal(err)
  605. }
  606. })
  607. ts := httptest.NewServer(server)
  608. defer ts.Close()
  609. _, err := idCardByURL(ts.URL+apiIDCard, "mock-access-token", ts.URL+"/mediaurl", RecognizeModePhoto)
  610. if err != nil {
  611. t.Fatal(err)
  612. }
  613. _, err = idCardByURL(ts.URL+apiIDCard, "mock-access-token", ts.URL+"/mediaurl", RecognizeModeScan)
  614. if err != nil {
  615. t.Fatal(err)
  616. }
  617. }
  618. func TestIDCard(t *testing.T) {
  619. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  620. if r.Method != "POST" {
  621. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  622. }
  623. path := r.URL.EscapedPath()
  624. if path != apiIDCard {
  625. t.Fatalf("Except to path '%s',get '%s'", apiIDCard, path)
  626. }
  627. if err := r.ParseForm(); err != nil {
  628. t.Fatal(err)
  629. }
  630. queries := []string{"type", "access_token"}
  631. for _, v := range queries {
  632. content := r.Form.Get(v)
  633. if content == "" {
  634. t.Fatalf("Params [%s] can not be empty", v)
  635. }
  636. }
  637. if _, _, err := r.FormFile("img"); err != nil {
  638. t.Fatal(err)
  639. }
  640. w.WriteHeader(http.StatusOK)
  641. w.Header().Set("Content-Type", "application/json")
  642. raw := `{
  643. "errcode": 0,
  644. "errmsg": "ok",
  645. "type": "Front",
  646. "id": "44XXXXXXXXXXXXXXX1"
  647. }`
  648. if _, err := w.Write([]byte(raw)); err != nil {
  649. t.Fatal(err)
  650. }
  651. }))
  652. defer ts.Close()
  653. _, err := idCard(ts.URL+apiIDCard, "mock-access-token", testIMGName, RecognizeModePhoto)
  654. if err != nil {
  655. t.Fatal(err)
  656. }
  657. _, err = idCard(ts.URL+apiIDCard, "mock-access-token", testIMGName, RecognizeModeScan)
  658. if err != nil {
  659. t.Fatal(err)
  660. }
  661. }
  662. func TestVehicleLicenseByURL(t *testing.T) {
  663. server := http.NewServeMux()
  664. server.HandleFunc(apiVehicleLicense, func(w http.ResponseWriter, r *http.Request) {
  665. if r.Method != "POST" {
  666. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  667. }
  668. path := r.URL.EscapedPath()
  669. if path != apiVehicleLicense {
  670. t.Fatalf("Except to path '%s',get '%s'", apiVehicleLicense, path)
  671. }
  672. if err := r.ParseForm(); err != nil {
  673. t.Fatal(err)
  674. }
  675. queries := []string{"type", "access_token", "img_url"}
  676. for _, v := range queries {
  677. content := r.Form.Get(v)
  678. if content == "" {
  679. t.Fatalf("Params [%s] can not be empty", v)
  680. }
  681. }
  682. w.WriteHeader(http.StatusOK)
  683. w.Header().Set("Content-Type", "application/json")
  684. raw := `{
  685. "vhicle_type": "小型普通客⻋",
  686. "owner": "东莞市xxxxx机械厂",
  687. "addr": "广东省东莞市xxxxx号",
  688. "use_character": "非营运",
  689. "model": "江淮牌HFCxxxxxxx",
  690. "vin": "LJ166xxxxxxxx51",
  691. "engine_num": "J3xxxxx3",
  692. "register_date": "2018-07-06",
  693. "issue_date": "2018-07-01",
  694. "plate_num_b": "粤xxxxx",
  695. "record": "441xxxxxx3",
  696. "passengers_num": "7人",
  697. "total_quality": "2700kg",
  698. "prepare_quality": "1995kg"
  699. }`
  700. if _, err := w.Write([]byte(raw)); err != nil {
  701. t.Fatal(err)
  702. }
  703. })
  704. server.HandleFunc("/mediaurl", func(w http.ResponseWriter, r *http.Request) {
  705. filename := testIMGName
  706. file, err := os.Open(filename)
  707. if err != nil {
  708. t.Fatal((err))
  709. }
  710. defer file.Close()
  711. ext := path.Ext(filename)
  712. ext = ext[1:len(ext)]
  713. w.Header().Set("Content-Type", "image/"+ext)
  714. w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", path.Base(filename)))
  715. w.WriteHeader(http.StatusOK)
  716. if _, err := io.Copy(w, file); err != nil {
  717. t.Fatal(err)
  718. }
  719. })
  720. ts := httptest.NewServer(server)
  721. defer ts.Close()
  722. _, err := vehicleLicenseByURL(ts.URL+apiVehicleLicense, "mock-access-token", ts.URL+"/mediaurl", RecognizeModePhoto)
  723. if err != nil {
  724. t.Fatal(err)
  725. }
  726. _, err = vehicleLicenseByURL(ts.URL+apiVehicleLicense, "mock-access-token", ts.URL+"/mediaurl", RecognizeModeScan)
  727. if err != nil {
  728. t.Fatal(err)
  729. }
  730. }
  731. func TestVehicleLicense(t *testing.T) {
  732. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  733. if r.Method != "POST" {
  734. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  735. }
  736. path := r.URL.EscapedPath()
  737. if path != apiVehicleLicense {
  738. t.Fatalf("Except to path '%s',get '%s'", apiVehicleLicense, path)
  739. }
  740. if err := r.ParseForm(); err != nil {
  741. t.Fatal(err)
  742. }
  743. queries := []string{"type", "access_token"}
  744. for _, v := range queries {
  745. content := r.Form.Get(v)
  746. if content == "" {
  747. t.Fatalf("Params [%s] can not be empty", v)
  748. }
  749. }
  750. if _, _, err := r.FormFile("img"); err != nil {
  751. t.Fatal(err)
  752. }
  753. w.WriteHeader(http.StatusOK)
  754. w.Header().Set("Content-Type", "application/json")
  755. raw := `{
  756. "vhicle_type": "小型普通客⻋",
  757. "owner": "东莞市xxxxx机械厂",
  758. "addr": "广东省东莞市xxxxx号",
  759. "use_character": "非营运",
  760. "model": "江淮牌HFCxxxxxxx",
  761. "vin": "LJ166xxxxxxxx51",
  762. "engine_num": "J3xxxxx3",
  763. "register_date": "2018-07-06",
  764. "issue_date": "2018-07-01",
  765. "plate_num_b": "粤xxxxx",
  766. "record": "441xxxxxx3",
  767. "passengers_num": "7人",
  768. "total_quality": "2700kg",
  769. "prepare_quality": "1995kg"
  770. }`
  771. if _, err := w.Write([]byte(raw)); err != nil {
  772. t.Fatal(err)
  773. }
  774. }))
  775. defer ts.Close()
  776. _, err := vehicleLicense(ts.URL+apiVehicleLicense, "mock-access-token", testIMGName, RecognizeModePhoto)
  777. if err != nil {
  778. t.Fatal(err)
  779. }
  780. _, err = vehicleLicense(ts.URL+apiVehicleLicense, "mock-access-token", testIMGName, RecognizeModeScan)
  781. if err != nil {
  782. t.Fatal(err)
  783. }
  784. }