blob: e6123198c1cbe266b82a5b712be952a96fbb9363 [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"
Brendan ODonnella69b3472015-04-27 13:59:41 -05006 "strings"
Jon Perrittf81e17a2014-09-15 01:29:41 -05007 "testing"
8
Jon Perritt27249f42016-02-18 10:35:59 -06009 "github.com/gophercloud/gophercloud/pagination"
10 th "github.com/gophercloud/gophercloud/testhelper"
11 fake "github.com/gophercloud/gophercloud/testhelper/client"
Jon Perrittf81e17a2014-09-15 01:29:41 -050012)
13
Jamie Hannaford2e784862014-10-27 10:40:27 +010014func TestDownloadReader(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050015 th.SetupHTTP()
16 defer th.TeardownHTTP()
17 HandleDownloadObjectSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050018
Jamie Hannaford2e784862014-10-27 10:40:27 +010019 response := Download(fake.ServiceClient(), "testContainer", "testObject", nil)
Jon Perrittf9fd95b2014-10-27 13:17:23 -050020 defer response.Body.Close()
Jamie Hannaford2e784862014-10-27 10:40:27 +010021
22 // Check reader
23 buf := bytes.NewBuffer(make([]byte, 0))
24 io.CopyN(buf, response.Body, 10)
25 th.CheckEquals(t, "Successful", string(buf.Bytes()))
26}
27
28func TestDownloadExtraction(t *testing.T) {
29 th.SetupHTTP()
30 defer th.TeardownHTTP()
31 HandleDownloadObjectSuccessfully(t)
32
33 response := Download(fake.ServiceClient(), "testContainer", "testObject", nil)
34
35 // Check []byte extraction
36 bytes, err := response.ExtractContent()
Jon Perritt457f8ca2014-10-15 00:28:23 -050037 th.AssertNoErr(t, err)
Jamie Hannaford2e784862014-10-27 10:40:27 +010038 th.CheckEquals(t, "Successful download with Gophercloud", string(bytes))
Jon Perrittf81e17a2014-09-15 01:29:41 -050039}
40
41func TestListObjectInfo(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050042 th.SetupHTTP()
43 defer th.TeardownHTTP()
44 HandleListObjectsInfoSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050045
Jon Perrittde47eac2014-09-30 15:34:17 -050046 count := 0
Jon Perritt457f8ca2014-10-15 00:28:23 -050047 options := &ListOpts{Full: true}
48 err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittde47eac2014-09-30 15:34:17 -050049 count++
Jon Perritt8c93a302014-09-28 22:35:57 -050050 actual, err := ExtractInfo(page)
Jon Perritt457f8ca2014-10-15 00:28:23 -050051 th.AssertNoErr(t, err)
Jon Perritt8c93a302014-09-28 22:35:57 -050052
Jon Perritt457f8ca2014-10-15 00:28:23 -050053 th.CheckDeepEquals(t, ExpectedListInfo, actual)
Jon Perritt8c93a302014-09-28 22:35:57 -050054
55 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -050056 })
Jon Perritt457f8ca2014-10-15 00:28:23 -050057 th.AssertNoErr(t, err)
58 th.CheckEquals(t, count, 1)
Jon Perrittf81e17a2014-09-15 01:29:41 -050059}
60
61func TestListObjectNames(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050062 th.SetupHTTP()
63 defer th.TeardownHTTP()
64 HandleListObjectNamesSuccessfully(t)
Jon Perrittf81e17a2014-09-15 01:29:41 -050065
Jon Perrittde47eac2014-09-30 15:34:17 -050066 count := 0
Jon Perritt457f8ca2014-10-15 00:28:23 -050067 options := &ListOpts{Full: false}
68 err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittde47eac2014-09-30 15:34:17 -050069 count++
Jon Perritt8c93a302014-09-28 22:35:57 -050070 actual, err := ExtractNames(page)
71 if err != nil {
Jon Perritt457f8ca2014-10-15 00:28:23 -050072 t.Errorf("Failed to extract container names: %v", err)
Jon Perritt8c93a302014-09-28 22:35:57 -050073 return false, err
74 }
75
Jon Perritt457f8ca2014-10-15 00:28:23 -050076 th.CheckDeepEquals(t, ExpectedListNames, actual)
Jon Perritt8c93a302014-09-28 22:35:57 -050077
78 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -050079 })
Jon Perritt457f8ca2014-10-15 00:28:23 -050080 th.AssertNoErr(t, err)
81 th.CheckEquals(t, count, 1)
Jon Perrittf81e17a2014-09-15 01:29:41 -050082}
Jon Perritt8aa40262014-09-29 15:41:32 -050083
Jon Perrittf81e17a2014-09-15 01:29:41 -050084func TestCreateObject(t *testing.T) {
Jon Perritt457f8ca2014-10-15 00:28:23 -050085 th.SetupHTTP()
86 defer th.TeardownHTTP()
Jon Perrittf81e17a2014-09-15 01:29:41 -050087
Jamie Hannaford08096232015-07-13 12:47:28 +020088 content := "Did gyre and gimble in the wabe"
89
90 HandleCreateTextObjectSuccessfully(t, content)
91
Jon Perritt397ade62016-03-15 06:55:02 -050092 options := &CreateOpts{ContentType: "text/plain", Content: strings.NewReader(content)}
93 res := Create(fake.ServiceClient(), "testContainer", "testObject", options)
Jon Perritt0ce24ad2014-10-20 21:59:45 -050094 th.AssertNoErr(t, res.Err)
Jon Perrittf81e17a2014-09-15 01:29:41 -050095}
96
Ash Wilson93beae02015-01-23 14:14:48 -050097func TestCreateObjectWithoutContentType(t *testing.T) {
98 th.SetupHTTP()
99 defer th.TeardownHTTP()
Jamie Hannaford08096232015-07-13 12:47:28 +0200100
101 content := "The sky was the color of television, tuned to a dead channel."
102
103 HandleCreateTypelessObjectSuccessfully(t, content)
104
Jon Perritt397ade62016-03-15 06:55:02 -0500105 res := Create(fake.ServiceClient(), "testContainer", "testObject", &CreateOpts{Content: strings.NewReader(content)})
Jamie Hannaford08096232015-07-13 12:47:28 +0200106 th.AssertNoErr(t, res.Err)
107}
108
Jon Perritt397ade62016-03-15 06:55:02 -0500109/*
Jamie Hannaford08096232015-07-13 12:47:28 +0200110func TestErrorIsRaisedForChecksumMismatch(t *testing.T) {
111 th.SetupHTTP()
112 defer th.TeardownHTTP()
113
114 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
115 w.Header().Set("ETag", "acbd18db4cc2f85cedef654fccc4a4d8")
116 w.WriteHeader(http.StatusCreated)
117 })
Ash Wilson93beae02015-01-23 14:14:48 -0500118
Brendan ODonnella69b3472015-04-27 13:59:41 -0500119 content := strings.NewReader("The sky was the color of television, tuned to a dead channel.")
Jon Perritt397ade62016-03-15 06:55:02 -0500120 res := Create(fake.ServiceClient(), "testContainer", "testObject", &CreateOpts{Content: content})
Jamie Hannaford08096232015-07-13 12:47:28 +0200121
122 err := fmt.Errorf("Local checksum does not match API ETag header")
123 th.AssertDeepEquals(t, err, res.Err)
Ash Wilson93beae02015-01-23 14:14:48 -0500124}
Jon Perritt397ade62016-03-15 06:55:02 -0500125*/
Ash Wilson93beae02015-01-23 14:14:48 -0500126
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}