ele_client_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package client
  2. import (
  3. "bytes"
  4. "context"
  5. "testing"
  6. "time"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/netutil/breaker"
  9. xtime "go-common/library/time"
  10. "github.com/smartystreets/goconvey/convey"
  11. )
  12. var cl *EleClient
  13. func TestNewEleClient(t *testing.T) {
  14. var c = &Config{
  15. App: &App{
  16. Key: "sdfsdf",
  17. Secret: "sdfsdf",
  18. },
  19. }
  20. var client = bm.NewClient(&bm.ClientConfig{
  21. App: &bm.App{
  22. Key: "53e2fa226f5ad348",
  23. Secret: "3cf6bd1b0ff671021da5f424fea4b04a",
  24. },
  25. Dial: xtime.Duration(time.Second),
  26. Timeout: xtime.Duration(time.Second),
  27. KeepAlive: xtime.Duration(time.Second),
  28. Breaker: &breaker.Config{
  29. Window: 10 * xtime.Duration(time.Second),
  30. Sleep: 50 * xtime.Duration(time.Millisecond),
  31. Bucket: 10,
  32. Ratio: 0.5,
  33. Request: 100,
  34. },
  35. },
  36. )
  37. convey.Convey("NewEleClient", t, func() {
  38. cl = NewEleClient(c, client)
  39. convey.So(cl, convey.ShouldNotBeNil)
  40. })
  41. convey.Convey("Get", t, func() {
  42. err := cl.Get(context.TODO(), "http://api.bilibili.co", "/x/internal/vip/user/info", nil, nil)
  43. convey.So(err, convey.ShouldBeNil)
  44. })
  45. convey.Convey("Post", t, func() {
  46. err := cl.Post(context.TODO(), "http://api.bilibili.co", "/x/internal/vip/order/create", nil, nil)
  47. convey.So(err, convey.ShouldBeNil)
  48. })
  49. convey.Convey("newRequest", t, func() {
  50. req, err := cl.newRequest("POST", "http://api.bilibili.co", "/x/internal/vip/user/info", nil)
  51. convey.So(err, convey.ShouldBeNil)
  52. convey.So(req, convey.ShouldNotBeNil)
  53. })
  54. }
  55. func TestIsSuccess(t *testing.T) {
  56. convey.Convey("IsSuccess", t, func() {
  57. p1 := IsSuccess("ok")
  58. convey.So(p1, convey.ShouldNotBeNil)
  59. })
  60. }
  61. func TestEleSign(t *testing.T) {
  62. convey.Convey("eleSign", t, func() {
  63. p1 := eleSign("", "", "", "", "")
  64. convey.So(p1, convey.ShouldNotBeNil)
  65. })
  66. }
  67. func TestComputeHmac256(t *testing.T) {
  68. convey.Convey("computeHmac256", t, func() {
  69. var b bytes.Buffer
  70. b.WriteString("http://bilibili.com/x/vip")
  71. b.WriteString("&")
  72. b.WriteString("consumer_key=")
  73. p1 := computeHmac256(b, "xxx")
  74. convey.So(p1, convey.ShouldNotBeNil)
  75. })
  76. }
  77. func TestUUID4(t *testing.T) {
  78. convey.Convey("UUID4", t, func() {
  79. p1 := UUID4()
  80. convey.So(p1, convey.ShouldNotBeNil)
  81. })
  82. }