附近小店
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

customer_service_message_test.go 7.2 KiB

před 1 měsícem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. package weapp
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "net/http/httptest"
  8. "os"
  9. "path"
  10. "reflect"
  11. "testing"
  12. )
  13. func TestSetTyping(t *testing.T) {
  14. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  15. if r.Method != "POST" {
  16. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  17. }
  18. path := r.URL.EscapedPath()
  19. if path != apiSetTyping {
  20. t.Fatalf("Except to path '%s',get '%s'", apiSetTyping, path)
  21. }
  22. if err := r.ParseForm(); err != nil {
  23. t.Fatal(err)
  24. }
  25. if r.Form.Get("access_token") == "" {
  26. t.Fatalf("access_token can not be empty")
  27. }
  28. params := make(map[string]interface{})
  29. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  30. t.Fatal(err)
  31. }
  32. param, ok := params["touser"]
  33. if !ok || param == "" {
  34. t.Error("param touser can not be empty")
  35. }
  36. param, ok = params["command"]
  37. if !ok || param == "" {
  38. t.Error("param command can not be empty")
  39. }
  40. w.WriteHeader(http.StatusOK)
  41. raw := `{
  42. "errcode": 0,
  43. "errmsg": "ok"
  44. }`
  45. if _, err := w.Write([]byte(raw)); err != nil {
  46. t.Fatal(err)
  47. }
  48. }))
  49. defer ts.Close()
  50. _, err := setTyping("mock-access-token", "mock-open-id", "mock-command", ts.URL+apiSetTyping)
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. }
  55. func TestUploadTempMedia(t *testing.T) {
  56. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  57. if r.Method != "POST" {
  58. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  59. }
  60. path := r.URL.EscapedPath()
  61. if path != apiUploadTemplateMedia {
  62. t.Fatalf("Except to path '%s',get '%s'", apiUploadTemplateMedia, path)
  63. }
  64. if err := r.ParseForm(); err != nil {
  65. t.Fatal(err)
  66. }
  67. queries := []string{"access_token", "type"}
  68. for _, v := range queries {
  69. content := r.Form.Get(v)
  70. if content == "" {
  71. t.Fatalf("%v can not be empty", v)
  72. }
  73. }
  74. if _, _, err := r.FormFile("media"); err != nil {
  75. t.Fatal(err)
  76. }
  77. w.WriteHeader(http.StatusOK)
  78. w.Header().Set("Content-Type", "application/json")
  79. raw := `{
  80. "errcode": 0,
  81. "errmsg": "ok",
  82. "type": "image",
  83. "media_id": "MEDIA_ID",
  84. "created_at": 1234567890
  85. }`
  86. if _, err := w.Write([]byte(raw)); err != nil {
  87. t.Fatal(err)
  88. }
  89. }))
  90. defer ts.Close()
  91. _, err := uploadTempMedia("mock-access-token", "mock-media-type", testIMGName, ts.URL+apiUploadTemplateMedia)
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. }
  96. func TestGetTempMedia(t *testing.T) {
  97. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  98. if r.Method != "GET" {
  99. t.Fatalf("Expect 'GET' get '%s'", r.Method)
  100. }
  101. ePath := r.URL.EscapedPath()
  102. if ePath != apiGetTemplateMedia {
  103. t.Fatalf("Except to path '%s',get '%s'", apiGetTemplateMedia, ePath)
  104. }
  105. if err := r.ParseForm(); err != nil {
  106. t.Fatal(err)
  107. }
  108. queries := []string{"access_token", "media_id"}
  109. for _, v := range queries {
  110. content := r.Form.Get(v)
  111. if content == "" {
  112. t.Fatalf("%v can not be empty", v)
  113. }
  114. }
  115. filename := testIMGName
  116. file, err := os.Open(filename)
  117. if err != nil {
  118. t.Fatal((err))
  119. }
  120. defer file.Close()
  121. ext := path.Ext(filename)
  122. ext = ext[1:len(ext)]
  123. w.Header().Set("Content-Type", "image/"+ext)
  124. w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", path.Base(filename)))
  125. w.WriteHeader(http.StatusOK)
  126. if _, err := io.Copy(w, file); err != nil {
  127. t.Fatal(err)
  128. }
  129. }))
  130. defer ts.Close()
  131. resp, _, err := getTempMedia("mock-access-token", "mock-media-id", ts.URL+apiGetTemplateMedia)
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. resp.Body.Close()
  136. }
  137. func TestSendCustomerServiceMessage(t *testing.T) {
  138. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  139. if r.Method != "POST" {
  140. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  141. }
  142. path := r.URL.EscapedPath()
  143. if path != apiSendMessage {
  144. t.Fatalf("Except to path '%s',get '%s'", apiSendMessage, path)
  145. }
  146. if err := r.ParseForm(); err != nil {
  147. t.Fatal(err)
  148. }
  149. if r.Form.Get("access_token") == "" {
  150. t.Error("access_token can not be empty")
  151. }
  152. params := make(object)
  153. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  154. t.Fatal(err)
  155. }
  156. param, ok := params["touser"]
  157. if !ok || param == "" {
  158. t.Error("param touser can not be empty")
  159. }
  160. param, ok = params["msgtype"]
  161. if !ok || param == "" {
  162. t.Error("param command can not be empty")
  163. }
  164. switch param {
  165. case "text":
  166. param, ok := params["text"]
  167. if !ok {
  168. t.Error("param text can not be empty")
  169. }
  170. obj, ok := param.(object)
  171. if !ok {
  172. t.Errorf("unexpected value type of tex: %s", reflect.TypeOf(param))
  173. }
  174. param, ok = obj["content"]
  175. if !ok {
  176. t.Error("param text.content can not be empty")
  177. }
  178. case "image":
  179. param, ok := params["image"]
  180. if !ok {
  181. t.Error("param command can not be empty")
  182. }
  183. obj, ok := param.(object)
  184. if !ok {
  185. t.Error("unexpected value type of image")
  186. }
  187. param, ok = obj["media_id"]
  188. if !ok {
  189. t.Error("param image.media_id can not be empty")
  190. }
  191. case "link":
  192. param, ok := params["link"]
  193. if !ok {
  194. t.Error("param link can not be empty")
  195. }
  196. obj, ok := param.(object)
  197. if !ok {
  198. t.Error("unexpected value type of link")
  199. }
  200. param, ok = obj["title"]
  201. if !ok {
  202. t.Error("param link.title can not be empty")
  203. }
  204. param, ok = obj["description"]
  205. if !ok {
  206. t.Error("param link.description can not be empty")
  207. }
  208. param, ok = obj["url"]
  209. if !ok {
  210. t.Error("param link.url can not be empty")
  211. }
  212. param, ok = obj["thumb_url"]
  213. if !ok {
  214. t.Error("param link.thumb_url can not be empty")
  215. }
  216. case "miniprogrampage":
  217. param, ok := params["miniprogrampage"]
  218. if !ok {
  219. t.Error("param miniprogrampage can not be empty")
  220. }
  221. obj, ok := param.(object)
  222. if !ok {
  223. t.Error("unexpected value type of miniprogrampage")
  224. }
  225. param, ok = obj["title"]
  226. if !ok {
  227. t.Error("param miniprogrampage.title can not be empty")
  228. }
  229. param, ok = obj["pagepath"]
  230. if !ok {
  231. t.Error("param miniprogrampage.pagepath can not be empty")
  232. }
  233. param, ok = obj["thumb_media_id"]
  234. if !ok {
  235. t.Error("param miniprogrampage.thumb_media_id can not be empty")
  236. }
  237. default:
  238. t.Fatalf("unexpected msgtype: %s", param)
  239. }
  240. w.WriteHeader(http.StatusOK)
  241. raw := `{
  242. "errcode": 0,
  243. "errmsg": "ok"
  244. }`
  245. if _, err := w.Write([]byte(raw)); err != nil {
  246. t.Fatal(err)
  247. }
  248. }))
  249. defer ts.Close()
  250. messages := []csMessage{
  251. csMessage{
  252. Receiver: "mock-open-id",
  253. Type: csMsgTypeText,
  254. Image: CSMsgImage{
  255. MediaID: "mock-media-id",
  256. },
  257. },
  258. csMessage{
  259. Receiver: "mock-open-id",
  260. Type: csMsgTypeLink,
  261. Link: CSMsgLink{
  262. Title: "mock-title",
  263. Description: "mock-description",
  264. URL: "mock-url",
  265. ThumbURL: "mock-thumb-url",
  266. },
  267. },
  268. csMessage{
  269. Receiver: "mock-open-id",
  270. Type: csMsgTypeMPCard,
  271. MPCard: CSMsgMPCard{
  272. Title: "mock-title",
  273. PagePath: "mock-page-path",
  274. ThumbMediaID: "mock-thumb-media-id",
  275. },
  276. },
  277. csMessage{
  278. Receiver: "mock-open-id",
  279. Type: csMsgTypeText,
  280. Text: CSMsgText{
  281. Content: "mock-content",
  282. },
  283. },
  284. }
  285. for _, msg := range messages {
  286. _, err := doSendMessage("mock-access-token", msg, ts.URL+apiSendMessage)
  287. if err != nil {
  288. t.Error(err)
  289. }
  290. }
  291. }