blob: 3935e1b7815c44cf708dbfd1021898be0fd3ebd1 [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)
19
20 // Check reader
21 buf := bytes.NewBuffer(make([]byte, 0))
22 io.CopyN(buf, response.Body, 10)
23 th.CheckEquals(t, "Successful", string(buf.Bytes()))
24}
25
26func TestDownloadExtraction(t *testing.T) {
27 th.SetupHTTP()
28 defer th.TeardownHTTP()
29 HandleDownloadObjectSuccessfully(t)
30
31 response := Download(fake.ServiceClient(), "testContainer", "testObject", nil)
32
33 // Check []byte extraction
34 bytes, err := response.ExtractContent()
Jon Perritt457f8ca2014-10-15 00:28:23 -050035 th.AssertNoErr(t, err)
Jamie Hannaford2e784862014-10-27 10:40:27 +010036 th.CheckEquals(t, "Successful download with Gophercloud", string(bytes))
Jon Perrittf81e17a2014-09-15 01:29:41 -050037}
38
39func TestListObjectInfo(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050040 th.SetupHTTP()
41 defer th.TeardownHTTP()
42 HandleListObjectsInfoSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050043
Jon Perrittde47eac2014-09-30 15:34:17 -050044 count := 0
Jon Perritt457f8ca2014-10-15 00:28:23 -050045 options := &ListOpts{Full: true}
46 err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittde47eac2014-09-30 15:34:17 -050047 count++
Jon Perritt8c93a302014-09-28 22:35:57 -050048 actual, err := ExtractInfo(page)
Jon Perritt457f8ca2014-10-15 00:28:23 -050049 th.AssertNoErr(t, err)
Jon Perritt8c93a302014-09-28 22:35:57 -050050
Jon Perritt457f8ca2014-10-15 00:28:23 -050051 th.CheckDeepEquals(t, ExpectedListInfo, actual)
Jon Perritt8c93a302014-09-28 22:35:57 -050052
53 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -050054 })
Jon Perritt457f8ca2014-10-15 00:28:23 -050055 th.AssertNoErr(t, err)
56 th.CheckEquals(t, count, 1)
Jon Perrittf81e17a2014-09-15 01:29:41 -050057}
58
59func TestListObjectNames(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050060 th.SetupHTTP()
61 defer th.TeardownHTTP()
62 HandleListObjectNamesSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050063
Jon Perrittde47eac2014-09-30 15:34:17 -050064 count := 0
Jon Perritt457f8ca2014-10-15 00:28:23 -050065 options := &ListOpts{Full: false}
66 err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittde47eac2014-09-30 15:34:17 -050067 count++
Jon Perritt8c93a302014-09-28 22:35:57 -050068 actual, err := ExtractNames(page)
69 if err != nil {
Jon Perritt457f8ca2014-10-15 00:28:23 -050070 t.Errorf("Failed to extract container names: %v", err)
Jon Perritt8c93a302014-09-28 22:35:57 -050071 return false, err
72 }
73
Jon Perritt457f8ca2014-10-15 00:28:23 -050074 th.CheckDeepEquals(t, ExpectedListNames, actual)
Jon Perritt8c93a302014-09-28 22:35:57 -050075
76 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -050077 })
Jon Perritt457f8ca2014-10-15 00:28:23 -050078 th.AssertNoErr(t, err)
79 th.CheckEquals(t, count, 1)
Jon Perrittf81e17a2014-09-15 01:29:41 -050080}
Jon Perritt8aa40262014-09-29 15:41:32 -050081
Jon Perrittf81e17a2014-09-15 01:29:41 -050082func TestCreateObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050083 th.SetupHTTP()
84 defer th.TeardownHTTP()
85 HandleCreateObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050086
Jon Perritt8c93a302014-09-28 22:35:57 -050087 content := bytes.NewBufferString("Did gyre and gimble in the wabe")
Jon Perrittd3080ee2014-10-13 21:08:55 -050088 options := &CreateOpts{ContentType: "application/json"}
Jon Perritt0ce24ad2014-10-20 21:59:45 -050089 res := Create(fake.ServiceClient(), "testContainer", "testObject", content, options)
90 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -050091}
92
93func TestCopyObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050094 th.SetupHTTP()
95 defer th.TeardownHTTP()
96 HandleCopyObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050097
Jon Perrittd3080ee2014-10-13 21:08:55 -050098 options := &CopyOpts{Destination: "/newTestContainer/newTestObject"}
Jon Perritt0ce24ad2014-10-20 21:59:45 -050099 res := Copy(fake.ServiceClient(), "testContainer", "testObject", options)
100 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500101}
102
103func TestDeleteObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500104 th.SetupHTTP()
105 defer th.TeardownHTTP()
106 HandleDeleteObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500107
Jon Perritt0ce24ad2014-10-20 21:59:45 -0500108 res := Delete(fake.ServiceClient(), "testContainer", "testObject", nil)
109 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500110}
111
112func TestUpateObjectMetadata(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500113 th.SetupHTTP()
114 defer th.TeardownHTTP()
115 HandleUpdateObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500116
Jon Perritt457f8ca2014-10-15 00:28:23 -0500117 options := &UpdateOpts{Metadata: map[string]string{"Gophercloud-Test": "objects"}}
Jon Perritt0ce24ad2014-10-20 21:59:45 -0500118 res := Update(fake.ServiceClient(), "testContainer", "testObject", options)
119 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500120}
121
Jon Perritt8c93a302014-09-28 22:35:57 -0500122func TestGetObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500123 th.SetupHTTP()
124 defer th.TeardownHTTP()
125 HandleGetObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500126
Jon Perritt457f8ca2014-10-15 00:28:23 -0500127 expected := map[string]string{"Gophercloud-Test": "objects"}
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200128 actual, err := Get(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractMetadata()
Jon Perritt457f8ca2014-10-15 00:28:23 -0500129 th.AssertNoErr(t, err)
130 th.CheckDeepEquals(t, expected, actual)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500131}