upbfs_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "time"
  7. "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestDaoUpload(t *testing.T) {
  10. var (
  11. c = context.Background()
  12. fileName = "test.txt"
  13. fileType = "txt"
  14. timing = time.Now().Unix()
  15. bfs = d.c.Bfs
  16. data = []byte("123")
  17. url = fmt.Sprintf(bfs.Host+_uploadURL, bfs.Bucket, fileName)
  18. )
  19. convey.Convey("Upload", t, func(ctx convey.C) {
  20. ctx.Convey("http code error", func(ctx convey.C) {
  21. httpMock(_method, url).Reply(-400)
  22. _, err := d.Upload(c, fileName, fileType, timing, data)
  23. ctx.So(err, convey.ShouldNotBeNil)
  24. })
  25. ctx.Convey("business code error", func(ctx convey.C) {
  26. httpMock(_method, url).Reply(200).JSON(`{"code":-400}`)
  27. _, err := d.Upload(c, fileName, fileType, timing, data)
  28. ctx.So(err, convey.ShouldNotBeNil)
  29. })
  30. ctx.Convey("everything is fine", func(ctx convey.C) {
  31. httpMock(_method, url).Reply(200).SetHeader("Location", "test").SetHeader("code", "200").JSON(`{"code":200}`)
  32. location, err := d.Upload(c, fileName, fileType, timing, data)
  33. ctx.So(err, convey.ShouldBeNil)
  34. ctx.So(location, convey.ShouldNotBeNil)
  35. })
  36. })
  37. }
  38. func TestDaoauthorize(t *testing.T) {
  39. var (
  40. key = "key"
  41. secret = "secret"
  42. method = "put"
  43. bucket = "tv-cover"
  44. file = "file"
  45. expire = int64(0)
  46. )
  47. convey.Convey("authorize", t, func(ctx convey.C) {
  48. authorization := authorize(key, secret, method, bucket, file, expire)
  49. ctx.Convey("Then authorization should not be nil.", func(ctx convey.C) {
  50. ctx.So(authorization, convey.ShouldNotBeNil)
  51. })
  52. })
  53. }