blob: 941e59e6d8cff912f72827566ff900a90200f452 [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"
Brendan ODonnella69b3472015-04-27 13:59:41 -05006 "strings"
Jon Perrittf81e17a2014-09-15 01:29:41 -05007 "testing"
8
Jon Perritt8c93a302014-09-28 22:35:57 -05009 "github.com/rackspace/gophercloud/pagination"
Jon Perritt457f8ca2014-10-15 00:28:23 -050010 th "github.com/rackspace/gophercloud/testhelper"
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +020011 fake "github.com/rackspace/gophercloud/testhelper/client"
Jon Perrittf81e17a2014-09-15 01:29:41 -050012)
13
Jamie Hannaford2e784862014-10-27 10:40:27 +010014func TestDownloadReader(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050015 th.SetupHTTP()
16 defer th.TeardownHTTP()
17 HandleDownloadObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050018
Jamie Hannaford2e784862014-10-27 10:40:27 +010019 response := Download(fake.ServiceClient(), "testContainer", "testObject", nil)
Jon Perrittf9fd95b2014-10-27 13:17:23 -050020 defer response.Body.Close()
Jamie Hannaford2e784862014-10-27 10:40:27 +010021
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
28func 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 Perritt457f8ca2014-10-15 00:28:23 -050037 th.AssertNoErr(t, err)
Jamie Hannaford2e784862014-10-27 10:40:27 +010038 th.CheckEquals(t, "Successful download with Gophercloud", string(bytes))
Jon Perrittf81e17a2014-09-15 01:29:41 -050039}
40
41func TestListObjectInfo(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050042 th.SetupHTTP()
43 defer th.TeardownHTTP()
44 HandleListObjectsInfoSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050045
Jon Perrittde47eac2014-09-30 15:34:17 -050046 count := 0
Jon Perritt457f8ca2014-10-15 00:28:23 -050047 options := &ListOpts{Full: true}
48 err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittde47eac2014-09-30 15:34:17 -050049 count++
Jon Perritt8c93a302014-09-28 22:35:57 -050050 actual, err := ExtractInfo(page)
Jon Perritt457f8ca2014-10-15 00:28:23 -050051 th.AssertNoErr(t, err)
Jon Perritt8c93a302014-09-28 22:35:57 -050052
Jon Perritt457f8ca2014-10-15 00:28:23 -050053 th.CheckDeepEquals(t, ExpectedListInfo, actual)
Jon Perritt8c93a302014-09-28 22:35:57 -050054
55 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -050056 })
Jon Perritt457f8ca2014-10-15 00:28:23 -050057 th.AssertNoErr(t, err)
58 th.CheckEquals(t, count, 1)
Jon Perrittf81e17a2014-09-15 01:29:41 -050059}
60
61func TestListObjectNames(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050062 th.SetupHTTP()
63 defer th.TeardownHTTP()
64 HandleListObjectNamesSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050065
Jon Perrittde47eac2014-09-30 15:34:17 -050066 count := 0
Jon Perritt457f8ca2014-10-15 00:28:23 -050067 options := &ListOpts{Full: false}
68 err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittde47eac2014-09-30 15:34:17 -050069 count++
Jon Perritt8c93a302014-09-28 22:35:57 -050070 actual, err := ExtractNames(page)
71 if err != nil {
Jon Perritt457f8ca2014-10-15 00:28:23 -050072 t.Errorf("Failed to extract container names: %v", err)
Jon Perritt8c93a302014-09-28 22:35:57 -050073 return false, err
74 }
75
Jon Perritt457f8ca2014-10-15 00:28:23 -050076 th.CheckDeepEquals(t, ExpectedListNames, actual)
Jon Perritt8c93a302014-09-28 22:35:57 -050077
78 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -050079 })
Jon Perritt457f8ca2014-10-15 00:28:23 -050080 th.AssertNoErr(t, err)
81 th.CheckEquals(t, count, 1)
Jon Perrittf81e17a2014-09-15 01:29:41 -050082}
Jon Perritt8aa40262014-09-29 15:41:32 -050083
Jon Perrittf81e17a2014-09-15 01:29:41 -050084func TestCreateObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050085 th.SetupHTTP()
86 defer th.TeardownHTTP()
Ash Wilson93beae02015-01-23 14:14:48 -050087 HandleCreateTextObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050088
Brendan ODonnella69b3472015-04-27 13:59:41 -050089 content := strings.NewReader("Did gyre and gimble in the wabe")
Ash Wilson93beae02015-01-23 14:14:48 -050090 options := &CreateOpts{ContentType: "text/plain"}
Jon Perritt0ce24ad2014-10-20 21:59:45 -050091 res := Create(fake.ServiceClient(), "testContainer", "testObject", content, options)
92 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -050093}
94
Ash Wilson93beae02015-01-23 14:14:48 -050095func TestCreateObjectWithoutContentType(t *testing.T) {
96 th.SetupHTTP()
97 defer th.TeardownHTTP()
98 HandleCreateTypelessObjectSuccessfully(t)
99
Brendan ODonnella69b3472015-04-27 13:59:41 -0500100 content := strings.NewReader("The sky was the color of television, tuned to a dead channel.")
Ash Wilson93beae02015-01-23 14:14:48 -0500101 res := Create(fake.ServiceClient(), "testContainer", "testObject", content, &CreateOpts{})
102 th.AssertNoErr(t, res.Err)
103}
104
Jon Perrittf81e17a2014-09-15 01:29:41 -0500105func TestCopyObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500106 th.SetupHTTP()
107 defer th.TeardownHTTP()
108 HandleCopyObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500109
Jon Perrittd3080ee2014-10-13 21:08:55 -0500110 options := &CopyOpts{Destination: "/newTestContainer/newTestObject"}
Jon Perritt0ce24ad2014-10-20 21:59:45 -0500111 res := Copy(fake.ServiceClient(), "testContainer", "testObject", options)
112 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500113}
114
115func TestDeleteObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500116 th.SetupHTTP()
117 defer th.TeardownHTTP()
118 HandleDeleteObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500119
Jon Perritt0ce24ad2014-10-20 21:59:45 -0500120 res := Delete(fake.ServiceClient(), "testContainer", "testObject", nil)
121 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500122}
123
124func TestUpateObjectMetadata(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500125 th.SetupHTTP()
126 defer th.TeardownHTTP()
127 HandleUpdateObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500128
Jon Perritt457f8ca2014-10-15 00:28:23 -0500129 options := &UpdateOpts{Metadata: map[string]string{"Gophercloud-Test": "objects"}}
Jon Perritt0ce24ad2014-10-20 21:59:45 -0500130 res := Update(fake.ServiceClient(), "testContainer", "testObject", options)
131 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500132}
133
Jon Perritt8c93a302014-09-28 22:35:57 -0500134func TestGetObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500135 th.SetupHTTP()
136 defer th.TeardownHTTP()
137 HandleGetObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500138
Jon Perritt457f8ca2014-10-15 00:28:23 -0500139 expected := map[string]string{"Gophercloud-Test": "objects"}
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200140 actual, err := Get(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractMetadata()
Jon Perritt457f8ca2014-10-15 00:28:23 -0500141 th.AssertNoErr(t, err)
142 th.CheckDeepEquals(t, expected, actual)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500143}