blob: 7ab40f2d14de0ea2071bcb7b914167bd6e78ca99 [file] [log] [blame]
Jon Perrittf81e17a2014-09-15 01:29:41 -05001package objects
2
3import (
4 "bytes"
Jon Perrittf81e17a2014-09-15 01:29:41 -05005 "testing"
6
Jon Perritt8c93a302014-09-28 22:35:57 -05007 "github.com/rackspace/gophercloud/pagination"
Jon Perritt457f8ca2014-10-15 00:28:23 -05008 th "github.com/rackspace/gophercloud/testhelper"
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +02009 fake "github.com/rackspace/gophercloud/testhelper/client"
Jon Perrittf81e17a2014-09-15 01:29:41 -050010)
11
Jon Perritt8c93a302014-09-28 22:35:57 -050012func TestDownloadObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050013 th.SetupHTTP()
14 defer th.TeardownHTTP()
15 HandleDownloadObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050016
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +020017 content, err := Download(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractContent()
Jon Perritt457f8ca2014-10-15 00:28:23 -050018 th.AssertNoErr(t, err)
19 th.CheckEquals(t, string(content), "Successful download with Gophercloud")
Jon Perrittf81e17a2014-09-15 01:29:41 -050020}
21
22func TestListObjectInfo(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050023 th.SetupHTTP()
24 defer th.TeardownHTTP()
25 HandleListObjectsInfoSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050026
Jon Perrittde47eac2014-09-30 15:34:17 -050027 count := 0
Jon Perritt457f8ca2014-10-15 00:28:23 -050028 options := &ListOpts{Full: true}
29 err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittde47eac2014-09-30 15:34:17 -050030 count++
Jon Perritt8c93a302014-09-28 22:35:57 -050031 actual, err := ExtractInfo(page)
Jon Perritt457f8ca2014-10-15 00:28:23 -050032 th.AssertNoErr(t, err)
Jon Perritt8c93a302014-09-28 22:35:57 -050033
Jon Perritt457f8ca2014-10-15 00:28:23 -050034 th.CheckDeepEquals(t, ExpectedListInfo, actual)
Jon Perritt8c93a302014-09-28 22:35:57 -050035
36 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -050037 })
Jon Perritt457f8ca2014-10-15 00:28:23 -050038 th.AssertNoErr(t, err)
39 th.CheckEquals(t, count, 1)
Jon Perrittf81e17a2014-09-15 01:29:41 -050040}
41
42func TestListObjectNames(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050043 th.SetupHTTP()
44 defer th.TeardownHTTP()
45 HandleListObjectNamesSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050046
Jon Perrittde47eac2014-09-30 15:34:17 -050047 count := 0
Jon Perritt457f8ca2014-10-15 00:28:23 -050048 options := &ListOpts{Full: false}
49 err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittde47eac2014-09-30 15:34:17 -050050 count++
Jon Perritt8c93a302014-09-28 22:35:57 -050051 actual, err := ExtractNames(page)
52 if err != nil {
Jon Perritt457f8ca2014-10-15 00:28:23 -050053 t.Errorf("Failed to extract container names: %v", err)
Jon Perritt8c93a302014-09-28 22:35:57 -050054 return false, err
55 }
56
Jon Perritt457f8ca2014-10-15 00:28:23 -050057 th.CheckDeepEquals(t, ExpectedListNames, actual)
Jon Perritt8c93a302014-09-28 22:35:57 -050058
59 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -050060 })
Jon Perritt457f8ca2014-10-15 00:28:23 -050061 th.AssertNoErr(t, err)
62 th.CheckEquals(t, count, 1)
Jon Perrittf81e17a2014-09-15 01:29:41 -050063}
Jon Perritt8aa40262014-09-29 15:41:32 -050064
Jon Perrittf81e17a2014-09-15 01:29:41 -050065func TestCreateObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050066 th.SetupHTTP()
67 defer th.TeardownHTTP()
68 HandleCreateObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050069
Jon Perritt8c93a302014-09-28 22:35:57 -050070 content := bytes.NewBufferString("Did gyre and gimble in the wabe")
Jon Perrittd3080ee2014-10-13 21:08:55 -050071 options := &CreateOpts{ContentType: "application/json"}
Jon Perritt0ce24ad2014-10-20 21:59:45 -050072 res := Create(fake.ServiceClient(), "testContainer", "testObject", content, options)
73 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -050074}
75
76func TestCopyObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050077 th.SetupHTTP()
78 defer th.TeardownHTTP()
79 HandleCopyObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050080
Jon Perrittd3080ee2014-10-13 21:08:55 -050081 options := &CopyOpts{Destination: "/newTestContainer/newTestObject"}
Jon Perritt0ce24ad2014-10-20 21:59:45 -050082 res := Copy(fake.ServiceClient(), "testContainer", "testObject", options)
83 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -050084}
85
86func TestDeleteObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050087 th.SetupHTTP()
88 defer th.TeardownHTTP()
89 HandleDeleteObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050090
Jon Perritt0ce24ad2014-10-20 21:59:45 -050091 res := Delete(fake.ServiceClient(), "testContainer", "testObject", nil)
92 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -050093}
94
95func TestUpateObjectMetadata(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050096 th.SetupHTTP()
97 defer th.TeardownHTTP()
98 HandleUpdateObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050099
Jon Perritt457f8ca2014-10-15 00:28:23 -0500100 options := &UpdateOpts{Metadata: map[string]string{"Gophercloud-Test": "objects"}}
Jon Perritt0ce24ad2014-10-20 21:59:45 -0500101 res := Update(fake.ServiceClient(), "testContainer", "testObject", options)
102 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500103}
104
Jon Perritt8c93a302014-09-28 22:35:57 -0500105func TestGetObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500106 th.SetupHTTP()
107 defer th.TeardownHTTP()
108 HandleGetObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500109
Jon Perritt457f8ca2014-10-15 00:28:23 -0500110 expected := map[string]string{"Gophercloud-Test": "objects"}
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200111 actual, err := Get(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractMetadata()
Jon Perritt457f8ca2014-10-15 00:28:23 -0500112 th.AssertNoErr(t, err)
113 th.CheckDeepEquals(t, expected, actual)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500114}