blob: 34310562a6eb4380f40aa9b691276d07289f66f3 [file] [log] [blame]
Jon Perritt0e28b112014-10-14 20:49:31 -05001package objects
Jon Perritt457f8ca2014-10-15 00:28:23 -05002
3import (
Brendan ODonnella69b3472015-04-27 13:59:41 -05004 "strings"
Jon Perritt457f8ca2014-10-15 00:28:23 -05005 "testing"
6
7 os "github.com/rackspace/gophercloud/openstack/objectstorage/v1/objects"
8 "github.com/rackspace/gophercloud/pagination"
9 th "github.com/rackspace/gophercloud/testhelper"
10 fake "github.com/rackspace/gophercloud/testhelper/client"
11)
12
13func TestDownloadObject(t *testing.T) {
14 th.SetupHTTP()
15 defer th.TeardownHTTP()
16 os.HandleDownloadObjectSuccessfully(t)
17
18 content, err := Download(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractContent()
19 th.AssertNoErr(t, err)
20 th.CheckEquals(t, string(content), "Successful download with Gophercloud")
21}
22
23func TestListObjectsInfo(t *testing.T) {
24 th.SetupHTTP()
25 defer th.TeardownHTTP()
26 os.HandleListObjectsInfoSuccessfully(t)
27
28 count := 0
29 options := &os.ListOpts{Full: true}
30 err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) {
31 count++
32 actual, err := ExtractInfo(page)
33 th.AssertNoErr(t, err)
34
35 th.CheckDeepEquals(t, os.ExpectedListInfo, actual)
36
37 return true, nil
38 })
39 th.AssertNoErr(t, err)
40 th.CheckEquals(t, count, 1)
41}
42
43func TestListObjectNames(t *testing.T) {
44 th.SetupHTTP()
45 defer th.TeardownHTTP()
46 os.HandleListObjectNamesSuccessfully(t)
47
48 count := 0
49 options := &os.ListOpts{Full: false}
50 err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) {
51 count++
52 actual, err := ExtractNames(page)
53 if err != nil {
54 t.Errorf("Failed to extract container names: %v", err)
55 return false, err
56 }
57
58 th.CheckDeepEquals(t, os.ExpectedListNames, actual)
59
60 return true, nil
61 })
62 th.AssertNoErr(t, err)
63 th.CheckEquals(t, count, 1)
64}
65
66func TestCreateObject(t *testing.T) {
67 th.SetupHTTP()
68 defer th.TeardownHTTP()
Ash Wilson161a8542015-01-23 14:32:12 -050069 os.HandleCreateTextObjectSuccessfully(t)
Jon Perritt457f8ca2014-10-15 00:28:23 -050070
Brendan ODonnella69b3472015-04-27 13:59:41 -050071 content := strings.NewReader("Did gyre and gimble in the wabe")
Ash Wilson161a8542015-01-23 14:32:12 -050072 options := &os.CreateOpts{ContentType: "text/plain"}
Jon Perritt260e0882014-10-20 23:31:23 -050073 res := Create(fake.ServiceClient(), "testContainer", "testObject", content, options)
74 th.AssertNoErr(t, res.Err)
Jon Perritt457f8ca2014-10-15 00:28:23 -050075}
76
Ash Wilson161a8542015-01-23 14:32:12 -050077func TestCreateObjectWithoutContentType(t *testing.T) {
78 th.SetupHTTP()
79 defer th.TeardownHTTP()
80 os.HandleCreateTypelessObjectSuccessfully(t)
81
Brendan ODonnella69b3472015-04-27 13:59:41 -050082 content := strings.NewReader("The sky was the color of television, tuned to a dead channel.")
Ash Wilson03732c22015-01-23 14:35:27 -050083 res := Create(fake.ServiceClient(), "testContainer", "testObject", content, &os.CreateOpts{})
Ash Wilson161a8542015-01-23 14:32:12 -050084 th.AssertNoErr(t, res.Err)
85}
86
Jon Perritt457f8ca2014-10-15 00:28:23 -050087func TestCopyObject(t *testing.T) {
88 th.SetupHTTP()
89 defer th.TeardownHTTP()
90 os.HandleCopyObjectSuccessfully(t)
91
92 options := &CopyOpts{Destination: "/newTestContainer/newTestObject"}
Jon Perritt260e0882014-10-20 23:31:23 -050093 res := Copy(fake.ServiceClient(), "testContainer", "testObject", options)
94 th.AssertNoErr(t, res.Err)
Jon Perritt457f8ca2014-10-15 00:28:23 -050095}
96
97func TestDeleteObject(t *testing.T) {
98 th.SetupHTTP()
99 defer th.TeardownHTTP()
100 os.HandleDeleteObjectSuccessfully(t)
101
Jon Perritt260e0882014-10-20 23:31:23 -0500102 res := Delete(fake.ServiceClient(), "testContainer", "testObject", nil)
103 th.AssertNoErr(t, res.Err)
Jon Perritt457f8ca2014-10-15 00:28:23 -0500104}
105
106func TestUpdateObject(t *testing.T) {
107 th.SetupHTTP()
108 defer th.TeardownHTTP()
109 os.HandleUpdateObjectSuccessfully(t)
110
111 options := &os.UpdateOpts{Metadata: map[string]string{"Gophercloud-Test": "objects"}}
Jon Perritt260e0882014-10-20 23:31:23 -0500112 res := Update(fake.ServiceClient(), "testContainer", "testObject", options)
113 th.AssertNoErr(t, res.Err)
Jon Perritt457f8ca2014-10-15 00:28:23 -0500114}
115
116func TestGetObject(t *testing.T) {
117 th.SetupHTTP()
118 defer th.TeardownHTTP()
119 os.HandleGetObjectSuccessfully(t)
120
121 expected := map[string]string{"Gophercloud-Test": "objects"}
122 actual, err := Get(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractMetadata()
123 th.AssertNoErr(t, err)
124 th.CheckDeepEquals(t, expected, actual)
125}