Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 1 | package objects |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 5 | "io" |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 6 | "testing" |
| 7 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 9 | th "github.com/rackspace/gophercloud/testhelper" |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 10 | fake "github.com/rackspace/gophercloud/testhelper/client" |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 11 | ) |
| 12 | |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 13 | func TestDownloadReader(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 14 | th.SetupHTTP() |
| 15 | defer th.TeardownHTTP() |
| 16 | HandleDownloadObjectSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 17 | |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 18 | response := Download(fake.ServiceClient(), "testContainer", "testObject", nil) |
Jon Perritt | f9fd95b | 2014-10-27 13:17:23 -0500 | [diff] [blame] | 19 | defer response.Body.Close() |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 20 | |
| 21 | // Check reader |
| 22 | buf := bytes.NewBuffer(make([]byte, 0)) |
| 23 | io.CopyN(buf, response.Body, 10) |
| 24 | th.CheckEquals(t, "Successful", string(buf.Bytes())) |
| 25 | } |
| 26 | |
| 27 | func TestDownloadExtraction(t *testing.T) { |
| 28 | th.SetupHTTP() |
| 29 | defer th.TeardownHTTP() |
| 30 | HandleDownloadObjectSuccessfully(t) |
| 31 | |
| 32 | response := Download(fake.ServiceClient(), "testContainer", "testObject", nil) |
| 33 | |
| 34 | // Check []byte extraction |
| 35 | bytes, err := response.ExtractContent() |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 36 | th.AssertNoErr(t, err) |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 37 | th.CheckEquals(t, "Successful download with Gophercloud", string(bytes)) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | func TestListObjectInfo(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 41 | th.SetupHTTP() |
| 42 | defer th.TeardownHTTP() |
| 43 | HandleListObjectsInfoSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 44 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 45 | count := 0 |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 46 | options := &ListOpts{Full: true} |
| 47 | 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] | 48 | count++ |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 49 | actual, err := ExtractInfo(page) |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 50 | th.AssertNoErr(t, err) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 51 | |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 52 | th.CheckDeepEquals(t, ExpectedListInfo, actual) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 53 | |
| 54 | return true, nil |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 55 | }) |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 56 | th.AssertNoErr(t, err) |
| 57 | th.CheckEquals(t, count, 1) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | func TestListObjectNames(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 61 | th.SetupHTTP() |
| 62 | defer th.TeardownHTTP() |
| 63 | HandleListObjectNamesSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 64 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 65 | count := 0 |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 66 | options := &ListOpts{Full: false} |
| 67 | 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] | 68 | count++ |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 69 | actual, err := ExtractNames(page) |
| 70 | if err != nil { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 71 | t.Errorf("Failed to extract container names: %v", err) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 72 | return false, err |
| 73 | } |
| 74 | |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 75 | th.CheckDeepEquals(t, ExpectedListNames, actual) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 76 | |
| 77 | return true, nil |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 78 | }) |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 79 | th.AssertNoErr(t, err) |
| 80 | th.CheckEquals(t, count, 1) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 81 | } |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 82 | |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 83 | func TestCreateObject(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 84 | th.SetupHTTP() |
| 85 | defer th.TeardownHTTP() |
Ash Wilson | 93beae0 | 2015-01-23 14:14:48 -0500 | [diff] [blame^] | 86 | HandleCreateTextObjectSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 87 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 88 | content := bytes.NewBufferString("Did gyre and gimble in the wabe") |
Ash Wilson | 93beae0 | 2015-01-23 14:14:48 -0500 | [diff] [blame^] | 89 | options := &CreateOpts{ContentType: "text/plain"} |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 90 | res := Create(fake.ServiceClient(), "testContainer", "testObject", content, options) |
| 91 | th.AssertNoErr(t, res.Err) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 92 | } |
| 93 | |
Ash Wilson | 93beae0 | 2015-01-23 14:14:48 -0500 | [diff] [blame^] | 94 | func TestCreateObjectWithoutContentType(t *testing.T) { |
| 95 | th.SetupHTTP() |
| 96 | defer th.TeardownHTTP() |
| 97 | HandleCreateTypelessObjectSuccessfully(t) |
| 98 | |
| 99 | content := bytes.NewBufferString("The sky was the color of television, tuned to a dead channel.") |
| 100 | res := Create(fake.ServiceClient(), "testContainer", "testObject", content, &CreateOpts{}) |
| 101 | th.AssertNoErr(t, res.Err) |
| 102 | } |
| 103 | |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 104 | func TestCopyObject(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 105 | th.SetupHTTP() |
| 106 | defer th.TeardownHTTP() |
| 107 | HandleCopyObjectSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 108 | |
Jon Perritt | d3080ee | 2014-10-13 21:08:55 -0500 | [diff] [blame] | 109 | options := &CopyOpts{Destination: "/newTestContainer/newTestObject"} |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 110 | res := Copy(fake.ServiceClient(), "testContainer", "testObject", options) |
| 111 | th.AssertNoErr(t, res.Err) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | func TestDeleteObject(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 115 | th.SetupHTTP() |
| 116 | defer th.TeardownHTTP() |
| 117 | HandleDeleteObjectSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 118 | |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 119 | res := Delete(fake.ServiceClient(), "testContainer", "testObject", nil) |
| 120 | th.AssertNoErr(t, res.Err) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | func TestUpateObjectMetadata(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 124 | th.SetupHTTP() |
| 125 | defer th.TeardownHTTP() |
| 126 | HandleUpdateObjectSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 127 | |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 128 | options := &UpdateOpts{Metadata: map[string]string{"Gophercloud-Test": "objects"}} |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 129 | res := Update(fake.ServiceClient(), "testContainer", "testObject", options) |
| 130 | th.AssertNoErr(t, res.Err) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 131 | } |
| 132 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 133 | func TestGetObject(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 134 | th.SetupHTTP() |
| 135 | defer th.TeardownHTTP() |
| 136 | HandleGetObjectSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 137 | |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 138 | expected := map[string]string{"Gophercloud-Test": "objects"} |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 139 | actual, err := Get(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractMetadata() |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 140 | th.AssertNoErr(t, err) |
| 141 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 142 | } |