附近小店
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.
 
 
 

103 lines
2.5 KiB

  1. package weapp
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. )
  8. func TestSendUniformMessage(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 != apiSendUniformMessage {
  15. t.Fatalf("Except to path '%s',get '%s'", apiSendUniformMessage, 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. UniformWeappTmpMsg struct {
  26. TemplateID string `json:"template_id"`
  27. Page string `json:"page"`
  28. FormID string `json:"form_id"`
  29. Data map[string]struct {
  30. Value string `json:"value"`
  31. } `json:"data"`
  32. EmphasisKeyword string `json:"emphasis_keyword"`
  33. } `json:"weapp_template_msg"`
  34. UniformMpTmpMsg struct {
  35. AppID string `json:"appid"`
  36. TemplateID string `json:"template_id"`
  37. URL string `json:"url"`
  38. Miniprogram struct {
  39. AppID string `json:"appid"`
  40. PagePath string `json:"pagepath"`
  41. } `json:"miniprogram"`
  42. Data map[string]struct {
  43. Value string `json:"value"`
  44. Color string `json:"color,omitempty"`
  45. } `json:"data"`
  46. } `json:"mp_template_msg"`
  47. }{}
  48. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  49. t.Fatal(err)
  50. }
  51. if params.ToUser == "" {
  52. t.Fatal("param touser can not be empty")
  53. }
  54. w.WriteHeader(http.StatusOK)
  55. raw := `{
  56. "errcode": 0,
  57. "errmsg": "ok"
  58. }`
  59. if _, err := w.Write([]byte(raw)); err != nil {
  60. t.Fatal(err)
  61. }
  62. }))
  63. defer ts.Close()
  64. sender := UniformMsgSender{
  65. ToUser: "mock-open-id",
  66. UniformWeappTmpMsg: UniformWeappTmpMsg{
  67. TemplateID: "mock-template-id",
  68. Page: "mock-page",
  69. FormID: "mock-form-id",
  70. Data: UniformMsgData{
  71. "mock-keyword": UniformMsgKeyword{Value: "mock-value"},
  72. },
  73. EmphasisKeyword: "mock-keyword.DATA",
  74. },
  75. UniformMpTmpMsg: UniformMpTmpMsg{
  76. AppID: "mock-app-id",
  77. TemplateID: "mock-template-id",
  78. URL: "mock-url",
  79. Miniprogram: UniformMsgMiniprogram{"mock-miniprogram-app-id", "mock-page-path"},
  80. Data: UniformMsgData{
  81. "mock-keyword": UniformMsgKeyword{"mock-value", "mock-color"},
  82. },
  83. },
  84. }
  85. _, err := sender.send(ts.URL+apiSendUniformMessage, "mock-access-token")
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. }