blob: c3c28a789b935eb4f0d28a7bc65a1d17ebd4dbdf [file] [log] [blame]
Jon Perrittf81e17a2014-09-15 01:29:41 -05001package objects
2
3import (
4 "bytes"
Jamie Hannaford2e784862014-10-27 10:40:27 +01005 "io"
Jon Perrittf81e17a2014-09-15 01:29:41 -05006 "testing"
7
Jon Perritt8c93a302014-09-28 22:35:57 -05008 "github.com/rackspace/gophercloud/pagination"
Jon Perritt457f8ca2014-10-15 00:28:23 -05009 th "github.com/rackspace/gophercloud/testhelper"
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +020010 fake "github.com/rackspace/gophercloud/testhelper/client"
Jon Perrittf81e17a2014-09-15 01:29:41 -050011)
12
Jamie Hannaford2e784862014-10-27 10:40:27 +010013func TestDownloadReader(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050014 th.SetupHTTP()
15 defer th.TeardownHTTP()
16 HandleDownloadObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050017
Jamie Hannaford2e784862014-10-27 10:40:27 +010018 response := Download(fake.ServiceClient(), "testContainer", "testObject", nil)
Jon Perrittf9fd95b2014-10-27 13:17:23 -050019 defer response.Body.Close()
Jamie Hannaford2e784862014-10-27 10:40:27 +010020
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
27func 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 Perritt457f8ca2014-10-15 00:28:23 -050036 th.AssertNoErr(t, err)
Jamie Hannaford2e784862014-10-27 10:40:27 +010037 th.CheckEquals(t, "Successful download with Gophercloud", string(bytes))
Jon Perrittf81e17a2014-09-15 01:29:41 -050038}
39
40func TestListObjectInfo(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050041 th.SetupHTTP()
42 defer th.TeardownHTTP()
43 HandleListObjectsInfoSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050044
Jon Perrittde47eac2014-09-30 15:34:17 -050045 count := 0
Jon Perritt457f8ca2014-10-15 00:28:23 -050046 options := &ListOpts{Full: true}
47 err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittde47eac2014-09-30 15:34:17 -050048 count++
Jon Perritt8c93a302014-09-28 22:35:57 -050049 actual, err := ExtractInfo(page)
Jon Perritt457f8ca2014-10-15 00:28:23 -050050 th.AssertNoErr(t, err)
Jon Perritt8c93a302014-09-28 22:35:57 -050051
Jon Perritt457f8ca2014-10-15 00:28:23 -050052 th.CheckDeepEquals(t, ExpectedListInfo, actual)
Jon Perritt8c93a302014-09-28 22:35:57 -050053
54 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -050055 })
Jon Perritt457f8ca2014-10-15 00:28:23 -050056 th.AssertNoErr(t, err)
57 th.CheckEquals(t, count, 1)
Jon Perrittf81e17a2014-09-15 01:29:41 -050058}
59
60func TestListObjectNames(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050061 th.SetupHTTP()
62 defer th.TeardownHTTP()
63 HandleListObjectNamesSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050064
Jon Perrittde47eac2014-09-30 15:34:17 -050065 count := 0
Jon Perritt457f8ca2014-10-15 00:28:23 -050066 options := &ListOpts{Full: false}
67 err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittde47eac2014-09-30 15:34:17 -050068 count++
Jon Perritt8c93a302014-09-28 22:35:57 -050069 actual, err := ExtractNames(page)
70 if err != nil {
Jon Perritt457f8ca2014-10-15 00:28:23 -050071 t.Errorf("Failed to extract container names: %v", err)
Jon Perritt8c93a302014-09-28 22:35:57 -050072 return false, err
73 }
74
Jon Perritt457f8ca2014-10-15 00:28:23 -050075 th.CheckDeepEquals(t, ExpectedListNames, actual)
Jon Perritt8c93a302014-09-28 22:35:57 -050076
77 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -050078 })
Jon Perritt457f8ca2014-10-15 00:28:23 -050079 th.AssertNoErr(t, err)
80 th.CheckEquals(t, count, 1)
Jon Perrittf81e17a2014-09-15 01:29:41 -050081}
Jon Perritt8aa40262014-09-29 15:41:32 -050082
Jon Perrittf81e17a2014-09-15 01:29:41 -050083func TestCreateObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050084 th.SetupHTTP()
85 defer th.TeardownHTTP()
86 HandleCreateObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050087
Jon Perritt8c93a302014-09-28 22:35:57 -050088 content := bytes.NewBufferString("Did gyre and gimble in the wabe")
Jon Perrittd3080ee2014-10-13 21:08:55 -050089 options := &CreateOpts{ContentType: "application/json"}
Jon Perritt0ce24ad2014-10-20 21:59:45 -050090 res := Create(fake.ServiceClient(), "testContainer", "testObject", content, options)
91 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -050092}
93
94func TestCopyObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050095 th.SetupHTTP()
96 defer th.TeardownHTTP()
97 HandleCopyObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050098
Jon Perrittd3080ee2014-10-13 21:08:55 -050099 options := &CopyOpts{Destination: "/newTestContainer/newTestObject"}
Jon Perritt0ce24ad2014-10-20 21:59:45 -0500100 res := Copy(fake.ServiceClient(), "testContainer", "testObject", options)
101 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500102}
103
104func TestDeleteObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500105 th.SetupHTTP()
106 defer th.TeardownHTTP()
107 HandleDeleteObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500108
Jon Perritt0ce24ad2014-10-20 21:59:45 -0500109 res := Delete(fake.ServiceClient(), "testContainer", "testObject", nil)
110 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500111}
112
113func TestUpateObjectMetadata(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500114 th.SetupHTTP()
115 defer th.TeardownHTTP()
116 HandleUpdateObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500117
Jon Perritt457f8ca2014-10-15 00:28:23 -0500118 options := &UpdateOpts{Metadata: map[string]string{"Gophercloud-Test": "objects"}}
Jon Perritt0ce24ad2014-10-20 21:59:45 -0500119 res := Update(fake.ServiceClient(), "testContainer", "testObject", options)
120 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500121}
122
Jon Perritt8c93a302014-09-28 22:35:57 -0500123func TestGetObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500124 th.SetupHTTP()
125 defer th.TeardownHTTP()
126 HandleGetObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500127
Jon Perritt457f8ca2014-10-15 00:28:23 -0500128 expected := map[string]string{"Gophercloud-Test": "objects"}
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200129 actual, err := Get(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractMetadata()
Jon Perritt457f8ca2014-10-15 00:28:23 -0500130 th.AssertNoErr(t, err)
131 th.CheckDeepEquals(t, expected, actual)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500132}