Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 1 | package objects |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 5 | "fmt" |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 6 | "io" |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 7 | "net/http" |
Brendan ODonnell | a69b347 | 2015-04-27 13:59:41 -0500 | [diff] [blame] | 8 | "strings" |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 9 | "testing" |
| 10 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 11 | "github.com/gophercloud/gophercloud/pagination" |
| 12 | th "github.com/gophercloud/gophercloud/testhelper" |
| 13 | fake "github.com/gophercloud/gophercloud/testhelper/client" |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 14 | ) |
| 15 | |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 16 | func TestDownloadReader(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 17 | th.SetupHTTP() |
| 18 | defer th.TeardownHTTP() |
| 19 | HandleDownloadObjectSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 20 | |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 21 | response := Download(fake.ServiceClient(), "testContainer", "testObject", nil) |
Jon Perritt | f9fd95b | 2014-10-27 13:17:23 -0500 | [diff] [blame] | 22 | defer response.Body.Close() |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 23 | |
| 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 | |
| 30 | func 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 Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 39 | th.AssertNoErr(t, err) |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 40 | th.CheckEquals(t, "Successful download with Gophercloud", string(bytes)) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | func TestListObjectInfo(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 44 | th.SetupHTTP() |
| 45 | defer th.TeardownHTTP() |
| 46 | HandleListObjectsInfoSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 47 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 48 | count := 0 |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 49 | options := &ListOpts{Full: true} |
| 50 | 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] | 51 | count++ |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 52 | actual, err := ExtractInfo(page) |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 53 | th.AssertNoErr(t, err) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 54 | |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 55 | th.CheckDeepEquals(t, ExpectedListInfo, actual) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 56 | |
| 57 | return true, nil |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 58 | }) |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 59 | th.AssertNoErr(t, err) |
| 60 | th.CheckEquals(t, count, 1) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | func TestListObjectNames(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 64 | th.SetupHTTP() |
| 65 | defer th.TeardownHTTP() |
| 66 | HandleListObjectNamesSuccessfully(t) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 67 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 68 | count := 0 |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 69 | options := &ListOpts{Full: false} |
| 70 | 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] | 71 | count++ |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 72 | actual, err := ExtractNames(page) |
| 73 | if err != nil { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 74 | t.Errorf("Failed to extract container names: %v", err) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 75 | return false, err |
| 76 | } |
| 77 | |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 78 | th.CheckDeepEquals(t, ExpectedListNames, actual) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 79 | |
| 80 | return true, nil |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 81 | }) |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 82 | th.AssertNoErr(t, err) |
| 83 | th.CheckEquals(t, count, 1) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 84 | } |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 85 | |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 86 | func TestCreateObject(t *testing.T) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 87 | th.SetupHTTP() |
| 88 | defer th.TeardownHTTP() |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 89 | |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 90 | content := "Did gyre and gimble in the wabe" |
| 91 | |
| 92 | HandleCreateTextObjectSuccessfully(t, content) |
| 93 | |
Ash Wilson | 93beae0 | 2015-01-23 14:14:48 -0500 | [diff] [blame] | 94 | options := &CreateOpts{ContentType: "text/plain"} |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 95 | res := Create(fake.ServiceClient(), "testContainer", "testObject", strings.NewReader(content), options) |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 96 | th.AssertNoErr(t, res.Err) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 97 | } |
| 98 | |
Ash Wilson | 93beae0 | 2015-01-23 14:14:48 -0500 | [diff] [blame] | 99 | func TestCreateObjectWithoutContentType(t *testing.T) { |
| 100 | th.SetupHTTP() |
| 101 | defer th.TeardownHTTP() |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 102 | |
| 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 | |
| 111 | func 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 Wilson | 93beae0 | 2015-01-23 14:14:48 -0500 | [diff] [blame] | 119 | |
Brendan ODonnell | a69b347 | 2015-04-27 13:59:41 -0500 | [diff] [blame] | 120 | content := strings.NewReader("The sky was the color of television, tuned to a dead channel.") |
Ash Wilson | 93beae0 | 2015-01-23 14:14:48 -0500 | [diff] [blame] | 121 | res := Create(fake.ServiceClient(), "testContainer", "testObject", content, &CreateOpts{}) |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 122 | |
| 123 | err := fmt.Errorf("Local checksum does not match API ETag header") |
| 124 | th.AssertDeepEquals(t, err, res.Err) |
Ash Wilson | 93beae0 | 2015-01-23 14:14:48 -0500 | [diff] [blame] | 125 | } |
| 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 | } |