golang-im聊天
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.
 
 
 
 

209 lines
4.3 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("111.229.238.28:50000", 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 := "0"
  25. return metadata.NewOutgoingContext(context.TODO(), metadata.Pairs(
  26. "user_id", "2",
  27. "device_id", "1",
  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 alber ",
  49. })
  50. if err != nil {
  51. fmt.Println(err)
  52. return
  53. }
  54. resp, err := getLogicExtClient().SendMessage(getCtx(),
  55. &pb.SendMessageReq{
  56. ReceiverType: pb.ReceiverType_RT_USER,
  57. ReceiverId: 1,
  58. ToUserIds: nil,
  59. MessageType: pb.MessageType_MT_TEXT,
  60. MessageContent: buf,
  61. IsPersist: true,
  62. SendTime: util.UnixMilliTime(time.Now()),
  63. })
  64. if err != nil {
  65. fmt.Println(err)
  66. return
  67. }
  68. fmt.Printf("%+v\n", resp)
  69. }
  70. func TestLogicExtServer_SendImageMessage(t *testing.T) {
  71. buf, err := proto.Marshal(&pb.Image{
  72. Id: "",
  73. Width: 0,
  74. Height: 0,
  75. Url: "https://img.iplaysoft.com/wp-content/uploads/2019/free-images/free_stock_photo.jpg",
  76. ThumbnailUrl: "",
  77. })
  78. if err != nil {
  79. fmt.Println(err)
  80. return
  81. }
  82. resp, err := getLogicExtClient().SendMessage(getCtx(),
  83. &pb.SendMessageReq{
  84. ReceiverType: pb.ReceiverType_RT_USER,
  85. ReceiverId: 1,
  86. ToUserIds: nil,
  87. MessageType: pb.MessageType_MT_IMAGE,
  88. MessageContent: buf,
  89. IsPersist: true,
  90. SendTime: util.UnixMilliTime(time.Now()),
  91. })
  92. if err != nil {
  93. fmt.Println(err)
  94. return
  95. }
  96. fmt.Printf("%+v\n", resp)
  97. }
  98. func TestLogicExtServer_CreateGroup(t *testing.T) {
  99. resp, err := getLogicExtClient().CreateGroup(getCtx(),
  100. &pb.CreateGroupReq{
  101. Name: "10",
  102. Introduction: "10",
  103. Extra: "10",
  104. })
  105. if err != nil {
  106. fmt.Println(err)
  107. return
  108. }
  109. fmt.Printf("%+v\n", resp)
  110. }
  111. func TestLogicExtServer_UpdateGroup(t *testing.T) {
  112. resp, err := getLogicExtClient().UpdateGroup(getCtx(),
  113. &pb.UpdateGroupReq{
  114. GroupId: 2,
  115. Name: "11",
  116. Introduction: "11",
  117. Extra: "11",
  118. })
  119. if err != nil {
  120. fmt.Println(err)
  121. return
  122. }
  123. fmt.Printf("%+v\n", resp)
  124. }
  125. func TestLogicExtServer_GetGroup(t *testing.T) {
  126. resp, err := getLogicExtClient().GetGroup(getCtx(),
  127. &pb.GetGroupReq{
  128. GroupId: 2,
  129. })
  130. if err != nil {
  131. fmt.Println(err)
  132. return
  133. }
  134. fmt.Printf("%+v\n", resp)
  135. }
  136. func TestLogicExtServer_GetUserGroups(t *testing.T) {
  137. resp, err := getLogicExtClient().GetGroups(getCtx(), &pb.Empty{})
  138. if err != nil {
  139. fmt.Println(err)
  140. return
  141. }
  142. // todo 不能获取用户所在的超大群组
  143. fmt.Printf("%+v\n", resp)
  144. }
  145. func TestLogicExtServer_AddGroupMember(t *testing.T) {
  146. resp, err := getLogicExtClient().AddGroupMembers(getCtx(),
  147. &pb.AddGroupMembersReq{
  148. GroupId: 2,
  149. })
  150. if err != nil {
  151. fmt.Println(err)
  152. return
  153. }
  154. fmt.Printf("%+v\n", resp)
  155. }
  156. func TestLogicExtServer_UpdateGroupMember(t *testing.T) {
  157. resp, err := getLogicExtClient().UpdateGroupMember(getCtx(),
  158. &pb.UpdateGroupMemberReq{
  159. GroupId: 2,
  160. UserId: 3,
  161. Remarks: "2",
  162. Extra: "2",
  163. })
  164. if err != nil {
  165. fmt.Println(err)
  166. return
  167. }
  168. fmt.Printf("%+v\n", resp)
  169. }
  170. func TestLogicExtServer_DeleteGroupMember(t *testing.T) {
  171. resp, err := getLogicExtClient().DeleteGroupMember(getCtx(),
  172. &pb.DeleteGroupMemberReq{
  173. GroupId: 10,
  174. UserId: 1,
  175. })
  176. if err != nil {
  177. fmt.Println(err)
  178. return
  179. }
  180. fmt.Printf("%+v\n", resp)
  181. }
  182. func TestLogicExtServer_GetGroupMembers(t *testing.T) {
  183. resp, err := getLogicExtClient().GetGroupMembers(getCtx(),
  184. &pb.GetGroupMembersReq{
  185. GroupId: 2,
  186. })
  187. if err != nil {
  188. fmt.Println(err)
  189. return
  190. }
  191. fmt.Printf("%+v\n", resp)
  192. }