附近小店
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

img_test.go 9.7 KiB

hace 2 meses
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 TestAICrop(t *testing.T) {
  12. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  13. if r.Method != "POST" {
  14. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  15. }
  16. if r.URL.EscapedPath() != "/cv/img/aicrop" {
  17. t.Error("Invalid request path")
  18. }
  19. if err := r.ParseForm(); err != nil {
  20. t.Fatal(err)
  21. }
  22. if r.Form.Get("access_token") == "" {
  23. t.Fatalf("access_token can not be empty")
  24. }
  25. if _, _, err := r.FormFile("img"); err != nil {
  26. t.Fatal(err)
  27. }
  28. w.WriteHeader(http.StatusOK)
  29. w.Header().Set("Content-Type", "application/json")
  30. raw := `{
  31. "errcode": 0,
  32. "errmsg": "ok",
  33. "results": [
  34. {
  35. "crop_left": 112,
  36. "crop_top": 0,
  37. "crop_right": 839,
  38. "crop_bottom": 727
  39. },
  40. {
  41. "crop_left": 0,
  42. "crop_top": 205,
  43. "crop_right": 965,
  44. "crop_bottom": 615
  45. }
  46. ],
  47. "img_size": {
  48. "w": 966,
  49. "h": 728
  50. }
  51. }`
  52. if _, err := w.Write([]byte(raw)); err != nil {
  53. t.Fatal(err)
  54. }
  55. }))
  56. defer ts.Close()
  57. _, err := aiCrop(ts.URL+apiAICrop, "mock-access-token", testIMGName)
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. }
  62. func TestAICropByURL(t *testing.T) {
  63. server := http.NewServeMux()
  64. server.HandleFunc("/cv/img/aicrop", func(w http.ResponseWriter, r *http.Request) {
  65. if r.Method != "POST" {
  66. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  67. }
  68. if r.URL.EscapedPath() != "/cv/img/aicrop" {
  69. t.Error("Invalid request path")
  70. }
  71. if err := r.ParseForm(); err != nil {
  72. t.Fatal(err)
  73. }
  74. queries := []string{"access_token", "img_url"}
  75. for _, v := range queries {
  76. content := r.Form.Get(v)
  77. if content == "" {
  78. t.Fatalf("%v can not be empty", v)
  79. }
  80. }
  81. w.WriteHeader(http.StatusOK)
  82. w.Header().Set("Content-Type", "application/json")
  83. raw := `{
  84. "errcode": 0,
  85. "errmsg": "ok",
  86. "results": [
  87. {
  88. "crop_left": 112,
  89. "crop_top": 0,
  90. "crop_right": 839,
  91. "crop_bottom": 727
  92. },
  93. {
  94. "crop_left": 0,
  95. "crop_top": 205,
  96. "crop_right": 965,
  97. "crop_bottom": 615
  98. }
  99. ],
  100. "img_size": {
  101. "w": 966,
  102. "h": 728
  103. }
  104. }`
  105. if _, err := w.Write([]byte(raw)); err != nil {
  106. t.Fatal(err)
  107. }
  108. })
  109. server.HandleFunc("/mediaurl", func(w http.ResponseWriter, r *http.Request) {
  110. filename := testIMGName
  111. file, err := os.Open(filename)
  112. if err != nil {
  113. t.Fatal((err))
  114. }
  115. defer file.Close()
  116. ext := path.Ext(filename)
  117. ext = ext[1:len(ext)]
  118. w.Header().Set("Content-Type", "image/"+ext)
  119. w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", path.Base(filename)))
  120. w.WriteHeader(http.StatusOK)
  121. if _, err := io.Copy(w, file); err != nil {
  122. t.Fatal(err)
  123. }
  124. })
  125. ts := httptest.NewServer(server)
  126. defer ts.Close()
  127. _, err := aiCropByURL(ts.URL+apiAICrop, "mock-access-token", ts.URL+"/mediaurl")
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. }
  132. func TestScanQRCode(t *testing.T) {
  133. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  134. if r.Method != "POST" {
  135. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  136. }
  137. path := r.URL.EscapedPath()
  138. if path != apiScanQRCode {
  139. t.Fatalf("Except to path '%s',get '%s'", apiScanQRCode, path)
  140. }
  141. if err := r.ParseForm(); err != nil {
  142. t.Fatal(err)
  143. }
  144. if r.Form.Get("access_token") == "" {
  145. t.Fatalf("access_token can not be empty")
  146. }
  147. if _, _, err := r.FormFile("img"); err != nil {
  148. t.Fatal(err)
  149. }
  150. w.WriteHeader(http.StatusOK)
  151. w.Header().Set("Content-Type", "application/json")
  152. raw := `{
  153. "errcode": 0,
  154. "errmsg": "ok",
  155. "code_results": [
  156. {
  157. "type_name": "QR_CODE",
  158. "data": "http://www.qq.com",
  159. "pos": {
  160. "left_top": {
  161. "x": 585,
  162. "y": 378
  163. },
  164. "right_top": {
  165. "x": 828,
  166. "y": 378
  167. },
  168. "right_bottom": {
  169. "x": 828,
  170. "y": 618
  171. },
  172. "left_bottom": {
  173. "x": 585,
  174. "y": 618
  175. }
  176. }
  177. },
  178. {
  179. "type_name": "QR_CODE",
  180. "data": "https://mp.weixin.qq.com",
  181. "pos": {
  182. "left_top": {
  183. "x": 185,
  184. "y": 142
  185. },
  186. "right_top": {
  187. "x": 396,
  188. "y": 142
  189. },
  190. "right_bottom": {
  191. "x": 396,
  192. "y": 353
  193. },
  194. "left_bottom": {
  195. "x": 185,
  196. "y": 353
  197. }
  198. }
  199. },
  200. {
  201. "type_name": "EAN_13",
  202. "data": "5906789678957"
  203. },
  204. {
  205. "type_name": "CODE_128",
  206. "data": "50090500019191"
  207. }
  208. ],
  209. "img_size": {
  210. "w": 1000,
  211. "h": 900
  212. }
  213. }`
  214. if _, err := w.Write([]byte(raw)); err != nil {
  215. t.Fatal(err)
  216. }
  217. }))
  218. defer ts.Close()
  219. _, err := scanQRCode(ts.URL+apiScanQRCode, "mock-access-token", testIMGName)
  220. if err != nil {
  221. t.Fatal(err)
  222. }
  223. }
  224. func TestScanQRCodeByURL(t *testing.T) {
  225. server := http.NewServeMux()
  226. server.HandleFunc(apiScanQRCode, func(w http.ResponseWriter, r *http.Request) {
  227. if r.Method != "POST" {
  228. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  229. }
  230. path := r.URL.EscapedPath()
  231. if path != apiScanQRCode {
  232. t.Fatalf("Except to path '%s',get '%s'", apiScanQRCode, path)
  233. }
  234. if err := r.ParseForm(); err != nil {
  235. t.Fatal(err)
  236. }
  237. queries := []string{"access_token", "img_url"}
  238. for _, v := range queries {
  239. content := r.Form.Get(v)
  240. if content == "" {
  241. t.Fatalf("%v can not be empty", v)
  242. }
  243. }
  244. w.WriteHeader(http.StatusOK)
  245. w.Header().Set("Content-Type", "application/json")
  246. raw := `{
  247. "errcode": 0,
  248. "errmsg": "ok",
  249. "code_results": [
  250. {
  251. "type_name": "QR_CODE",
  252. "data": "http://www.qq.com",
  253. "pos": {
  254. "left_top": {
  255. "x": 585,
  256. "y": 378
  257. },
  258. "right_top": {
  259. "x": 828,
  260. "y": 378
  261. },
  262. "right_bottom": {
  263. "x": 828,
  264. "y": 618
  265. },
  266. "left_bottom": {
  267. "x": 585,
  268. "y": 618
  269. }
  270. }
  271. },
  272. {
  273. "type_name": "QR_CODE",
  274. "data": "https://mp.weixin.qq.com",
  275. "pos": {
  276. "left_top": {
  277. "x": 185,
  278. "y": 142
  279. },
  280. "right_top": {
  281. "x": 396,
  282. "y": 142
  283. },
  284. "right_bottom": {
  285. "x": 396,
  286. "y": 353
  287. },
  288. "left_bottom": {
  289. "x": 185,
  290. "y": 353
  291. }
  292. }
  293. },
  294. {
  295. "type_name": "EAN_13",
  296. "data": "5906789678957"
  297. },
  298. {
  299. "type_name": "CODE_128",
  300. "data": "50090500019191"
  301. }
  302. ],
  303. "img_size": {
  304. "w": 1000,
  305. "h": 900
  306. }
  307. }`
  308. if _, err := w.Write([]byte(raw)); err != nil {
  309. t.Fatal(err)
  310. }
  311. })
  312. server.HandleFunc("/mediaurl", func(w http.ResponseWriter, r *http.Request) {
  313. filename := testIMGName
  314. file, err := os.Open(filename)
  315. if err != nil {
  316. t.Fatal((err))
  317. }
  318. defer file.Close()
  319. ext := path.Ext(filename)
  320. ext = ext[1:len(ext)]
  321. w.Header().Set("Content-Type", "image/"+ext)
  322. w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", path.Base(filename)))
  323. w.WriteHeader(http.StatusOK)
  324. if _, err := io.Copy(w, file); err != nil {
  325. t.Fatal(err)
  326. }
  327. })
  328. ts := httptest.NewServer(server)
  329. defer ts.Close()
  330. _, err := scanQRCodeByURL(ts.URL+apiScanQRCode, "mock-access-token", ts.URL+"/mediaurl")
  331. if err != nil {
  332. t.Fatal(err)
  333. }
  334. }
  335. func TestSuperResolution(t *testing.T) {
  336. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  337. if r.Method != "POST" {
  338. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  339. }
  340. path := r.URL.EscapedPath()
  341. if path != apiSuperResolution {
  342. t.Fatalf("Except to path '%s',get '%s'", apiSuperResolution, path)
  343. }
  344. if err := r.ParseForm(); err != nil {
  345. t.Fatal(err)
  346. }
  347. if r.Form.Get("access_token") == "" {
  348. t.Fatalf("access_token can not be empty")
  349. }
  350. if _, _, err := r.FormFile("img"); err != nil {
  351. t.Fatal(err)
  352. }
  353. w.WriteHeader(http.StatusOK)
  354. w.Header().Set("Content-Type", "application/json")
  355. raw := `{
  356. "errcode": 0,
  357. "errmsg": "ok",
  358. "media_id": "6WXsIXkG7lXuDLspD9xfm5dsvHzb0EFl0li6ySxi92ap8Vl3zZoD9DpOyNudeJGB"
  359. }`
  360. if _, err := w.Write([]byte(raw)); err != nil {
  361. t.Fatal(err)
  362. }
  363. }))
  364. defer ts.Close()
  365. _, err := superResolution(ts.URL+apiSuperResolution, "mock-access-token", testIMGName)
  366. if err != nil {
  367. t.Fatal(err)
  368. }
  369. }
  370. func TestSuperResolutionByURL(t *testing.T) {
  371. server := http.NewServeMux()
  372. server.HandleFunc(apiSuperResolution, func(w http.ResponseWriter, r *http.Request) {
  373. if r.Method != "POST" {
  374. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  375. }
  376. path := r.URL.EscapedPath()
  377. if path != apiSuperResolution {
  378. t.Fatalf("Except to path '%s',get '%s'", apiSuperResolution, path)
  379. }
  380. if err := r.ParseForm(); err != nil {
  381. t.Fatal(err)
  382. }
  383. queries := []string{"access_token", "img_url"}
  384. for _, v := range queries {
  385. content := r.Form.Get(v)
  386. if content == "" {
  387. t.Fatalf("%v can not be empty", v)
  388. }
  389. }
  390. w.WriteHeader(http.StatusOK)
  391. w.Header().Set("Content-Type", "application/json")
  392. raw := `{
  393. "errcode": 0,
  394. "errmsg": "ok",
  395. "media_id": "6WXsIXkG7lXuDLspD9xfm5dsvHzb0EFl0li6ySxi92ap8Vl3zZoD9DpOyNudeJGB"
  396. }`
  397. if _, err := w.Write([]byte(raw)); err != nil {
  398. t.Fatal(err)
  399. }
  400. })
  401. server.HandleFunc("/mediaurl", func(w http.ResponseWriter, r *http.Request) {
  402. filename := testIMGName
  403. file, err := os.Open(filename)
  404. if err != nil {
  405. t.Fatal((err))
  406. }
  407. defer file.Close()
  408. ext := path.Ext(filename)
  409. ext = ext[1:len(ext)]
  410. w.Header().Set("Content-Type", "image/"+ext)
  411. w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", path.Base(filename)))
  412. w.WriteHeader(http.StatusOK)
  413. if _, err := io.Copy(w, file); err != nil {
  414. t.Fatal(err)
  415. }
  416. })
  417. ts := httptest.NewServer(server)
  418. defer ts.Close()
  419. _, err := superResolutionByURL(ts.URL+apiSuperResolution, "mock-access-token", ts.URL+"/mediaurl")
  420. if err != nil {
  421. t.Fatal(err)
  422. }
  423. }