Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 1 | package objects |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 5 | "io" |
Brendan ODonnell | a69b347 | 2015-04-27 13:59:41 -0500 | [diff] [blame] | 6 | "strings" |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 7 | "testing" |
| 8 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 9 | "github.com/gophercloud/gophercloud/pagination" |
| 10 | th "github.com/gophercloud/gophercloud/testhelper" |
| 11 | fake "github.com/gophercloud/gophercloud/testhelper/client" |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 12 | ) |
| 13 | |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 14 | func TestDownloadReader(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 15 | th.SetupHTTP() |
| 16 | defer th.TeardownHTTP() |
| 17 | HandleDownloadObjectSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 18 | |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 19 | response := Download(fake.ServiceClient(), "testContainer", "testObject", nil) |
Jon Perritt | f9fd95b | 2014-10-27 13:17:23 -0500 | [diff] [blame] | 20 | defer response.Body.Close() |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 21 | |
| 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 | |
| 28 | func 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 Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 37 | th.AssertNoErr(t, err) |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 38 | th.CheckEquals(t, "Successful download with Gophercloud", string(bytes)) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | func TestListObjectInfo(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 42 | th.SetupHTTP() |
| 43 | defer th.TeardownHTTP() |
| 44 | HandleListObjectsInfoSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 45 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 46 | count := 0 |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 47 | options := &ListOpts{Full: true} |
| 48 | err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 49 | count++ |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 50 | actual, err := ExtractInfo(page) |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 51 | th.AssertNoErr(t, err) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 52 | |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 53 | th.CheckDeepEquals(t, ExpectedListInfo, actual) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 54 | |
| 55 | return true, nil |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 56 | }) |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 57 | th.AssertNoErr(t, err) |
| 58 | th.CheckEquals(t, count, 1) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | func TestListObjectNames(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 62 | th.SetupHTTP() |
| 63 | defer th.TeardownHTTP() |
| 64 | HandleListObjectNamesSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 65 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 66 | count := 0 |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 67 | options := &ListOpts{Full: false} |
| 68 | err := List(fake.ServiceClient(), "testContainer", options).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 69 | count++ |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 70 | actual, err := ExtractNames(page) |
| 71 | if err != nil { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 72 | t.Errorf("Failed to extract container names: %v", err) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 73 | return false, err |
| 74 | } |
| 75 | |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 76 | th.CheckDeepEquals(t, ExpectedListNames, actual) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 77 | |
| 78 | return true, nil |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 79 | }) |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 80 | th.AssertNoErr(t, err) |
| 81 | th.CheckEquals(t, count, 1) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 82 | } |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 83 | |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 84 | func TestCreateObject(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 85 | th.SetupHTTP() |
| 86 | defer th.TeardownHTTP() |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 87 | |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 88 | content := "Did gyre and gimble in the wabe" |
| 89 | |
| 90 | HandleCreateTextObjectSuccessfully(t, content) |
| 91 | |
Jon Perritt | 397ade6 | 2016-03-15 06:55:02 -0500 | [diff] [blame^] | 92 | options := &CreateOpts{ContentType: "text/plain", Content: strings.NewReader(content)} |
| 93 | res := Create(fake.ServiceClient(), "testContainer", "testObject", options) |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 94 | th.AssertNoErr(t, res.Err) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 95 | } |
| 96 | |
Ash Wilson | 93beae0 | 2015-01-23 14:14:48 -0500 | [diff] [blame] | 97 | func TestCreateObjectWithoutContentType(t *testing.T) { |
| 98 | th.SetupHTTP() |
| 99 | defer th.TeardownHTTP() |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 100 | |
| 101 | content := "The sky was the color of television, tuned to a dead channel." |
| 102 | |
| 103 | HandleCreateTypelessObjectSuccessfully(t, content) |
| 104 | |
Jon Perritt | 397ade6 | 2016-03-15 06:55:02 -0500 | [diff] [blame^] | 105 | res := Create(fake.ServiceClient(), "testContainer", "testObject", &CreateOpts{Content: strings.NewReader(content)}) |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 106 | th.AssertNoErr(t, res.Err) |
| 107 | } |
| 108 | |
Jon Perritt | 397ade6 | 2016-03-15 06:55:02 -0500 | [diff] [blame^] | 109 | /* |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 110 | func 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 Wilson | 93beae0 | 2015-01-23 14:14:48 -0500 | [diff] [blame] | 118 | |
Brendan ODonnell | a69b347 | 2015-04-27 13:59:41 -0500 | [diff] [blame] | 119 | content := strings.NewReader("The sky was the color of television, tuned to a dead channel.") |
Jon Perritt | 397ade6 | 2016-03-15 06:55:02 -0500 | [diff] [blame^] | 120 | res := Create(fake.ServiceClient(), "testContainer", "testObject", &CreateOpts{Content: content}) |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 121 | |
| 122 | err := fmt.Errorf("Local checksum does not match API ETag header") |
| 123 | th.AssertDeepEquals(t, err, res.Err) |
Ash Wilson | 93beae0 | 2015-01-23 14:14:48 -0500 | [diff] [blame] | 124 | } |
Jon Perritt | 397ade6 | 2016-03-15 06:55:02 -0500 | [diff] [blame^] | 125 | */ |
Ash Wilson | 93beae0 | 2015-01-23 14:14:48 -0500 | [diff] [blame] | 126 | |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 127 | func TestCopyObject(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 128 | th.SetupHTTP() |
| 129 | defer th.TeardownHTTP() |
| 130 | HandleCopyObjectSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 131 | |
Jon Perritt | d3080ee | 2014-10-13 21:08:55 -0500 | [diff] [blame] | 132 | options := &CopyOpts{Destination: "/newTestContainer/newTestObject"} |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 133 | res := Copy(fake.ServiceClient(), "testContainer", "testObject", options) |
| 134 | th.AssertNoErr(t, res.Err) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | func TestDeleteObject(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 138 | th.SetupHTTP() |
| 139 | defer th.TeardownHTTP() |
| 140 | HandleDeleteObjectSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 141 | |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 142 | res := Delete(fake.ServiceClient(), "testContainer", "testObject", nil) |
| 143 | th.AssertNoErr(t, res.Err) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | func TestUpateObjectMetadata(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 147 | th.SetupHTTP() |
| 148 | defer th.TeardownHTTP() |
| 149 | HandleUpdateObjectSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 150 | |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 151 | options := &UpdateOpts{Metadata: map[string]string{"Gophercloud-Test": "objects"}} |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 152 | res := Update(fake.ServiceClient(), "testContainer", "testObject", options) |
| 153 | th.AssertNoErr(t, res.Err) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 154 | } |
| 155 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 156 | func TestGetObject(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 157 | th.SetupHTTP() |
| 158 | defer th.TeardownHTTP() |
| 159 | HandleGetObjectSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 160 | |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 161 | expected := map[string]string{"Gophercloud-Test": "objects"} |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 162 | actual, err := Get(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractMetadata() |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 163 | th.AssertNoErr(t, err) |
| 164 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 165 | } |