附近小店
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

241 rader
5.3 KiB

  1. package weapp
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "net/http/httptest"
  9. "os"
  10. "path"
  11. "strings"
  12. "testing"
  13. )
  14. func TestIMGSecCheck(t *testing.T) {
  15. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  16. if r.Method != "POST" {
  17. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  18. }
  19. path := r.URL.EscapedPath()
  20. if path != apiIMGSecCheck {
  21. t.Fatalf("Except to path '%s',get '%s'", apiIMGSecCheck, path)
  22. }
  23. if err := r.ParseForm(); err != nil {
  24. t.Fatal(err)
  25. }
  26. if r.Form.Get("access_token") == "" {
  27. t.Fatalf("access_token can not be empty")
  28. }
  29. if _, _, err := r.FormFile("media"); err != nil {
  30. t.Fatal(err)
  31. }
  32. w.WriteHeader(http.StatusOK)
  33. w.Header().Set("Content-Type", "application/json")
  34. raw := `{
  35. "errcode": 0,
  36. "errmsg": "ok"
  37. }`
  38. if _, err := w.Write([]byte(raw)); err != nil {
  39. t.Fatal(err)
  40. }
  41. }))
  42. defer ts.Close()
  43. _, err := imgSecCheck(ts.URL+apiIMGSecCheck, "mock-access-token", testIMGName)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. }
  48. func TestMediaCheckAsync(t *testing.T) {
  49. localServer := http.NewServeMux()
  50. localServer.HandleFunc("/notify", func(w http.ResponseWriter, r *http.Request) {
  51. aesKey := base64.StdEncoding.EncodeToString([]byte("mock-aes-key"))
  52. srv, err := NewServer("mock-app-id", "mock-access-token", aesKey, "mock-mch-id", "mock-api-key", false)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. srv.OnMediaCheckAsync(func(mix *MediaCheckAsyncResult) {
  57. if mix.ToUserName == "" {
  58. t.Error("ToUserName can not be empty")
  59. }
  60. if mix.FromUserName == "" {
  61. t.Error("FromUserName can not be empty")
  62. }
  63. if mix.CreateTime == 0 {
  64. t.Error("CreateTime can not be empty")
  65. }
  66. if mix.MsgType != "event" {
  67. t.Error("Unexpected message type")
  68. }
  69. if mix.Event != "wxa_media_check" {
  70. t.Error("Unexpected message event")
  71. }
  72. if mix.AppID == "" {
  73. t.Error("AppID can not be empty")
  74. }
  75. if mix.TraceID == "" {
  76. t.Error("TraceID can not be empty")
  77. }
  78. })
  79. if err := srv.Serve(w, r); err != nil {
  80. t.Fatal(err)
  81. }
  82. })
  83. tls := httptest.NewServer(localServer)
  84. defer tls.Close()
  85. remoteServer := http.NewServeMux()
  86. remoteServer.HandleFunc(apiMediaCheckAsync, func(w http.ResponseWriter, r *http.Request) {
  87. if r.Method != "POST" {
  88. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  89. }
  90. path := r.URL.EscapedPath()
  91. if path != apiMediaCheckAsync {
  92. t.Fatalf("Except to path '%s',get '%s'", apiMediaCheckAsync, path)
  93. }
  94. if err := r.ParseForm(); err != nil {
  95. t.Fatal(err)
  96. }
  97. if r.Form.Get("access_token") == "" {
  98. t.Fatalf("access_token can not be empty")
  99. }
  100. params := struct {
  101. MediaURL string `json:"media_url"`
  102. MediaType uint8 `json:"media_type"`
  103. }{}
  104. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  105. t.Fatal(err)
  106. }
  107. if params.MediaURL == "" {
  108. t.Error("Response column media_url can not be empty")
  109. }
  110. if params.MediaType == 0 {
  111. t.Error("Response column media_type can not be zero")
  112. }
  113. w.WriteHeader(http.StatusOK)
  114. raw := `{
  115. "errcode" : 0,
  116. "errmsg" : "ok",
  117. "trace_id" : "967e945cd8a3e458f3c74dcb886068e9"
  118. }`
  119. if _, err := w.Write([]byte(raw)); err != nil {
  120. t.Fatal(err)
  121. }
  122. raw = `{
  123. "ToUserName" : "gh_38cc49f9733b",
  124. "FromUserName" : "oH1fu0FdHqpToe2T6gBj0WyB8iS1",
  125. "CreateTime" : 1552465698,
  126. "MsgType" : "event",
  127. "Event" : "wxa_media_check",
  128. "isrisky" : 0,
  129. "extra_info_json" : "",
  130. "appid" : "wxd8c59133dfcbfc71",
  131. "trace_id" : "967e945cd8a3e458f3c74dcb886068e9",
  132. "status_code" : 0
  133. }`
  134. reader := strings.NewReader(raw)
  135. http.Post(tls.URL+"/notify", "application/json", reader)
  136. })
  137. remoteServer.HandleFunc("/mediaurl", func(w http.ResponseWriter, r *http.Request) {
  138. filename := testIMGName
  139. file, err := os.Open(filename)
  140. if err != nil {
  141. t.Fatal((err))
  142. }
  143. defer file.Close()
  144. ext := path.Ext(filename)
  145. ext = ext[1:len(ext)]
  146. w.Header().Set("Content-Type", "image/"+ext)
  147. w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", path.Base(filename)))
  148. w.WriteHeader(http.StatusOK)
  149. if _, err := io.Copy(w, file); err != nil {
  150. t.Fatal(err)
  151. }
  152. })
  153. trs := httptest.NewServer(remoteServer)
  154. defer trs.Close()
  155. _, err := mediaCheckAsync(trs.URL+apiMediaCheckAsync, "mock-access-token", trs.URL+"/mediaurl", MediaTypeImage)
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. }
  160. func TestMSGSecCheck(t *testing.T) {
  161. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  162. if r.Method != "POST" {
  163. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  164. }
  165. path := r.URL.EscapedPath()
  166. if path != "/wxa/img_sec_check" {
  167. t.Error("Invalid request path")
  168. }
  169. if err := r.ParseForm(); err != nil {
  170. t.Fatal(err)
  171. }
  172. if r.Form.Get("access_token") == "" {
  173. t.Fatalf("access_token can not be empty")
  174. }
  175. params := struct {
  176. Content string `json:"content"`
  177. }{}
  178. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  179. t.Fatal(err)
  180. }
  181. if params.Content == "" {
  182. t.Error("Response column content can not be empty")
  183. }
  184. w.WriteHeader(http.StatusOK)
  185. raw := `{
  186. "errcode": 0,
  187. "errmsg": "ok"
  188. }`
  189. if _, err := w.Write([]byte(raw)); err != nil {
  190. t.Fatal(err)
  191. }
  192. }))
  193. defer ts.Close()
  194. _, err := msgSecCheck(ts.URL+apiIMGSecCheck, "mock-access-token", "mock-content")
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. }