附近小店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

76 lines
1.6 KiB

  1. package weapp
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. )
  8. func TestSendSubscribeMessage(t *testing.T) {
  9. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  10. if r.Method != "POST" {
  11. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  12. }
  13. path := r.URL.EscapedPath()
  14. if path != "/cgi-bin/message/subscribe/send" {
  15. t.Fatalf("Except path '/cgi-bin/message/subscribe/send' got '%s'", path)
  16. }
  17. if err := r.ParseForm(); err != nil {
  18. t.Fatal(err)
  19. }
  20. if r.Form.Get("access_token") == "" {
  21. t.Fatalf("access_token can not be empty")
  22. }
  23. params := struct {
  24. ToUser string `json:"touser"` // 用户 openid
  25. TemplateID string `json:"template_id"`
  26. Page string `json:"page,omitempty"`
  27. Data map[string]struct {
  28. Value string `json:"value"`
  29. } `json:"data"`
  30. }{}
  31. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  32. t.Fatal(err)
  33. }
  34. if params.ToUser == "" {
  35. t.Fatal("param touser can not be empty")
  36. }
  37. w.WriteHeader(http.StatusOK)
  38. raw := `{
  39. "errcode": 0,
  40. "errmsg": "ok"
  41. }`
  42. if _, err := w.Write([]byte(raw)); err != nil {
  43. t.Fatal(err)
  44. }
  45. }))
  46. defer ts.Close()
  47. sender := SubscribeMessage{
  48. ToUser: "mock-open-id",
  49. TemplateID: "mock-template-id",
  50. Page: "mock-page",
  51. MiniprogramState: MiniprogramStateDeveloper,
  52. Data: SubscribeMessageData{
  53. "mock01.DATA": {
  54. Value: "mock-value",
  55. },
  56. },
  57. }
  58. _, err := sender.send(ts.URL+apiSendSubscribeMessage, "mock-access-token")
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. }