record_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package service
  2. import (
  3. "context"
  4. "crypto/sha1"
  5. "encoding/hex"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "testing"
  10. "time"
  11. "go-common/app/interface/main/upload/conf"
  12. "go-common/app/interface/main/upload/model"
  13. . "github.com/smartystreets/goconvey/convey"
  14. )
  15. func loadbs() []byte {
  16. client := &http.Client{}
  17. resp, err := client.Get("https://i0.hdslb.com/bfs/album/b11defd6410e9fa5b6962c3c5f0402be2608db8c.jpg")
  18. if err != nil {
  19. panic(err)
  20. }
  21. defer resp.Body.Close()
  22. bs, err := ioutil.ReadAll(resp.Body)
  23. if err != nil {
  24. panic(err)
  25. }
  26. return bs
  27. }
  28. func TestGenImageUpload(t *testing.T) {
  29. Convey("create image and upload it", t, func() {
  30. res, err := svr.GenImageUpload(context.TODO(), "b4cfeeadca80f6f5", "c605dd5324f91ea1", "hello world", 2, true)
  31. So(err, ShouldBeNil)
  32. t.Logf("result:%+v", res)
  33. })
  34. }
  35. func TestUpload(t *testing.T) {
  36. bs := loadbs()
  37. Convey("test Upload", t, func() {
  38. now := time.Now().Unix()
  39. sha1 := sha1.New()
  40. sha1.Write([]byte(fmt.Sprintf("i love bilibili %s:%d", conf.Conf.Auths[0].AppSercet, now)))
  41. token := fmt.Sprintf("%s:%d", hex.EncodeToString(sha1.Sum([]byte(""))), now)
  42. result, err := svr.Upload(context.Background(), conf.Conf.Auths[0].AppKey, token, "", bs)
  43. if err != nil {
  44. t.Fatal(err.Error())
  45. }
  46. So("54aeb138b7fea2fe812aa8548f96cf1c0e4596ff", ShouldResemble, result.Etag)
  47. })
  48. }
  49. func TestUploadRecord(t *testing.T) {
  50. bs := loadbs()
  51. Convey("test UploadRecord", t, func() {
  52. ap := &model.UploadParam{
  53. ContentType: "image/jpeg",
  54. Bucket: "static",
  55. FileName: "",
  56. Dir: "",
  57. }
  58. result, err := svr.UploadRecord(context.Background(), model.UploadInternal, 11, ap, bs)
  59. if err != nil {
  60. t.Fatal(err.Error())
  61. }
  62. So("54aeb138b7fea2fe812aa8548f96cf1c0e4596ff", ShouldResemble, result.Etag)
  63. })
  64. }