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