golang-im聊天
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

211 行
4.4 KiB

  1. package api
  2. import (
  3. "context"
  4. "fmt"
  5. "gim/pkg/pb"
  6. "gim/pkg/util"
  7. "strconv"
  8. "testing"
  9. "time"
  10. "google.golang.org/grpc"
  11. "google.golang.org/grpc/metadata"
  12. "google.golang.org/protobuf/proto"
  13. )
  14. func getLogicExtClient() pb.LogicExtClient {
  15. conn, err := grpc.Dial("im-rpc-logic.izhyin.com:8003", grpc.WithInsecure())
  16. if err != nil {
  17. fmt.Println(err)
  18. return nil
  19. }
  20. return pb.NewLogicExtClient(conn)
  21. }
  22. // deprecated:
  23. func getCtx() context.Context {
  24. token := "VRZWQVPFXHPUCCKFOANEYOLCDSFMKEAVYVMAZQWQ"
  25. return metadata.NewOutgoingContext(context.TODO(), metadata.Pairs(
  26. "user_id", "2",
  27. "device_id", "2",
  28. "token", token,
  29. "request_id", strconv.FormatInt(time.Now().UnixNano(), 10)))
  30. }
  31. func TestLogicExtServer_RegisterDevice(t *testing.T) {
  32. resp, err := getLogicExtClient().RegisterDevice(context.TODO(),
  33. &pb.RegisterDeviceReq{
  34. Type: 1,
  35. Brand: "huawei",
  36. Model: "huawei P30",
  37. SystemVersion: "1.0.0",
  38. SdkVersion: "1.0.0",
  39. })
  40. if err != nil {
  41. fmt.Println(err)
  42. return
  43. }
  44. fmt.Printf("%+v\n", resp)
  45. }
  46. func TestLogicExtServer_SendMessage(t *testing.T) {
  47. buf, err := proto.Marshal(&pb.Text{
  48. Text: "hello world!",
  49. })
  50. if err != nil {
  51. fmt.Println(err)
  52. return
  53. }
  54. var one = string(buf)
  55. fmt.Printf(one)
  56. resp, err := getLogicExtClient().SendMessage(getCtx(),
  57. &pb.SendMessageReq{
  58. ReceiverType: pb.ReceiverType_RT_USER,
  59. ReceiverId: 1,
  60. ToUserIds: nil,
  61. MessageType: pb.MessageType_MT_TEXT,
  62. MessageContent: buf,
  63. IsPersist: true,
  64. SendTime: util.UnixMilliTime(time.Now()),
  65. })
  66. if err != nil {
  67. fmt.Println(err)
  68. return
  69. }
  70. fmt.Printf("%+v\n", resp)
  71. }
  72. func TestLogicExtServer_SendImageMessage(t *testing.T) {
  73. buf, err := proto.Marshal(&pb.Image{
  74. Id: "",
  75. Width: 0,
  76. Height: 0,
  77. Url: "https://img.iplaysoft.com/wp-content/uploads/2019/free-images/free_stock_photo.jpg",
  78. ThumbnailUrl: "",
  79. })
  80. if err != nil {
  81. fmt.Println(err)
  82. return
  83. }
  84. resp, err := getLogicExtClient().SendMessage(getCtx(),
  85. &pb.SendMessageReq{
  86. ReceiverType: pb.ReceiverType_RT_USER,
  87. ReceiverId: 1,
  88. ToUserIds: nil,
  89. MessageType: pb.MessageType_MT_IMAGE,
  90. MessageContent: buf,
  91. IsPersist: true,
  92. SendTime: util.UnixMilliTime(time.Now()),
  93. })
  94. if err != nil {
  95. fmt.Println(err)
  96. return
  97. }
  98. fmt.Printf("%+v\n", resp)
  99. }
  100. func TestLogicExtServer_CreateGroup(t *testing.T) {
  101. resp, err := getLogicExtClient().CreateGroup(getCtx(),
  102. &pb.CreateGroupReq{
  103. Name: "10",
  104. Introduction: "10",
  105. Extra: "10",
  106. })
  107. if err != nil {
  108. fmt.Println(err)
  109. return
  110. }
  111. fmt.Printf("%+v\n", resp)
  112. }
  113. func TestLogicExtServer_UpdateGroup(t *testing.T) {
  114. resp, err := getLogicExtClient().UpdateGroup(getCtx(),
  115. &pb.UpdateGroupReq{
  116. GroupId: 2,
  117. Name: "11",
  118. Introduction: "11",
  119. Extra: "11",
  120. })
  121. if err != nil {
  122. fmt.Println(err)
  123. return
  124. }
  125. fmt.Printf("%+v\n", resp)
  126. }
  127. func TestLogicExtServer_GetGroup(t *testing.T) {
  128. resp, err := getLogicExtClient().GetGroup(getCtx(),
  129. &pb.GetGroupReq{
  130. GroupId: 2,
  131. })
  132. if err != nil {
  133. fmt.Println(err)
  134. return
  135. }
  136. fmt.Printf("%+v\n", resp)
  137. }
  138. func TestLogicExtServer_GetUserGroups(t *testing.T) {
  139. resp, err := getLogicExtClient().GetGroups(getCtx(), &pb.Empty{})
  140. if err != nil {
  141. fmt.Println(err)
  142. return
  143. }
  144. // todo 不能获取用户所在的超大群组
  145. fmt.Printf("%+v\n", resp)
  146. }
  147. func TestLogicExtServer_AddGroupMember(t *testing.T) {
  148. resp, err := getLogicExtClient().AddGroupMembers(getCtx(),
  149. &pb.AddGroupMembersReq{
  150. GroupId: 2,
  151. })
  152. if err != nil {
  153. fmt.Println(err)
  154. return
  155. }
  156. fmt.Printf("%+v\n", resp)
  157. }
  158. func TestLogicExtServer_UpdateGroupMember(t *testing.T) {
  159. resp, err := getLogicExtClient().UpdateGroupMember(getCtx(),
  160. &pb.UpdateGroupMemberReq{
  161. GroupId: 2,
  162. UserId: 3,
  163. Remarks: "2",
  164. Extra: "2",
  165. })
  166. if err != nil {
  167. fmt.Println(err)
  168. return
  169. }
  170. fmt.Printf("%+v\n", resp)
  171. }
  172. func TestLogicExtServer_DeleteGroupMember(t *testing.T) {
  173. resp, err := getLogicExtClient().DeleteGroupMember(getCtx(),
  174. &pb.DeleteGroupMemberReq{
  175. GroupId: 10,
  176. UserId: 1,
  177. })
  178. if err != nil {
  179. fmt.Println(err)
  180. return
  181. }
  182. fmt.Printf("%+v\n", resp)
  183. }
  184. func TestLogicExtServer_GetGroupMembers(t *testing.T) {
  185. resp, err := getLogicExtClient().GetGroupMembers(getCtx(),
  186. &pb.GetGroupMembersReq{
  187. GroupId: 2,
  188. })
  189. if err != nil {
  190. fmt.Println(err)
  191. return
  192. }
  193. fmt.Printf("%+v\n", resp)
  194. }