Jon Perritt | 0e28b11 | 2014-10-14 20:49:31 -0500 | [diff] [blame] | 1 | package objects |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 2 | |
| 3 | import ( |
Brendan ODonnell | a69b347 | 2015-04-27 13:59:41 -0500 | [diff] [blame] | 4 | "strings" |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 5 | "testing" |
| 6 | |
| 7 | os "github.com/rackspace/gophercloud/openstack/objectstorage/v1/objects" |
| 8 | "github.com/rackspace/gophercloud/pagination" |
| 9 | th "github.com/rackspace/gophercloud/testhelper" |
| 10 | fake "github.com/rackspace/gophercloud/testhelper/client" |
| 11 | ) |
| 12 | |
| 13 | func TestDownloadObject(t *testing.T) { |
| 14 | th.SetupHTTP() |
| 15 | defer th.TeardownHTTP() |
| 16 | os.HandleDownloadObjectSuccessfully(t) |
| 17 | |
| 18 | content, err := Download(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractContent() |
| 19 | th.AssertNoErr(t, err) |
| 20 | th.CheckEquals(t, string(content), "Successful download with Gophercloud") |
| 21 | } |
| 22 | |
| 23 | func TestListObjectsInfo(t *testing.T) { |
| 24 | th.SetupHTTP() |
| 25 | defer th.TeardownHTTP() |
| 26 | os.HandleListObjectsInfoSuccessfully(t) |
| 27 | |
| 28 | count := 0 |
| 29 | options := &os.ListOpts{Full: true} |
| 30 | err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) { |
| 31 | count++ |
| 32 | actual, err := ExtractInfo(page) |
| 33 | th.AssertNoErr(t, err) |
| 34 | |
| 35 | th.CheckDeepEquals(t, os.ExpectedListInfo, actual) |
| 36 | |
| 37 | return true, nil |
| 38 | }) |
| 39 | th.AssertNoErr(t, err) |
| 40 | th.CheckEquals(t, count, 1) |
| 41 | } |
| 42 | |
| 43 | func TestListObjectNames(t *testing.T) { |
| 44 | th.SetupHTTP() |
| 45 | defer th.TeardownHTTP() |
| 46 | os.HandleListObjectNamesSuccessfully(t) |
| 47 | |
| 48 | count := 0 |
| 49 | options := &os.ListOpts{Full: false} |
| 50 | err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) { |
| 51 | count++ |
| 52 | actual, err := ExtractNames(page) |
| 53 | if err != nil { |
| 54 | t.Errorf("Failed to extract container names: %v", err) |
| 55 | return false, err |
| 56 | } |
| 57 | |
| 58 | th.CheckDeepEquals(t, os.ExpectedListNames, actual) |
| 59 | |
| 60 | return true, nil |
| 61 | }) |
| 62 | th.AssertNoErr(t, err) |
| 63 | th.CheckEquals(t, count, 1) |
| 64 | } |
| 65 | |
| 66 | func TestCreateObject(t *testing.T) { |
| 67 | th.SetupHTTP() |
| 68 | defer th.TeardownHTTP() |
Jamie Hannaford | b9396a1 | 2015-07-14 10:00:19 +0200 | [diff] [blame] | 69 | |
| 70 | content := "Did gyre and gimble in the wabe" |
| 71 | os.HandleCreateTextObjectSuccessfully(t, content) |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 72 | |
Ash Wilson | 161a854 | 2015-01-23 14:32:12 -0500 | [diff] [blame] | 73 | options := &os.CreateOpts{ContentType: "text/plain"} |
Jamie Hannaford | b9396a1 | 2015-07-14 10:00:19 +0200 | [diff] [blame] | 74 | res := Create(fake.ServiceClient(), "testContainer", "testObject", strings.NewReader(content), options) |
Jon Perritt | 260e088 | 2014-10-20 23:31:23 -0500 | [diff] [blame] | 75 | th.AssertNoErr(t, res.Err) |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 76 | } |
| 77 | |
Ash Wilson | 161a854 | 2015-01-23 14:32:12 -0500 | [diff] [blame] | 78 | func TestCreateObjectWithoutContentType(t *testing.T) { |
| 79 | th.SetupHTTP() |
| 80 | defer th.TeardownHTTP() |
Ash Wilson | 161a854 | 2015-01-23 14:32:12 -0500 | [diff] [blame] | 81 | |
Jamie Hannaford | b9396a1 | 2015-07-14 10:00:19 +0200 | [diff] [blame] | 82 | content := "The sky was the color of television, tuned to a dead channel." |
| 83 | os.HandleCreateTypelessObjectSuccessfully(t, content) |
| 84 | |
| 85 | res := Create(fake.ServiceClient(), "testContainer", "testObject", strings.NewReader(content), &os.CreateOpts{}) |
Ash Wilson | 161a854 | 2015-01-23 14:32:12 -0500 | [diff] [blame] | 86 | th.AssertNoErr(t, res.Err) |
| 87 | } |
| 88 | |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 89 | func TestCopyObject(t *testing.T) { |
| 90 | th.SetupHTTP() |
| 91 | defer th.TeardownHTTP() |
| 92 | os.HandleCopyObjectSuccessfully(t) |
| 93 | |
| 94 | options := &CopyOpts{Destination: "/newTestContainer/newTestObject"} |
Jon Perritt | 260e088 | 2014-10-20 23:31:23 -0500 | [diff] [blame] | 95 | res := Copy(fake.ServiceClient(), "testContainer", "testObject", options) |
| 96 | th.AssertNoErr(t, res.Err) |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | func TestDeleteObject(t *testing.T) { |
| 100 | th.SetupHTTP() |
| 101 | defer th.TeardownHTTP() |
| 102 | os.HandleDeleteObjectSuccessfully(t) |
| 103 | |
Jon Perritt | 260e088 | 2014-10-20 23:31:23 -0500 | [diff] [blame] | 104 | res := Delete(fake.ServiceClient(), "testContainer", "testObject", nil) |
| 105 | th.AssertNoErr(t, res.Err) |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | func TestUpdateObject(t *testing.T) { |
| 109 | th.SetupHTTP() |
| 110 | defer th.TeardownHTTP() |
| 111 | os.HandleUpdateObjectSuccessfully(t) |
| 112 | |
| 113 | options := &os.UpdateOpts{Metadata: map[string]string{"Gophercloud-Test": "objects"}} |
Jon Perritt | 260e088 | 2014-10-20 23:31:23 -0500 | [diff] [blame] | 114 | res := Update(fake.ServiceClient(), "testContainer", "testObject", options) |
| 115 | th.AssertNoErr(t, res.Err) |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | func TestGetObject(t *testing.T) { |
| 119 | th.SetupHTTP() |
| 120 | defer th.TeardownHTTP() |
| 121 | os.HandleGetObjectSuccessfully(t) |
| 122 | |
| 123 | expected := map[string]string{"Gophercloud-Test": "objects"} |
| 124 | actual, err := Get(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractMetadata() |
| 125 | th.AssertNoErr(t, err) |
| 126 | th.CheckDeepEquals(t, expected, actual) |
| 127 | } |