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

updatable_message_test.go 2.8 KiB

2 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package weapp
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. )
  8. func TestCreateActivityID(t *testing.T) {
  9. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  10. if r.Method != "GET" {
  11. t.Fatalf("Expect 'GET' get '%s'", r.Method)
  12. }
  13. realPath := r.URL.EscapedPath()
  14. expectPath := "/cgi-bin/message/wxopen/activityid/create"
  15. if realPath != expectPath {
  16. t.Fatalf("Expect to path '%s',get '%s'", expectPath, realPath)
  17. }
  18. if err := r.ParseForm(); err != nil {
  19. t.Fatal(err)
  20. }
  21. if r.Form.Get("access_token") == "" {
  22. t.Fatalf("access_token can not be empty")
  23. }
  24. w.WriteHeader(http.StatusOK)
  25. raw := `{
  26. "expiration_time": 1000,
  27. "activity_id": "ok",
  28. "errcode": 0,
  29. "errmsg": "ok"
  30. }`
  31. if _, err := w.Write([]byte(raw)); err != nil {
  32. t.Fatal(err)
  33. }
  34. }))
  35. defer ts.Close()
  36. res, err := createActivityID(ts.URL+apiCreateActivityID, "mock-access-token")
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. if res.ActivityID == "" {
  41. t.Error("Response column activity_id can not be empty")
  42. }
  43. if res.ExpirationTime == 0 {
  44. t.Error("Response column expiration_time can not be zero")
  45. }
  46. }
  47. func TestSetUpdatableMsg(t *testing.T) {
  48. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  49. if r.Method != "POST" {
  50. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  51. }
  52. realPath := r.URL.EscapedPath()
  53. expectPath := "/cgi-bin/message/wxopen/updatablemsg/send"
  54. if realPath != expectPath {
  55. t.Fatalf("Expect to path '%s',get '%s'", expectPath, realPath)
  56. }
  57. if err := r.ParseForm(); err != nil {
  58. t.Fatal(err)
  59. }
  60. if r.Form.Get("access_token") == "" {
  61. t.Fatalf("access_token can not be empty")
  62. }
  63. params := struct {
  64. ActivityID string `json:"activity_id"`
  65. TargetState uint8 `json:"target_state"`
  66. TemplateInfo struct {
  67. ParameterList []struct {
  68. Name string `json:"name"`
  69. Value string `json:"value"`
  70. } `json:"parameter_list"`
  71. } `json:"template_info"`
  72. }{}
  73. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  74. t.Fatal(err)
  75. }
  76. if params.ActivityID == "" {
  77. t.Fatal("param activity_id can not be empty")
  78. }
  79. if len(params.TemplateInfo.ParameterList) == 0 {
  80. t.Fatal("param template_info.parameter_list can not be empty")
  81. }
  82. w.WriteHeader(http.StatusOK)
  83. raw := `{
  84. "errcode": 0,
  85. "errmsg": "ok"
  86. }`
  87. if _, err := w.Write([]byte(raw)); err != nil {
  88. t.Fatal(err)
  89. }
  90. }))
  91. defer ts.Close()
  92. setter := UpdatableMsgSetter{
  93. "mock-activity-id",
  94. UpdatableMsgJoining,
  95. UpdatableMsgTempInfo{
  96. []UpdatableMsgParameter{
  97. {UpdatableMsgParamMemberCount, "mock-parameter-value-number"},
  98. {UpdatableMsgParamRoomLimit, "mock-parameter-value-number"},
  99. },
  100. },
  101. }
  102. _, err := setter.set(ts.URL+apiSetUpdatableMsg, "mock-access-token")
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. }