bfs_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "reflect"
  8. "testing"
  9. "github.com/bouk/monkey"
  10. "github.com/smartystreets/goconvey/convey"
  11. )
  12. func TestDaoUpload(t *testing.T) {
  13. convey.Convey("Upload", t, func(ctx convey.C) {
  14. var (
  15. c = context.Background()
  16. fileName = ""
  17. fileType = ""
  18. expire = int64(0)
  19. body io.Reader
  20. )
  21. bfsConf := d.c.Bfs
  22. url := fmt.Sprintf(bfsConf.Addr+_uploadURL, bfsConf.Bucket, fileName)
  23. ctx.Convey("When everything is correct", func(ctx convey.C) {
  24. httpMock("PUT", url).Reply(200).SetHeaders(map[string]string{
  25. "Code": "200",
  26. "Location": "SomePlace",
  27. })
  28. location, err := d.Upload(c, fileName, fileType, expire, body)
  29. ctx.Convey("Then err should be nil.location should not be nil.", func(ctx convey.C) {
  30. ctx.So(err, convey.ShouldBeNil)
  31. ctx.So(location, convey.ShouldNotBeNil)
  32. })
  33. })
  34. ctx.Convey("When d.bfsClient.Do gets error", func(ctx convey.C) {
  35. guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.bfsClient), "Do",
  36. func(_ *http.Client, _ *http.Request) (*http.Response, error) {
  37. return nil, fmt.Errorf("d.bfsClient.Do Error")
  38. })
  39. defer guard.Unpatch()
  40. _, err := d.Upload(c, fileName, fileType, expire, body)
  41. ctx.Convey("Then err should not be nil", func(ctx convey.C) {
  42. ctx.So(err, convey.ShouldNotBeNil)
  43. })
  44. })
  45. ctx.Convey("When http request status != 200", func(ctx convey.C) {
  46. httpMock("PUT", url).Reply(404)
  47. _, err := d.Upload(c, fileName, fileType, expire, body)
  48. ctx.Convey("Then err should not be nil", func(ctx convey.C) {
  49. ctx.So(err, convey.ShouldNotBeNil)
  50. })
  51. })
  52. ctx.Convey("When http request Code in header != 200", func(ctx convey.C) {
  53. httpMock("PUT", url).Reply(404).SetHeaders(map[string]string{
  54. "Code": "404",
  55. "Location": "SomePlace",
  56. })
  57. _, err := d.Upload(c, fileName, fileType, expire, body)
  58. ctx.Convey("Then err should not be nil", func(ctx convey.C) {
  59. ctx.So(err, convey.ShouldNotBeNil)
  60. })
  61. })
  62. })
  63. }
  64. func TestDaoauthorize(t *testing.T) {
  65. var (
  66. key = "1234"
  67. secret = "2345"
  68. method = ""
  69. bucket = ""
  70. file = ""
  71. expire = int64(0)
  72. )
  73. convey.Convey("authorize", t, func(ctx convey.C) {
  74. authorization := authorize(key, secret, method, bucket, file, expire)
  75. ctx.Convey("Then authorization should not be nil.", func(ctx convey.C) {
  76. ctx.So(authorization, convey.ShouldNotBeNil)
  77. })
  78. })
  79. }