|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474 |
- package weapp
-
- import (
- "fmt"
- "io"
- "net/http"
- "net/http/httptest"
- "os"
- "path"
- "testing"
- )
-
- func TestAICrop(t *testing.T) {
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- if r.Method != "POST" {
- t.Fatalf("Expect 'POST' get '%s'", r.Method)
- }
-
- if r.URL.EscapedPath() != "/cv/img/aicrop" {
- t.Error("Invalid request path")
- }
-
- if err := r.ParseForm(); err != nil {
- t.Fatal(err)
- }
-
- if r.Form.Get("access_token") == "" {
- t.Fatalf("access_token can not be empty")
- }
-
- if _, _, err := r.FormFile("img"); err != nil {
- t.Fatal(err)
- }
-
- w.WriteHeader(http.StatusOK)
- w.Header().Set("Content-Type", "application/json")
- raw := `{
- "errcode": 0,
- "errmsg": "ok",
- "results": [
- {
- "crop_left": 112,
- "crop_top": 0,
- "crop_right": 839,
- "crop_bottom": 727
- },
- {
- "crop_left": 0,
- "crop_top": 205,
- "crop_right": 965,
- "crop_bottom": 615
- }
- ],
- "img_size": {
- "w": 966,
- "h": 728
- }
- }`
- if _, err := w.Write([]byte(raw)); err != nil {
- t.Fatal(err)
- }
- }))
- defer ts.Close()
-
- _, err := aiCrop(ts.URL+apiAICrop, "mock-access-token", testIMGName)
- if err != nil {
- t.Fatal(err)
- }
- }
-
- func TestAICropByURL(t *testing.T) {
- server := http.NewServeMux()
- server.HandleFunc("/cv/img/aicrop", func(w http.ResponseWriter, r *http.Request) {
- if r.Method != "POST" {
- t.Fatalf("Expect 'POST' get '%s'", r.Method)
- }
-
- if r.URL.EscapedPath() != "/cv/img/aicrop" {
- t.Error("Invalid request path")
- }
-
- if err := r.ParseForm(); err != nil {
- t.Fatal(err)
- }
-
- queries := []string{"access_token", "img_url"}
- for _, v := range queries {
- content := r.Form.Get(v)
- if content == "" {
- t.Fatalf("%v can not be empty", v)
- }
- }
-
- w.WriteHeader(http.StatusOK)
- w.Header().Set("Content-Type", "application/json")
- raw := `{
- "errcode": 0,
- "errmsg": "ok",
- "results": [
- {
- "crop_left": 112,
- "crop_top": 0,
- "crop_right": 839,
- "crop_bottom": 727
- },
- {
- "crop_left": 0,
- "crop_top": 205,
- "crop_right": 965,
- "crop_bottom": 615
- }
- ],
- "img_size": {
- "w": 966,
- "h": 728
- }
- }`
- if _, err := w.Write([]byte(raw)); err != nil {
- t.Fatal(err)
- }
- })
-
- server.HandleFunc("/mediaurl", func(w http.ResponseWriter, r *http.Request) {
- filename := testIMGName
- file, err := os.Open(filename)
- if err != nil {
- t.Fatal((err))
- }
- defer file.Close()
-
- ext := path.Ext(filename)
- ext = ext[1:len(ext)]
- w.Header().Set("Content-Type", "image/"+ext)
- w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", path.Base(filename)))
- w.WriteHeader(http.StatusOK)
-
- if _, err := io.Copy(w, file); err != nil {
- t.Fatal(err)
- }
- })
-
- ts := httptest.NewServer(server)
- defer ts.Close()
-
- _, err := aiCropByURL(ts.URL+apiAICrop, "mock-access-token", ts.URL+"/mediaurl")
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestScanQRCode(t *testing.T) {
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- if r.Method != "POST" {
- t.Fatalf("Expect 'POST' get '%s'", r.Method)
- }
-
- path := r.URL.EscapedPath()
- if path != apiScanQRCode {
- t.Fatalf("Except to path '%s',get '%s'", apiScanQRCode, path)
- }
-
- if err := r.ParseForm(); err != nil {
- t.Fatal(err)
- }
-
- if r.Form.Get("access_token") == "" {
- t.Fatalf("access_token can not be empty")
- }
-
- if _, _, err := r.FormFile("img"); err != nil {
- t.Fatal(err)
- }
-
- w.WriteHeader(http.StatusOK)
- w.Header().Set("Content-Type", "application/json")
- raw := `{
- "errcode": 0,
- "errmsg": "ok",
- "code_results": [
- {
- "type_name": "QR_CODE",
- "data": "http://www.qq.com",
- "pos": {
- "left_top": {
- "x": 585,
- "y": 378
- },
- "right_top": {
- "x": 828,
- "y": 378
- },
- "right_bottom": {
- "x": 828,
- "y": 618
- },
- "left_bottom": {
- "x": 585,
- "y": 618
- }
- }
- },
- {
- "type_name": "QR_CODE",
- "data": "https://mp.weixin.qq.com",
- "pos": {
- "left_top": {
- "x": 185,
- "y": 142
- },
- "right_top": {
- "x": 396,
- "y": 142
- },
- "right_bottom": {
- "x": 396,
- "y": 353
- },
- "left_bottom": {
- "x": 185,
- "y": 353
- }
- }
- },
- {
- "type_name": "EAN_13",
- "data": "5906789678957"
- },
- {
- "type_name": "CODE_128",
- "data": "50090500019191"
- }
- ],
- "img_size": {
- "w": 1000,
- "h": 900
- }
- }`
- if _, err := w.Write([]byte(raw)); err != nil {
- t.Fatal(err)
- }
- }))
- defer ts.Close()
-
- _, err := scanQRCode(ts.URL+apiScanQRCode, "mock-access-token", testIMGName)
- if err != nil {
- t.Fatal(err)
- }
- }
-
- func TestScanQRCodeByURL(t *testing.T) {
- server := http.NewServeMux()
- server.HandleFunc(apiScanQRCode, func(w http.ResponseWriter, r *http.Request) {
- if r.Method != "POST" {
- t.Fatalf("Expect 'POST' get '%s'", r.Method)
- }
-
- path := r.URL.EscapedPath()
- if path != apiScanQRCode {
- t.Fatalf("Except to path '%s',get '%s'", apiScanQRCode, path)
- }
-
- if err := r.ParseForm(); err != nil {
- t.Fatal(err)
- }
-
- queries := []string{"access_token", "img_url"}
- for _, v := range queries {
- content := r.Form.Get(v)
- if content == "" {
- t.Fatalf("%v can not be empty", v)
- }
- }
-
- w.WriteHeader(http.StatusOK)
- w.Header().Set("Content-Type", "application/json")
- raw := `{
- "errcode": 0,
- "errmsg": "ok",
- "code_results": [
- {
- "type_name": "QR_CODE",
- "data": "http://www.qq.com",
- "pos": {
- "left_top": {
- "x": 585,
- "y": 378
- },
- "right_top": {
- "x": 828,
- "y": 378
- },
- "right_bottom": {
- "x": 828,
- "y": 618
- },
- "left_bottom": {
- "x": 585,
- "y": 618
- }
- }
- },
- {
- "type_name": "QR_CODE",
- "data": "https://mp.weixin.qq.com",
- "pos": {
- "left_top": {
- "x": 185,
- "y": 142
- },
- "right_top": {
- "x": 396,
- "y": 142
- },
- "right_bottom": {
- "x": 396,
- "y": 353
- },
- "left_bottom": {
- "x": 185,
- "y": 353
- }
- }
- },
- {
- "type_name": "EAN_13",
- "data": "5906789678957"
- },
- {
- "type_name": "CODE_128",
- "data": "50090500019191"
- }
- ],
- "img_size": {
- "w": 1000,
- "h": 900
- }
- }`
- if _, err := w.Write([]byte(raw)); err != nil {
- t.Fatal(err)
- }
- })
-
- server.HandleFunc("/mediaurl", func(w http.ResponseWriter, r *http.Request) {
- filename := testIMGName
- file, err := os.Open(filename)
- if err != nil {
- t.Fatal((err))
- }
- defer file.Close()
-
- ext := path.Ext(filename)
- ext = ext[1:len(ext)]
- w.Header().Set("Content-Type", "image/"+ext)
- w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", path.Base(filename)))
- w.WriteHeader(http.StatusOK)
-
- if _, err := io.Copy(w, file); err != nil {
- t.Fatal(err)
- }
- })
-
- ts := httptest.NewServer(server)
- defer ts.Close()
-
- _, err := scanQRCodeByURL(ts.URL+apiScanQRCode, "mock-access-token", ts.URL+"/mediaurl")
- if err != nil {
- t.Fatal(err)
- }
- }
-
- func TestSuperResolution(t *testing.T) {
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- if r.Method != "POST" {
- t.Fatalf("Expect 'POST' get '%s'", r.Method)
- }
-
- path := r.URL.EscapedPath()
- if path != apiSuperResolution {
- t.Fatalf("Except to path '%s',get '%s'", apiSuperResolution, path)
- }
-
- if err := r.ParseForm(); err != nil {
- t.Fatal(err)
- }
-
- if r.Form.Get("access_token") == "" {
- t.Fatalf("access_token can not be empty")
- }
-
- if _, _, err := r.FormFile("img"); err != nil {
- t.Fatal(err)
- }
-
- w.WriteHeader(http.StatusOK)
- w.Header().Set("Content-Type", "application/json")
- raw := `{
- "errcode": 0,
- "errmsg": "ok",
- "media_id": "6WXsIXkG7lXuDLspD9xfm5dsvHzb0EFl0li6ySxi92ap8Vl3zZoD9DpOyNudeJGB"
- }`
- if _, err := w.Write([]byte(raw)); err != nil {
- t.Fatal(err)
- }
- }))
- defer ts.Close()
-
- _, err := superResolution(ts.URL+apiSuperResolution, "mock-access-token", testIMGName)
- if err != nil {
- t.Fatal(err)
- }
- }
-
- func TestSuperResolutionByURL(t *testing.T) {
- server := http.NewServeMux()
- server.HandleFunc(apiSuperResolution, func(w http.ResponseWriter, r *http.Request) {
- if r.Method != "POST" {
- t.Fatalf("Expect 'POST' get '%s'", r.Method)
- }
-
- path := r.URL.EscapedPath()
- if path != apiSuperResolution {
- t.Fatalf("Except to path '%s',get '%s'", apiSuperResolution, path)
- }
-
- if err := r.ParseForm(); err != nil {
- t.Fatal(err)
- }
-
- queries := []string{"access_token", "img_url"}
- for _, v := range queries {
- content := r.Form.Get(v)
- if content == "" {
- t.Fatalf("%v can not be empty", v)
- }
- }
-
- w.WriteHeader(http.StatusOK)
- w.Header().Set("Content-Type", "application/json")
- raw := `{
- "errcode": 0,
- "errmsg": "ok",
- "media_id": "6WXsIXkG7lXuDLspD9xfm5dsvHzb0EFl0li6ySxi92ap8Vl3zZoD9DpOyNudeJGB"
- }`
- if _, err := w.Write([]byte(raw)); err != nil {
- t.Fatal(err)
- }
- })
-
- server.HandleFunc("/mediaurl", func(w http.ResponseWriter, r *http.Request) {
- filename := testIMGName
- file, err := os.Open(filename)
- if err != nil {
- t.Fatal((err))
- }
- defer file.Close()
-
- ext := path.Ext(filename)
- ext = ext[1:len(ext)]
- w.Header().Set("Content-Type", "image/"+ext)
- w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", path.Base(filename)))
- w.WriteHeader(http.StatusOK)
-
- if _, err := io.Copy(w, file); err != nil {
- t.Fatal(err)
- }
- })
-
- ts := httptest.NewServer(server)
- defer ts.Close()
-
- _, err := superResolutionByURL(ts.URL+apiSuperResolution, "mock-access-token", ts.URL+"/mediaurl")
- if err != nil {
- t.Fatal(err)
- }
- }
|