Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame^] | 1 | package objects |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/testhelper" |
| 10 | ) |
| 11 | |
| 12 | const ( |
| 13 | tokenId = "abcabcabcabc" |
| 14 | ) |
| 15 | |
| 16 | var metadata = map[string]string{"gophercloud-test": "objects"} |
| 17 | |
| 18 | func serviceClient() *gophercloud.ServiceClient { |
| 19 | return &gophercloud.ServiceClient{ |
| 20 | Provider: &gophercloud.ProviderClient{TokenID: tokenId}, |
| 21 | Endpoint: testhelper.Endpoint(), |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func TestDownloadObject(t * testing.T) { |
| 26 | testhelper.SetupHTTP() |
| 27 | defer testhelper.TeardownHTTP() |
| 28 | |
| 29 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 30 | testhelper.TestMethod(t, r, "GET") |
| 31 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 32 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 33 | }) |
| 34 | |
| 35 | client := serviceClient() |
| 36 | _, err := Download(client, DownloadOpts{ |
| 37 | Container: "testContainer", |
| 38 | Name: "testObject", |
| 39 | }) |
| 40 | if err != nil { |
| 41 | t.Fatalf("Unexpected error downloading object: %v", err) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func TestListObjectInfo(t *testing.T) { |
| 46 | testhelper.SetupHTTP() |
| 47 | defer testhelper.TeardownHTTP() |
| 48 | |
| 49 | testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 50 | testhelper.TestMethod(t, r, "GET") |
| 51 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 52 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 53 | }) |
| 54 | |
| 55 | client := serviceClient() |
| 56 | _, err := List(client, ListOpts{ |
| 57 | Full: true, |
| 58 | Container: "testContainer", |
| 59 | }) |
| 60 | if err != nil { |
| 61 | t.Fatalf("Unexpected error listing objects info: %v", err) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestListObjectNames(t *testing.T) { |
| 66 | testhelper.SetupHTTP() |
| 67 | defer testhelper.TeardownHTTP() |
| 68 | |
| 69 | testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 70 | testhelper.TestMethod(t, r, "GET") |
| 71 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 72 | testhelper.TestHeader(t, r, "Accept", "text/plain") |
| 73 | }) |
| 74 | |
| 75 | client := serviceClient() |
| 76 | _, err := List(client, ListOpts{ |
| 77 | Container: "testContainer", |
| 78 | }) |
| 79 | if err != nil { |
| 80 | t.Fatalf("Unexpected error listing object names: %v", err) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestCreateObject(t *testing.T) { |
| 85 | testhelper.SetupHTTP() |
| 86 | defer testhelper.TeardownHTTP() |
| 87 | |
| 88 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 89 | testhelper.TestMethod(t, r, "PUT") |
| 90 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 91 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 92 | w.WriteHeader(http.StatusCreated) |
| 93 | }) |
| 94 | |
| 95 | client := serviceClient() |
| 96 | err := Create(client, CreateOpts{ |
| 97 | Content: bytes.NewBufferString("Did gyre and gimble in the wabe:"), |
| 98 | Container: "testContainer", |
| 99 | Name: "testObject", |
| 100 | }) |
| 101 | if err != nil { |
| 102 | t.Fatalf("Unexpected error creating object: %v", err) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | func TestCopyObject(t *testing.T) { |
| 107 | testhelper.SetupHTTP() |
| 108 | defer testhelper.TeardownHTTP() |
| 109 | |
| 110 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 111 | testhelper.TestMethod(t, r, "COPY") |
| 112 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 113 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 114 | testhelper.TestHeader(t, r, "Destination", "/newTestContainer/newTestObject") |
| 115 | w.WriteHeader(http.StatusCreated) |
| 116 | }) |
| 117 | |
| 118 | client := serviceClient() |
| 119 | err := Copy(client, CopyOpts{ |
| 120 | NewContainer: "newTestContainer", |
| 121 | NewName: "newTestObject", |
| 122 | Container: "testContainer", |
| 123 | Name: "testObject", |
| 124 | }) |
| 125 | if err != nil { |
| 126 | t.Fatalf("Unexpected error copying object: %v", err) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func TestDeleteObject(t *testing.T) { |
| 131 | testhelper.SetupHTTP() |
| 132 | defer testhelper.TeardownHTTP() |
| 133 | |
| 134 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 135 | testhelper.TestMethod(t, r, "DELETE") |
| 136 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 137 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 138 | w.WriteHeader(http.StatusNoContent) |
| 139 | }) |
| 140 | |
| 141 | client := serviceClient() |
| 142 | err := Delete(client, DeleteOpts{ |
| 143 | Container: "testContainer", |
| 144 | Name: "testObject", |
| 145 | }) |
| 146 | if err != nil { |
| 147 | t.Fatalf("Unexpected error deleting object: %v", err) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | func TestUpateObjectMetadata(t *testing.T) { |
| 152 | testhelper.SetupHTTP() |
| 153 | defer testhelper.TeardownHTTP() |
| 154 | |
| 155 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 156 | testhelper.TestMethod(t, r, "POST") |
| 157 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 158 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 159 | testhelper.TestHeader(t, r, "X-Object-Meta-Gophercloud-Test", "objects") |
| 160 | w.WriteHeader(http.StatusAccepted) |
| 161 | }) |
| 162 | |
| 163 | client := serviceClient() |
| 164 | err := Update(client, UpdateOpts{ |
| 165 | Container: "testContainer", |
| 166 | Name: "testObject", |
| 167 | Metadata: metadata, |
| 168 | }) |
| 169 | if err != nil { |
| 170 | t.Fatalf("Unexpected error updating object metadata: %v", err) |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | func TestGetContainer(t *testing.T) { |
| 175 | testhelper.SetupHTTP() |
| 176 | defer testhelper.TeardownHTTP() |
| 177 | |
| 178 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 179 | testhelper.TestMethod(t, r, "HEAD") |
| 180 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 181 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 182 | w.WriteHeader(http.StatusNoContent) |
| 183 | }) |
| 184 | |
| 185 | client := serviceClient() |
| 186 | _, err := Get(client, GetOpts{ |
| 187 | Container: "testContainer", |
| 188 | Name: "testObject", |
| 189 | }) |
| 190 | if err != nil { |
| 191 | t.Fatalf("Unexpected error getting object metadata: %v", err) |
| 192 | } |
| 193 | } |