blob: aa44efd2ff9a7445669e468ca409d6bb114e266c [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 Perritt27249f42016-02-18 10:35:59 -060011 "github.com/gophercloud/gophercloud/pagination"
12 th "github.com/gophercloud/gophercloud/testhelper"
13 fake "github.com/gophercloud/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
Ash Wilson93beae02015-01-23 14:14:48 -050099func TestCreateObjectWithoutContentType(t *testing.T) {
100 th.SetupHTTP()
101 defer th.TeardownHTTP()
Jamie Hannaford08096232015-07-13 12:47:28 +0200102
103 content := "The sky was the color of television, tuned to a dead channel."
104
105 HandleCreateTypelessObjectSuccessfully(t, content)
106
107 res := Create(fake.ServiceClient(), "testContainer", "testObject", strings.NewReader(content), &CreateOpts{})
108 th.AssertNoErr(t, res.Err)
109}
110
111func TestErrorIsRaisedForChecksumMismatch(t *testing.T) {
112 th.SetupHTTP()
113 defer th.TeardownHTTP()
114
115 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
116 w.Header().Set("ETag", "acbd18db4cc2f85cedef654fccc4a4d8")
117 w.WriteHeader(http.StatusCreated)
118 })
Ash Wilson93beae02015-01-23 14:14:48 -0500119
Brendan ODonnella69b3472015-04-27 13:59:41 -0500120 content := strings.NewReader("The sky was the color of television, tuned to a dead channel.")
Ash Wilson93beae02015-01-23 14:14:48 -0500121 res := Create(fake.ServiceClient(), "testContainer", "testObject", content, &CreateOpts{})
Jamie Hannaford08096232015-07-13 12:47:28 +0200122
123 err := fmt.Errorf("Local checksum does not match API ETag header")
124 th.AssertDeepEquals(t, err, res.Err)
Ash Wilson93beae02015-01-23 14:14:48 -0500125}
126
Jon Perrittf81e17a2014-09-15 01:29:41 -0500127func TestCopyObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500128 th.SetupHTTP()
129 defer th.TeardownHTTP()
130 HandleCopyObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500131
Jon Perrittd3080ee2014-10-13 21:08:55 -0500132 options := &CopyOpts{Destination: "/newTestContainer/newTestObject"}
Jon Perritt0ce24ad2014-10-20 21:59:45 -0500133 res := Copy(fake.ServiceClient(), "testContainer", "testObject", options)
134 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500135}
136
137func TestDeleteObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500138 th.SetupHTTP()
139 defer th.TeardownHTTP()
140 HandleDeleteObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500141
Jon Perritt0ce24ad2014-10-20 21:59:45 -0500142 res := Delete(fake.ServiceClient(), "testContainer", "testObject", nil)
143 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500144}
145
146func TestUpateObjectMetadata(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500147 th.SetupHTTP()
148 defer th.TeardownHTTP()
149 HandleUpdateObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500150
Jon Perritt457f8ca2014-10-15 00:28:23 -0500151 options := &UpdateOpts{Metadata: map[string]string{"Gophercloud-Test": "objects"}}
Jon Perritt0ce24ad2014-10-20 21:59:45 -0500152 res := Update(fake.ServiceClient(), "testContainer", "testObject", options)
153 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500154}
155
Jon Perritt8c93a302014-09-28 22:35:57 -0500156func TestGetObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500157 th.SetupHTTP()
158 defer th.TeardownHTTP()
159 HandleGetObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500160
Jon Perritt457f8ca2014-10-15 00:28:23 -0500161 expected := map[string]string{"Gophercloud-Test": "objects"}
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200162 actual, err := Get(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractMetadata()
Jon Perritt457f8ca2014-10-15 00:28:23 -0500163 th.AssertNoErr(t, err)
164 th.CheckDeepEquals(t, expected, actual)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500165}