blob: 3a9d098d44c0a450f1816f3d980741d5ac177a6c [file] [log] [blame]
Jon Perrittf81e17a2014-09-15 01:29:41 -05001package objects
2
3import (
4 "bytes"
Jamie Hannaford08096232015-07-13 12:47:28 +02005 "fmt"
Jamie Hannaford2e784862014-10-27 10:40:27 +01006 "io"
Jamie Hannaford08096232015-07-13 12:47:28 +02007 "net/http"
Brendan ODonnella69b3472015-04-27 13:59:41 -05008 "strings"
Jon Perrittf81e17a2014-09-15 01:29:41 -05009 "testing"
10
Jon Perritt8c93a302014-09-28 22:35:57 -050011 "github.com/rackspace/gophercloud/pagination"
Jon Perritt457f8ca2014-10-15 00:28:23 -050012 th "github.com/rackspace/gophercloud/testhelper"
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +020013 fake "github.com/rackspace/gophercloud/testhelper/client"
Jon Perrittf81e17a2014-09-15 01:29:41 -050014)
15
Jamie Hannaford2e784862014-10-27 10:40:27 +010016func TestDownloadReader(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050017 th.SetupHTTP()
18 defer th.TeardownHTTP()
19 HandleDownloadObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050020
Jamie Hannaford2e784862014-10-27 10:40:27 +010021 response := Download(fake.ServiceClient(), "testContainer", "testObject", nil)
Jon Perrittf9fd95b2014-10-27 13:17:23 -050022 defer response.Body.Close()
Jamie Hannaford2e784862014-10-27 10:40:27 +010023
24 // Check reader
25 buf := bytes.NewBuffer(make([]byte, 0))
26 io.CopyN(buf, response.Body, 10)
27 th.CheckEquals(t, "Successful", string(buf.Bytes()))
28}
29
30func TestDownloadExtraction(t *testing.T) {
31 th.SetupHTTP()
32 defer th.TeardownHTTP()
33 HandleDownloadObjectSuccessfully(t)
34
35 response := Download(fake.ServiceClient(), "testContainer", "testObject", nil)
36
37 // Check []byte extraction
38 bytes, err := response.ExtractContent()
Jon Perritt457f8ca2014-10-15 00:28:23 -050039 th.AssertNoErr(t, err)
Jamie Hannaford2e784862014-10-27 10:40:27 +010040 th.CheckEquals(t, "Successful download with Gophercloud", string(bytes))
Jon Perrittf81e17a2014-09-15 01:29:41 -050041}
42
43func TestListObjectInfo(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050044 th.SetupHTTP()
45 defer th.TeardownHTTP()
46 HandleListObjectsInfoSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050047
Jon Perrittde47eac2014-09-30 15:34:17 -050048 count := 0
Jon Perritt457f8ca2014-10-15 00:28:23 -050049 options := &ListOpts{Full: true}
50 err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittde47eac2014-09-30 15:34:17 -050051 count++
Jon Perritt8c93a302014-09-28 22:35:57 -050052 actual, err := ExtractInfo(page)
Jon Perritt457f8ca2014-10-15 00:28:23 -050053 th.AssertNoErr(t, err)
Jon Perritt8c93a302014-09-28 22:35:57 -050054
Jon Perritt457f8ca2014-10-15 00:28:23 -050055 th.CheckDeepEquals(t, ExpectedListInfo, actual)
Jon Perritt8c93a302014-09-28 22:35:57 -050056
57 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -050058 })
Jon Perritt457f8ca2014-10-15 00:28:23 -050059 th.AssertNoErr(t, err)
60 th.CheckEquals(t, count, 1)
Jon Perrittf81e17a2014-09-15 01:29:41 -050061}
62
63func TestListObjectNames(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050064 th.SetupHTTP()
65 defer th.TeardownHTTP()
66 HandleListObjectNamesSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050067
Jon Perrittde47eac2014-09-30 15:34:17 -050068 count := 0
Jon Perritt457f8ca2014-10-15 00:28:23 -050069 options := &ListOpts{Full: false}
70 err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittde47eac2014-09-30 15:34:17 -050071 count++
Jon Perritt8c93a302014-09-28 22:35:57 -050072 actual, err := ExtractNames(page)
73 if err != nil {
Jon Perritt457f8ca2014-10-15 00:28:23 -050074 t.Errorf("Failed to extract container names: %v", err)
Jon Perritt8c93a302014-09-28 22:35:57 -050075 return false, err
76 }
77
Jon Perritt457f8ca2014-10-15 00:28:23 -050078 th.CheckDeepEquals(t, ExpectedListNames, actual)
Jon Perritt8c93a302014-09-28 22:35:57 -050079
80 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -050081 })
Jon Perritt457f8ca2014-10-15 00:28:23 -050082 th.AssertNoErr(t, err)
83 th.CheckEquals(t, count, 1)
Jon Perrittf81e17a2014-09-15 01:29:41 -050084}
Jon Perritt8aa40262014-09-29 15:41:32 -050085
Jon Perrittf81e17a2014-09-15 01:29:41 -050086func TestCreateObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050087 th.SetupHTTP()
88 defer th.TeardownHTTP()
Jon Perrittf81e17a2014-09-15 01:29:41 -050089
Jamie Hannaford08096232015-07-13 12:47:28 +020090 content := "Did gyre and gimble in the wabe"
91
92 HandleCreateTextObjectSuccessfully(t, content)
93
Ash Wilson93beae02015-01-23 14:14:48 -050094 options := &CreateOpts{ContentType: "text/plain"}
Jamie Hannaford08096232015-07-13 12:47:28 +020095 res := Create(fake.ServiceClient(), "testContainer", "testObject", strings.NewReader(content), options)
Jon Perritt0ce24ad2014-10-20 21:59:45 -050096 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -050097}
98
Rodrigo Lourencod1c7d252016-05-23 14:11:03 -030099func TestCreateObjectWithCacheControl(t *testing.T) {
100 th.SetupHTTP()
101 defer th.TeardownHTTP()
102
103 content := "All mimsy were the borogoves"
104
105 HandleCreateTextWithCacheControlSuccessfully(t, content)
106
107 options := &CreateOpts{CacheControl: `max-age="3600", public`}
108 res := Create(fake.ServiceClient(), "testContainer", "testObject", strings.NewReader(content), options)
109 th.AssertNoErr(t, res.Err)
110}
111
Ash Wilson93beae02015-01-23 14:14:48 -0500112func TestCreateObjectWithoutContentType(t *testing.T) {
113 th.SetupHTTP()
114 defer th.TeardownHTTP()
Jamie Hannaford08096232015-07-13 12:47:28 +0200115
116 content := "The sky was the color of television, tuned to a dead channel."
117
118 HandleCreateTypelessObjectSuccessfully(t, content)
119
120 res := Create(fake.ServiceClient(), "testContainer", "testObject", strings.NewReader(content), &CreateOpts{})
121 th.AssertNoErr(t, res.Err)
122}
123
124func TestErrorIsRaisedForChecksumMismatch(t *testing.T) {
125 th.SetupHTTP()
126 defer th.TeardownHTTP()
127
128 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
129 w.Header().Set("ETag", "acbd18db4cc2f85cedef654fccc4a4d8")
130 w.WriteHeader(http.StatusCreated)
131 })
Ash Wilson93beae02015-01-23 14:14:48 -0500132
Brendan ODonnella69b3472015-04-27 13:59:41 -0500133 content := strings.NewReader("The sky was the color of television, tuned to a dead channel.")
Ash Wilson93beae02015-01-23 14:14:48 -0500134 res := Create(fake.ServiceClient(), "testContainer", "testObject", content, &CreateOpts{})
Jamie Hannaford08096232015-07-13 12:47:28 +0200135
136 err := fmt.Errorf("Local checksum does not match API ETag header")
137 th.AssertDeepEquals(t, err, res.Err)
Ash Wilson93beae02015-01-23 14:14:48 -0500138}
139
Jon Perrittf81e17a2014-09-15 01:29:41 -0500140func TestCopyObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500141 th.SetupHTTP()
142 defer th.TeardownHTTP()
143 HandleCopyObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500144
Jon Perrittd3080ee2014-10-13 21:08:55 -0500145 options := &CopyOpts{Destination: "/newTestContainer/newTestObject"}
Jon Perritt0ce24ad2014-10-20 21:59:45 -0500146 res := Copy(fake.ServiceClient(), "testContainer", "testObject", options)
147 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500148}
149
150func TestDeleteObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500151 th.SetupHTTP()
152 defer th.TeardownHTTP()
153 HandleDeleteObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500154
Jon Perritt0ce24ad2014-10-20 21:59:45 -0500155 res := Delete(fake.ServiceClient(), "testContainer", "testObject", nil)
156 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500157}
158
159func TestUpateObjectMetadata(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500160 th.SetupHTTP()
161 defer th.TeardownHTTP()
162 HandleUpdateObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500163
Jon Perritt457f8ca2014-10-15 00:28:23 -0500164 options := &UpdateOpts{Metadata: map[string]string{"Gophercloud-Test": "objects"}}
Jon Perritt0ce24ad2014-10-20 21:59:45 -0500165 res := Update(fake.ServiceClient(), "testContainer", "testObject", options)
166 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500167}
168
Jon Perritt8c93a302014-09-28 22:35:57 -0500169func TestGetObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500170 th.SetupHTTP()
171 defer th.TeardownHTTP()
172 HandleGetObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500173
Jon Perritt457f8ca2014-10-15 00:28:23 -0500174 expected := map[string]string{"Gophercloud-Test": "objects"}
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200175 actual, err := Get(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractMetadata()
Jon Perritt457f8ca2014-10-15 00:28:23 -0500176 th.AssertNoErr(t, err)
177 th.CheckDeepEquals(t, expected, actual)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500178}