http_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package http
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "sync"
  9. "testing"
  10. "go-common/app/service/live/userexp/conf"
  11. "go-common/app/service/live/userexp/model"
  12. "go-common/app/service/live/userexp/service"
  13. httpx "go-common/library/net/http/blademaster"
  14. )
  15. const (
  16. _getLevelURL = "http://localhost:8801/x/internal/liveexp/level/get"
  17. _multiGetLevelURL = "http://localhost:8801/x/internal/liveexp/level/mulGet"
  18. _addUexpURL = "http://localhost:8801/x/internal/liveexp/level/addUexp"
  19. _addRexpURL = "http://localhost:8801/x/internal/liveexp/level/addRexp"
  20. )
  21. var (
  22. once sync.Once
  23. client *httpx.Client
  24. )
  25. func startHTTP() {
  26. if err := conf.Init(); err != nil {
  27. panic(fmt.Errorf("conf.Init() error(%v)", err))
  28. }
  29. svr := service.New(conf.Conf)
  30. client = httpx.NewClient(conf.Conf.HTTPClient.Read)
  31. Init(conf.Conf, svr)
  32. }
  33. func TestGetLevel(t *testing.T) {
  34. once.Do(startHTTP)
  35. params := url.Values{}
  36. params.Set("uid", "10001")
  37. var err error
  38. var req *http.Request
  39. if req, err = client.NewRequest("GET", _getLevelURL, "127.0.0.1", params); err != nil {
  40. t.Errorf("http.NewRequest(GET, %s) error(%v)", _getLevelURL, err)
  41. t.FailNow()
  42. }
  43. reqURI := req.URL.String()
  44. t.Logf("req uri: %s\n", reqURI)
  45. var res struct {
  46. Code int `json:"code"`
  47. Data *model.Level `json:"data"`
  48. }
  49. if err = client.Do(context.TODO(), req, &res); err != nil {
  50. t.Errorf("client.Do() error(%v)", err)
  51. t.FailNow()
  52. }
  53. str, _ := json.Marshal(res)
  54. t.Logf("res: %+v", string(str))
  55. }
  56. func TestMultiGetLevel(t *testing.T) {
  57. once.Do(startHTTP)
  58. params := url.Values{}
  59. params.Set("uids", "10001,10002,10003")
  60. var err error
  61. var req *http.Request
  62. if req, err = client.NewRequest("GET", _multiGetLevelURL, "127.0.0.1", params); err != nil {
  63. t.Errorf("http.NewRequest(GET, %s) error(%v)", _multiGetLevelURL, err)
  64. t.FailNow()
  65. }
  66. reqURI := req.URL.String()
  67. t.Logf("req uri: %s\n", reqURI)
  68. var res struct {
  69. Code int `json:"code"`
  70. Data map[string]*model.Level `json:"data"`
  71. }
  72. if err = client.Do(context.TODO(), req, &res); err != nil {
  73. t.Errorf("client.Do() error(%v)", err)
  74. t.FailNow()
  75. }
  76. str, _ := json.Marshal(res)
  77. t.Logf("res: %+v", string(str))
  78. }
  79. func TestAddUexp(t *testing.T) {
  80. once.Do(startHTTP)
  81. params := url.Values{}
  82. params.Set("uid", "10001")
  83. params.Set("uexp", "1234")
  84. var err error
  85. var req *http.Request
  86. if req, err = client.NewRequest("GET", _addUexpURL, "127.0.0.1", params); err != nil {
  87. t.Errorf("http.NewRequest(GET, %s) error(%v)", _addUexpURL, err)
  88. t.FailNow()
  89. }
  90. reqURI := req.URL.String()
  91. t.Logf("req uri: %s\n", reqURI)
  92. var res struct {
  93. Code int `json:"code"`
  94. Data string `json:"data"`
  95. }
  96. if err = client.Do(context.TODO(), req, &res); err != nil {
  97. t.Errorf("client.Do() error(%v)", err)
  98. t.FailNow()
  99. }
  100. str, _ := json.Marshal(res)
  101. t.Logf("res: %+v", string(str))
  102. }
  103. func TestAddRexp(t *testing.T) {
  104. once.Do(startHTTP)
  105. params := url.Values{}
  106. params.Set("uid", "10001")
  107. params.Set("rexp", "4321")
  108. var err error
  109. var req *http.Request
  110. if req, err = client.NewRequest("GET", _addRexpURL, "127.0.0.1", params); err != nil {
  111. t.Errorf("http.NewRequest(GET, %s) error(%v)", _addRexpURL, err)
  112. t.FailNow()
  113. }
  114. reqURI := req.URL.String()
  115. t.Logf("req uri: %s\n", reqURI)
  116. var res struct {
  117. Code int `json:"code"`
  118. Data string `json:"data"`
  119. }
  120. if err = client.Do(context.TODO(), req, &res); err != nil {
  121. t.Errorf("client.Do() error(%v)", err)
  122. t.FailNow()
  123. }
  124. str, _ := json.Marshal(res)
  125. t.Logf("res: %+v", string(str))
  126. }