Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 1 | package objects |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 5 | "testing" |
| 6 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 8 | th "github.com/rackspace/gophercloud/testhelper" |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 9 | fake "github.com/rackspace/gophercloud/testhelper/client" |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 10 | ) |
| 11 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 12 | func TestDownloadObject(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 13 | th.SetupHTTP() |
| 14 | defer th.TeardownHTTP() |
| 15 | HandleDownloadObjectSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 16 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 17 | content, err := Download(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractContent() |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 18 | th.AssertNoErr(t, err) |
| 19 | th.CheckEquals(t, string(content), "Successful download with Gophercloud") |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | func TestListObjectInfo(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 23 | th.SetupHTTP() |
| 24 | defer th.TeardownHTTP() |
| 25 | HandleListObjectsInfoSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 26 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 27 | count := 0 |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 28 | options := &ListOpts{Full: true} |
| 29 | err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 30 | count++ |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 31 | actual, err := ExtractInfo(page) |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 32 | th.AssertNoErr(t, err) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 33 | |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 34 | th.CheckDeepEquals(t, ExpectedListInfo, actual) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 35 | |
| 36 | return true, nil |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 37 | }) |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 38 | th.AssertNoErr(t, err) |
| 39 | th.CheckEquals(t, count, 1) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | func TestListObjectNames(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 43 | th.SetupHTTP() |
| 44 | defer th.TeardownHTTP() |
| 45 | HandleListObjectNamesSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 46 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 47 | count := 0 |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 48 | options := &ListOpts{Full: false} |
| 49 | err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 50 | count++ |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 51 | actual, err := ExtractNames(page) |
| 52 | if err != nil { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 53 | t.Errorf("Failed to extract container names: %v", err) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 54 | return false, err |
| 55 | } |
| 56 | |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 57 | th.CheckDeepEquals(t, ExpectedListNames, actual) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 58 | |
| 59 | return true, nil |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 60 | }) |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 61 | th.AssertNoErr(t, err) |
| 62 | th.CheckEquals(t, count, 1) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 63 | } |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 64 | |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 65 | func TestCreateObject(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 66 | th.SetupHTTP() |
| 67 | defer th.TeardownHTTP() |
| 68 | HandleCreateObjectSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 69 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 70 | content := bytes.NewBufferString("Did gyre and gimble in the wabe") |
Jon Perritt | d3080ee | 2014-10-13 21:08:55 -0500 | [diff] [blame] | 71 | options := &CreateOpts{ContentType: "application/json"} |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 72 | res := Create(fake.ServiceClient(), "testContainer", "testObject", content, options) |
| 73 | th.AssertNoErr(t, res.Err) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | func TestCopyObject(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 77 | th.SetupHTTP() |
| 78 | defer th.TeardownHTTP() |
| 79 | HandleCopyObjectSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 80 | |
Jon Perritt | d3080ee | 2014-10-13 21:08:55 -0500 | [diff] [blame] | 81 | options := &CopyOpts{Destination: "/newTestContainer/newTestObject"} |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 82 | res := Copy(fake.ServiceClient(), "testContainer", "testObject", options) |
| 83 | th.AssertNoErr(t, res.Err) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | func TestDeleteObject(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 87 | th.SetupHTTP() |
| 88 | defer th.TeardownHTTP() |
| 89 | HandleDeleteObjectSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 90 | |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 91 | res := Delete(fake.ServiceClient(), "testContainer", "testObject", nil) |
| 92 | th.AssertNoErr(t, res.Err) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | func TestUpateObjectMetadata(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 96 | th.SetupHTTP() |
| 97 | defer th.TeardownHTTP() |
| 98 | HandleUpdateObjectSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 99 | |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 100 | options := &UpdateOpts{Metadata: map[string]string{"Gophercloud-Test": "objects"}} |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 101 | res := Update(fake.ServiceClient(), "testContainer", "testObject", options) |
| 102 | th.AssertNoErr(t, res.Err) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 103 | } |
| 104 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 105 | func TestGetObject(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 106 | th.SetupHTTP() |
| 107 | defer th.TeardownHTTP() |
| 108 | HandleGetObjectSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 109 | |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 110 | expected := map[string]string{"Gophercloud-Test": "objects"} |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 111 | actual, err := Get(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractMetadata() |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 112 | th.AssertNoErr(t, err) |
| 113 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 114 | } |