http_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package http
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "encoding/json"
  7. "flag"
  8. "io/ioutil"
  9. "net/http"
  10. "net/url"
  11. "path/filepath"
  12. "strconv"
  13. "strings"
  14. "testing"
  15. "time"
  16. "go-common/app/admin/main/up/conf"
  17. "github.com/davecgh/go-spew/spew"
  18. . "github.com/smartystreets/goconvey/convey"
  19. )
  20. func init() {
  21. dir, _ := filepath.Abs("../cmd/up-admin.toml")
  22. flag.Set("conf", dir)
  23. conf.Init()
  24. // Init(conf.Conf)
  25. time.Sleep(time.Second)
  26. }
  27. // Sign fn
  28. func Sign(params url.Values) (sign string) {
  29. secret := params.Get("appsecret")
  30. params.Del("appsecret")
  31. tmp := params.Encode()
  32. if strings.IndexByte(tmp, '+') > -1 {
  33. tmp = strings.Replace(tmp, "+", "%20", -1)
  34. }
  35. mh := md5.Sum([]byte(tmp + secret))
  36. sign = hex.EncodeToString(mh[:])
  37. return
  38. }
  39. var (
  40. err error
  41. req *http.Request
  42. resp *http.Response
  43. HOST = "http://localhost:7441"
  44. URI = "/x/internal/up/register"
  45. infoURI = "/x/internal/up/info"
  46. c = context.Background()
  47. client = &http.Client{
  48. Timeout: time.Duration(time.Second * 2),
  49. }
  50. )
  51. func Test_Up(t *testing.T) {
  52. Convey("register", t, func() {
  53. params := url.Values{}
  54. params.Set("mid", strconv.FormatInt(2089809, 10))
  55. params.Set("from", strconv.FormatInt(0, 10))
  56. params.Set("is_author", strconv.FormatInt(0, 10))
  57. params.Set("appkey", conf.Conf.App.Key)
  58. params.Set("appsecret", conf.Conf.App.Secret)
  59. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  60. sign := Sign(params)
  61. params.Set("sign", sign)
  62. u, _ := url.ParseRequestURI(HOST)
  63. u.Path = URI
  64. url := u.String()
  65. req, err = http.NewRequest("POST", url, strings.NewReader(params.Encode()))
  66. So(err, ShouldBeNil)
  67. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  68. // timeout
  69. ctx, cancel := context.WithTimeout(c, time.Second*2)
  70. req = req.WithContext(ctx)
  71. defer cancel()
  72. resp, err = client.Do(req)
  73. So(err, ShouldBeNil)
  74. body, err1 := ioutil.ReadAll(resp.Body)
  75. err = err1
  76. So(err, ShouldBeNil)
  77. defer resp.Body.Close()
  78. var result struct {
  79. Code int `json:"code"`
  80. Message string `json:"message"`
  81. Data struct {
  82. Result bool `json:"result"`
  83. } `json:"data"`
  84. }
  85. spew.Dump(string(body))
  86. json.Unmarshal(body, &result)
  87. So(result, ShouldNotBeNil)
  88. So(result.Data.Result, ShouldBeTrue)
  89. })
  90. Convey("info", t, func() {
  91. params := url.Values{}
  92. params.Set("mid", strconv.FormatInt(2089809, 10))
  93. params.Set("from", strconv.FormatInt(1, 10))
  94. params.Set("appkey", conf.Conf.App.Key)
  95. params.Set("appsecret", conf.Conf.App.Secret)
  96. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  97. sign := Sign(params)
  98. params.Set("sign", sign)
  99. u, _ := url.ParseRequestURI(HOST)
  100. u.Path = infoURI
  101. url := u.String()
  102. reqURL := url + "?" + params.Encode()
  103. req, err = http.NewRequest("GET", reqURL, nil)
  104. So(err, ShouldBeNil)
  105. // timeout
  106. ctx, cancel := context.WithTimeout(c, time.Second*2)
  107. req = req.WithContext(ctx)
  108. defer cancel()
  109. resp, err = client.Do(req)
  110. So(err, ShouldBeNil)
  111. body, err := ioutil.ReadAll(resp.Body)
  112. So(err, ShouldBeNil)
  113. defer resp.Body.Close()
  114. var result struct {
  115. Code int `json:"code"`
  116. Message string `json:"message"`
  117. Data struct {
  118. IsAuthor bool `json:"is_author"`
  119. } `json:"data"`
  120. }
  121. spew.Dump(string(body))
  122. json.Unmarshal(body, &result)
  123. So(result, ShouldNotBeNil)
  124. })
  125. }