verify_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package verify
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "net/url"
  9. "testing"
  10. "time"
  11. "go-common/library/log"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/net/metadata"
  14. "go-common/library/net/netutil/breaker"
  15. xtime "go-common/library/time"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. type Response struct {
  19. Code int `json:"code"`
  20. Data string `json:"data"`
  21. }
  22. func init() {
  23. log.Init(&log.Config{
  24. Stdout: true,
  25. })
  26. }
  27. func verify() *Verify {
  28. return New(&Config{
  29. OpenServiceHost: "http://uat-open.bilibili.co",
  30. HTTPClient: &bm.ClientConfig{
  31. App: &bm.App{
  32. Key: "53e2fa226f5ad348",
  33. Secret: "3cf6bd1b0ff671021da5f424fea4b04a",
  34. },
  35. Dial: xtime.Duration(time.Second),
  36. Timeout: xtime.Duration(time.Second),
  37. KeepAlive: xtime.Duration(time.Second * 10),
  38. Breaker: &breaker.Config{
  39. Window: xtime.Duration(time.Second),
  40. Sleep: xtime.Duration(time.Millisecond * 100),
  41. Bucket: 10,
  42. Ratio: 0.5,
  43. Request: 100,
  44. },
  45. },
  46. })
  47. }
  48. func client() *bm.Client {
  49. return bm.NewClient(&bm.ClientConfig{
  50. App: &bm.App{
  51. Key: "53e2fa226f5ad348",
  52. Secret: "3cf6bd1b0ff671021da5f424fea4b04a",
  53. },
  54. Dial: xtime.Duration(time.Second),
  55. Timeout: xtime.Duration(time.Second),
  56. KeepAlive: xtime.Duration(time.Second * 10),
  57. Breaker: &breaker.Config{
  58. Window: xtime.Duration(time.Second),
  59. Sleep: xtime.Duration(time.Millisecond * 100),
  60. Bucket: 10,
  61. Ratio: 0.5,
  62. Request: 100,
  63. },
  64. })
  65. }
  66. func engine() *bm.Engine {
  67. e := bm.New()
  68. idt := verify()
  69. e.GET("/verify", idt.Verify, func(c *bm.Context) {
  70. c.JSON("pass", nil)
  71. })
  72. e.GET("/verifyUser", idt.VerifyUser, func(c *bm.Context) {
  73. mid := metadata.Int64(c, metadata.Mid)
  74. fmt.Println(mid)
  75. c.JSON(fmt.Sprintf("%d", mid), nil)
  76. })
  77. return e
  78. }
  79. func TestNewWithNilConfig(t *testing.T) {
  80. New(nil)
  81. }
  82. func TestVerifyIdentifyHandler(t *testing.T) {
  83. e := engine()
  84. go e.Run(":18080")
  85. time.Sleep(time.Second)
  86. // test cases
  87. testVerifyFailed(t)
  88. testVerifySuccess(t)
  89. testVerifyUser(t)
  90. testVerifyUserFailed(t)
  91. testVerifyUserInvalid(t)
  92. if err := e.Server().Shutdown(context.TODO()); err != nil {
  93. t.Logf("Failed to shutdown bm engine: %v", err)
  94. }
  95. }
  96. func httpGet(url string) (code int, content []byte, err error) {
  97. resp, err := http.Get(url)
  98. if err != nil {
  99. return
  100. }
  101. defer resp.Body.Close()
  102. content, err = ioutil.ReadAll(resp.Body)
  103. if err != nil {
  104. return
  105. }
  106. code = resp.StatusCode
  107. return
  108. }
  109. func testVerifyFailed(t *testing.T) {
  110. res := Response{}
  111. code, content, err := httpGet("http://127.0.0.1:18080/verify?ts=1&appkey=53e2fa226f5ad348")
  112. assert.NoError(t, err)
  113. assert.Equal(t, 200, code)
  114. err = json.Unmarshal(content, &res)
  115. assert.NoError(t, err)
  116. assert.Equal(t, -3, res.Code)
  117. }
  118. func testVerifySuccess(t *testing.T) {
  119. res := Response{}
  120. uv := url.Values{}
  121. uv.Set("appkey", "53e2fa226f5ad348")
  122. err := client().Get(context.TODO(), "http://127.0.0.1:18080/verify", "", uv, &res)
  123. assert.NoError(t, err)
  124. assert.Equal(t, 0, res.Code)
  125. assert.Equal(t, "pass", res.Data)
  126. }
  127. func testVerifyUser(t *testing.T) {
  128. res := Response{}
  129. query := url.Values{}
  130. query.Set("mid", "1")
  131. query.Set("appkey", "53e2fa226f5ad348")
  132. err := client().Get(context.TODO(), "http://127.0.0.1:18080/verifyUser", "", query, &res)
  133. assert.NoError(t, err)
  134. assert.Equal(t, 0, res.Code)
  135. assert.Equal(t, "1", res.Data)
  136. }
  137. func testVerifyUserFailed(t *testing.T) {
  138. res := Response{}
  139. code, content, err := httpGet("http://127.0.0.1:18080/verifyUser?ts=1&appkey=53e2fa226f5ad348")
  140. assert.NoError(t, err)
  141. assert.Equal(t, 200, code)
  142. err = json.Unmarshal(content, &res)
  143. assert.NoError(t, err)
  144. assert.Equal(t, -3, res.Code)
  145. }
  146. func testVerifyUserInvalid(t *testing.T) {
  147. res := Response{}
  148. query := url.Values{}
  149. query.Set("mid", "aaaa/")
  150. query.Set("appkey", "53e2fa226f5ad348")
  151. err := client().Get(context.TODO(), "http://127.0.0.1:18080/verifyUser", "", query, &res)
  152. assert.NoError(t, err)
  153. assert.Equal(t, -400, res.Code)
  154. assert.Equal(t, "", res.Data)
  155. }