proxy_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package proxy
  2. import (
  3. "bytes"
  4. "context"
  5. "net/http"
  6. "net/url"
  7. "sync"
  8. "testing"
  9. "time"
  10. "go-common/library/log"
  11. bm "go-common/library/net/http/blademaster"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func init() {
  15. log.Init(nil)
  16. }
  17. func TestProxy(t *testing.T) {
  18. engine := bm.Default()
  19. engine.GET("/icon", NewAlways("http://api.bilibili.com/x/web-interface/index/icon"))
  20. engine.POST("/x/web-interface/archive/like", NewAlways("http://api.bilibili.com"))
  21. go engine.Run(":18080")
  22. defer func() {
  23. engine.Server().Shutdown(context.TODO())
  24. }()
  25. time.Sleep(time.Second)
  26. req, err := http.NewRequest("GET", "http://127.0.0.1:18080/icon", nil)
  27. assert.NoError(t, err)
  28. req.Host = "api.bilibili.com"
  29. resp, err := http.DefaultClient.Do(req)
  30. assert.NoError(t, err)
  31. defer resp.Body.Close()
  32. assert.Equal(t, 200, resp.StatusCode)
  33. // proxy form request
  34. form := url.Values{}
  35. form.Set("arg1", "1")
  36. form.Set("arg2", "2")
  37. req, err = http.NewRequest("POST", "http://127.0.0.1:18080/x/web-interface/archive/like?param=test", bytes.NewReader([]byte(form.Encode())))
  38. assert.NoError(t, err)
  39. req.Host = "api.bilibili.com"
  40. resp, err = http.DefaultClient.Do(req)
  41. assert.NoError(t, err)
  42. defer resp.Body.Close()
  43. assert.Equal(t, 200, resp.StatusCode)
  44. // proxy json request
  45. bs := []byte(`{"arg1": 1, "arg2": 2}`)
  46. req, err = http.NewRequest("POST", "http://127.0.0.1:18080/x/web-interface/archive/like?param=test", bytes.NewReader(bs))
  47. assert.NoError(t, err)
  48. req.Host = "api.bilibili.com"
  49. req.Header.Set("Content-Type", "application/json; charset=utf-8")
  50. resp, err = http.DefaultClient.Do(req)
  51. assert.NoError(t, err)
  52. defer resp.Body.Close()
  53. assert.Equal(t, 200, resp.StatusCode)
  54. }
  55. func TestProxyRace(t *testing.T) {
  56. engine := bm.Default()
  57. engine.GET("/icon", NewAlways("http://api.bilibili.com/x/web-interface/index/icon"))
  58. go engine.Run(":18080")
  59. defer func() {
  60. engine.Server().Shutdown(context.TODO())
  61. }()
  62. time.Sleep(time.Second)
  63. wg := sync.WaitGroup{}
  64. for i := 0; i < 20; i++ {
  65. wg.Add(1)
  66. go func() {
  67. defer wg.Done()
  68. req, err := http.NewRequest("GET", "http://127.0.0.1:18080/icon", nil)
  69. assert.NoError(t, err)
  70. req.Host = "api.bilibili.com"
  71. resp, err := http.DefaultClient.Do(req)
  72. assert.NoError(t, err)
  73. defer resp.Body.Close()
  74. assert.Equal(t, 200, resp.StatusCode)
  75. }()
  76. }
  77. wg.Wait()
  78. }
  79. func TestZoneProxy(t *testing.T) {
  80. engine := bm.Default()
  81. engine.GET("/icon", NewZoneProxy("sh004", "http://api.bilibili.com/x/web-interface/index/icon"), func(ctx *bm.Context) {
  82. ctx.AbortWithStatus(500)
  83. })
  84. engine.GET("/icon2", NewZoneProxy("none", "http://api.bilibili.com/x/web-interface/index/icon2"), func(ctx *bm.Context) {
  85. ctx.AbortWithStatus(200)
  86. })
  87. ug := engine.Group("/update", NewZoneProxy("sh004", "http://api.bilibili.com"))
  88. ug.POST("/name", func(ctx *bm.Context) {
  89. ctx.AbortWithStatus(500)
  90. })
  91. ug.POST("/sign", func(ctx *bm.Context) {
  92. ctx.AbortWithStatus(500)
  93. })
  94. go engine.Run(":18080")
  95. defer func() {
  96. engine.Server().Shutdown(context.TODO())
  97. }()
  98. time.Sleep(time.Second)
  99. req, err := http.NewRequest("GET", "http://127.0.0.1:18080/icon", nil)
  100. assert.NoError(t, err)
  101. req.Host = "api.bilibili.com"
  102. req.Header.Set("X-BILI-SLB", "shjd-out-slb")
  103. resp, err := http.DefaultClient.Do(req)
  104. assert.NoError(t, err)
  105. defer resp.Body.Close()
  106. assert.Equal(t, 200, resp.StatusCode)
  107. req.URL.Path = "/icon2"
  108. resp, err = http.DefaultClient.Do(req)
  109. assert.NoError(t, err)
  110. defer resp.Body.Close()
  111. assert.Equal(t, 200, resp.StatusCode)
  112. req.URL.Path = "/update/name"
  113. resp, err = http.DefaultClient.Do(req)
  114. assert.NoError(t, err)
  115. defer resp.Body.Close()
  116. assert.Equal(t, 200, resp.StatusCode)
  117. req.URL.Path = "/update/sign"
  118. resp, err = http.DefaultClient.Do(req)
  119. assert.NoError(t, err)
  120. defer resp.Body.Close()
  121. assert.Equal(t, 200, resp.StatusCode)
  122. }
  123. func BenchmarkProxy(b *testing.B) {
  124. engine := bm.Default()
  125. engine.GET("/icon", NewAlways("http://api.bilibili.com/x/web-interface/index/icon"))
  126. go engine.Run(":18080")
  127. defer func() {
  128. engine.Server().Shutdown(context.TODO())
  129. }()
  130. time.Sleep(time.Second)
  131. b.ResetTimer()
  132. b.RunParallel(func(pb *testing.PB) {
  133. for pb.Next() {
  134. req, err := http.NewRequest("GET", "http://127.0.0.1:18080/icon", nil)
  135. assert.NoError(b, err)
  136. req.Host = "api.bilibili.com"
  137. resp, err := http.DefaultClient.Do(req)
  138. assert.NoError(b, err)
  139. defer resp.Body.Close()
  140. assert.Equal(b, 200, resp.StatusCode)
  141. }
  142. })
  143. }