OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/glue/resource_request_body.h" | 5 #include "content/browser/loader/upload_data_stream_builder.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
12 #include "base/message_loop/message_loop_proxy.h" | 12 #include "base/message_loop/message_loop_proxy.h" |
13 #include "base/time.h" | 13 #include "base/time.h" |
14 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
15 #include "net/base/upload_bytes_element_reader.h" | 15 #include "net/base/upload_bytes_element_reader.h" |
16 #include "net/base/upload_data_stream.h" | 16 #include "net/base/upload_data_stream.h" |
17 #include "net/base/upload_file_element_reader.h" | 17 #include "net/base/upload_file_element_reader.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "webkit/browser/blob/blob_storage_controller.h" | 19 #include "webkit/browser/blob/blob_storage_controller.h" |
| 20 #include "webkit/common/resource_request_body.h" |
20 | 21 |
21 using webkit_blob::BlobData; | 22 using webkit_blob::BlobData; |
22 using webkit_blob::BlobStorageController; | 23 using webkit_blob::BlobStorageController; |
| 24 using webkit_glue::ResourceRequestBody; |
23 | 25 |
24 namespace webkit_glue { | 26 namespace content { |
25 | |
26 namespace { | 27 namespace { |
27 | 28 |
28 bool AreElementsEqual(const net::UploadElementReader& reader, | 29 bool AreElementsEqual(const net::UploadElementReader& reader, |
29 const ResourceRequestBody::Element& element) { | 30 const ResourceRequestBody::Element& element) { |
30 switch(element.type()) { | 31 switch(element.type()) { |
31 case ResourceRequestBody::Element::TYPE_BYTES: { | 32 case ResourceRequestBody::Element::TYPE_BYTES: { |
32 const net::UploadBytesElementReader* bytes_reader = | 33 const net::UploadBytesElementReader* bytes_reader = |
33 reader.AsBytesReader(); | 34 reader.AsBytesReader(); |
34 return bytes_reader && | 35 return bytes_reader && |
35 element.length() == bytes_reader->length() && | 36 element.length() == bytes_reader->length() && |
(...skipping 11 matching lines...) Expand all Loading... |
47 break; | 48 break; |
48 } | 49 } |
49 default: | 50 default: |
50 NOTREACHED(); | 51 NOTREACHED(); |
51 } | 52 } |
52 return false; | 53 return false; |
53 } | 54 } |
54 | 55 |
55 } // namespace | 56 } // namespace |
56 | 57 |
57 TEST(ResourceRequestBodyTest, CreateUploadDataStreamWithoutBlob) { | 58 TEST(UploadDataStreamBuilderTest, CreateUploadDataStreamWithoutBlob) { |
58 base::MessageLoop message_loop; | 59 base::MessageLoop message_loop; |
59 scoped_refptr<ResourceRequestBody> request_body = new ResourceRequestBody; | 60 scoped_refptr<ResourceRequestBody> request_body = new ResourceRequestBody; |
60 | 61 |
61 const char kData[] = "123"; | 62 const char kData[] = "123"; |
62 const base::FilePath::StringType kFilePath = FILE_PATH_LITERAL("abc"); | 63 const base::FilePath::StringType kFilePath = FILE_PATH_LITERAL("abc"); |
63 const uint64 kFileOffset = 10U; | 64 const uint64 kFileOffset = 10U; |
64 const uint64 kFileLength = 100U; | 65 const uint64 kFileLength = 100U; |
65 const base::Time kFileTime = base::Time::FromDoubleT(999); | 66 const base::Time kFileTime = base::Time::FromDoubleT(999); |
66 const int64 kIdentifier = 12345; | 67 const int64 kIdentifier = 12345; |
67 | 68 |
68 request_body->AppendBytes(kData, arraysize(kData) - 1); | 69 request_body->AppendBytes(kData, arraysize(kData) - 1); |
69 request_body->AppendFileRange(base::FilePath(kFilePath), | 70 request_body->AppendFileRange(base::FilePath(kFilePath), |
70 kFileOffset, kFileLength, kFileTime); | 71 kFileOffset, kFileLength, kFileTime); |
71 request_body->set_identifier(kIdentifier); | 72 request_body->set_identifier(kIdentifier); |
72 | 73 |
73 scoped_ptr<net::UploadDataStream> upload( | 74 scoped_ptr<net::UploadDataStream> upload( |
74 request_body->ResolveElementsAndCreateUploadDataStream( | 75 UploadDataStreamBuilder::Build( |
75 NULL, NULL, base::MessageLoopProxy::current().get())); | 76 request_body, NULL, NULL, base::MessageLoopProxy::current().get())); |
76 | 77 |
77 EXPECT_EQ(kIdentifier, upload->identifier()); | 78 EXPECT_EQ(kIdentifier, upload->identifier()); |
78 ASSERT_EQ(request_body->elements()->size(), upload->element_readers().size()); | 79 ASSERT_EQ(request_body->elements()->size(), upload->element_readers().size()); |
79 | 80 |
80 const net::UploadBytesElementReader* r1 = | 81 const net::UploadBytesElementReader* r1 = |
81 upload->element_readers()[0]->AsBytesReader(); | 82 upload->element_readers()[0]->AsBytesReader(); |
82 ASSERT_TRUE(r1); | 83 ASSERT_TRUE(r1); |
83 EXPECT_EQ(kData, std::string(r1->bytes(), r1->length())); | 84 EXPECT_EQ(kData, std::string(r1->bytes(), r1->length())); |
84 | 85 |
85 const net::UploadFileElementReader* r2 = | 86 const net::UploadFileElementReader* r2 = |
86 upload->element_readers()[1]->AsFileReader(); | 87 upload->element_readers()[1]->AsFileReader(); |
87 ASSERT_TRUE(r2); | 88 ASSERT_TRUE(r2); |
88 EXPECT_EQ(kFilePath, r2->path().value()); | 89 EXPECT_EQ(kFilePath, r2->path().value()); |
89 EXPECT_EQ(kFileOffset, r2->range_offset()); | 90 EXPECT_EQ(kFileOffset, r2->range_offset()); |
90 EXPECT_EQ(kFileLength, r2->range_length()); | 91 EXPECT_EQ(kFileLength, r2->range_length()); |
91 EXPECT_EQ(kFileTime, r2->expected_modification_time()); | 92 EXPECT_EQ(kFileTime, r2->expected_modification_time()); |
92 } | 93 } |
93 | 94 |
94 TEST(ResourceRequestBodyTest, ResolveBlobAndCreateUploadDataStream) { | 95 TEST(UploadDataStreamBuilderTest, ResolveBlobAndCreateUploadDataStream) { |
95 base::MessageLoop message_loop; | 96 base::MessageLoop message_loop; |
96 // Setup blob data for testing. | 97 // Setup blob data for testing. |
97 base::Time time1, time2; | 98 base::Time time1, time2; |
98 base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", &time1); | 99 base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", &time1); |
99 base::Time::FromString("Mon, 14 Nov 1994, 11:30:49 GMT", &time2); | 100 base::Time::FromString("Mon, 14 Nov 1994, 11:30:49 GMT", &time2); |
100 | 101 |
101 BlobStorageController blob_storage_controller; | 102 BlobStorageController blob_storage_controller; |
102 scoped_refptr<BlobData> blob_data(new BlobData()); | 103 scoped_refptr<BlobData> blob_data(new BlobData()); |
103 | 104 |
104 GURL blob_url0("blob://url_0"); | 105 GURL blob_url0("blob://url_0"); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 | 137 |
137 // Test no blob reference. | 138 // Test no blob reference. |
138 scoped_refptr<ResourceRequestBody> request_body(new ResourceRequestBody()); | 139 scoped_refptr<ResourceRequestBody> request_body(new ResourceRequestBody()); |
139 request_body->AppendBytes(upload_element1.bytes(), upload_element1.length()); | 140 request_body->AppendBytes(upload_element1.bytes(), upload_element1.length()); |
140 request_body->AppendFileRange(upload_element2.path(), | 141 request_body->AppendFileRange(upload_element2.path(), |
141 upload_element2.offset(), | 142 upload_element2.offset(), |
142 upload_element2.length(), | 143 upload_element2.length(), |
143 upload_element2.expected_modification_time()); | 144 upload_element2.expected_modification_time()); |
144 | 145 |
145 scoped_ptr<net::UploadDataStream> upload( | 146 scoped_ptr<net::UploadDataStream> upload( |
146 request_body->ResolveElementsAndCreateUploadDataStream( | 147 UploadDataStreamBuilder::Build( |
| 148 request_body, |
147 &blob_storage_controller, | 149 &blob_storage_controller, |
148 NULL, | 150 NULL, |
149 base::MessageLoopProxy::current().get())); | 151 base::MessageLoopProxy::current().get())); |
150 | 152 |
151 ASSERT_EQ(2U, upload->element_readers().size()); | 153 ASSERT_EQ(2U, upload->element_readers().size()); |
152 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[0], upload_element1)); | 154 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[0], upload_element1)); |
153 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[1], upload_element2)); | 155 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[1], upload_element2)); |
154 | 156 |
155 // Test having only one blob reference that refers to empty blob data. | 157 // Test having only one blob reference that refers to empty blob data. |
156 request_body = new ResourceRequestBody(); | 158 request_body = new ResourceRequestBody(); |
157 request_body->AppendBlob(blob_url0); | 159 request_body->AppendBlob(blob_url0); |
158 | 160 |
159 upload.reset(request_body->ResolveElementsAndCreateUploadDataStream( | 161 upload = UploadDataStreamBuilder::Build( |
160 &blob_storage_controller, NULL, base::MessageLoopProxy::current().get())); | 162 request_body, |
| 163 &blob_storage_controller, |
| 164 NULL, |
| 165 base::MessageLoopProxy::current().get()); |
161 ASSERT_EQ(0U, upload->element_readers().size()); | 166 ASSERT_EQ(0U, upload->element_readers().size()); |
162 | 167 |
163 // Test having only one blob reference. | 168 // Test having only one blob reference. |
164 request_body = new ResourceRequestBody(); | 169 request_body = new ResourceRequestBody(); |
165 request_body->AppendBlob(blob_url1); | 170 request_body->AppendBlob(blob_url1); |
166 | 171 |
167 upload.reset(request_body->ResolveElementsAndCreateUploadDataStream( | 172 upload = UploadDataStreamBuilder::Build( |
168 &blob_storage_controller, NULL, base::MessageLoopProxy::current().get())); | 173 request_body, |
| 174 &blob_storage_controller, |
| 175 NULL, |
| 176 base::MessageLoopProxy::current().get()); |
169 ASSERT_EQ(2U, upload->element_readers().size()); | 177 ASSERT_EQ(2U, upload->element_readers().size()); |
170 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[0], blob_element1)); | 178 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[0], blob_element1)); |
171 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[1], blob_element2)); | 179 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[1], blob_element2)); |
172 | 180 |
173 // Test having one blob reference at the beginning. | 181 // Test having one blob reference at the beginning. |
174 request_body = new ResourceRequestBody(); | 182 request_body = new ResourceRequestBody(); |
175 request_body->AppendBlob(blob_url1); | 183 request_body->AppendBlob(blob_url1); |
176 request_body->AppendBytes(upload_element1.bytes(), upload_element1.length()); | 184 request_body->AppendBytes(upload_element1.bytes(), upload_element1.length()); |
177 request_body->AppendFileRange(upload_element2.path(), | 185 request_body->AppendFileRange(upload_element2.path(), |
178 upload_element2.offset(), | 186 upload_element2.offset(), |
179 upload_element2.length(), | 187 upload_element2.length(), |
180 upload_element2.expected_modification_time()); | 188 upload_element2.expected_modification_time()); |
181 | 189 |
182 upload.reset(request_body->ResolveElementsAndCreateUploadDataStream( | 190 upload = UploadDataStreamBuilder::Build( |
183 &blob_storage_controller, NULL, base::MessageLoopProxy::current().get())); | 191 request_body, |
| 192 &blob_storage_controller, |
| 193 NULL, |
| 194 base::MessageLoopProxy::current().get()); |
184 ASSERT_EQ(4U, upload->element_readers().size()); | 195 ASSERT_EQ(4U, upload->element_readers().size()); |
185 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[0], blob_element1)); | 196 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[0], blob_element1)); |
186 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[1], blob_element2)); | 197 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[1], blob_element2)); |
187 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[2], upload_element1)); | 198 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[2], upload_element1)); |
188 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[3], upload_element2)); | 199 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[3], upload_element2)); |
189 | 200 |
190 // Test having one blob reference at the end. | 201 // Test having one blob reference at the end. |
191 request_body = new ResourceRequestBody(); | 202 request_body = new ResourceRequestBody(); |
192 request_body->AppendBytes(upload_element1.bytes(), upload_element1.length()); | 203 request_body->AppendBytes(upload_element1.bytes(), upload_element1.length()); |
193 request_body->AppendFileRange(upload_element2.path(), | 204 request_body->AppendFileRange(upload_element2.path(), |
194 upload_element2.offset(), | 205 upload_element2.offset(), |
195 upload_element2.length(), | 206 upload_element2.length(), |
196 upload_element2.expected_modification_time()); | 207 upload_element2.expected_modification_time()); |
197 request_body->AppendBlob(blob_url1); | 208 request_body->AppendBlob(blob_url1); |
198 | 209 |
199 upload.reset(request_body->ResolveElementsAndCreateUploadDataStream( | 210 upload = UploadDataStreamBuilder::Build( |
200 &blob_storage_controller, NULL, base::MessageLoopProxy::current().get())); | 211 request_body, |
| 212 &blob_storage_controller, |
| 213 NULL, |
| 214 base::MessageLoopProxy::current().get()); |
201 ASSERT_EQ(4U, upload->element_readers().size()); | 215 ASSERT_EQ(4U, upload->element_readers().size()); |
202 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[0], upload_element1)); | 216 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[0], upload_element1)); |
203 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[1], upload_element2)); | 217 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[1], upload_element2)); |
204 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[2], blob_element1)); | 218 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[2], blob_element1)); |
205 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[3], blob_element2)); | 219 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[3], blob_element2)); |
206 | 220 |
207 // Test having one blob reference in the middle. | 221 // Test having one blob reference in the middle. |
208 request_body = new ResourceRequestBody(); | 222 request_body = new ResourceRequestBody(); |
209 request_body->AppendBytes(upload_element1.bytes(), upload_element1.length()); | 223 request_body->AppendBytes(upload_element1.bytes(), upload_element1.length()); |
210 request_body->AppendBlob(blob_url1); | 224 request_body->AppendBlob(blob_url1); |
211 request_body->AppendFileRange(upload_element2.path(), | 225 request_body->AppendFileRange(upload_element2.path(), |
212 upload_element2.offset(), | 226 upload_element2.offset(), |
213 upload_element2.length(), | 227 upload_element2.length(), |
214 upload_element2.expected_modification_time()); | 228 upload_element2.expected_modification_time()); |
215 | 229 |
216 upload.reset(request_body->ResolveElementsAndCreateUploadDataStream( | 230 upload = UploadDataStreamBuilder::Build( |
217 &blob_storage_controller, NULL, base::MessageLoopProxy::current().get())); | 231 request_body, |
| 232 &blob_storage_controller, |
| 233 NULL, |
| 234 base::MessageLoopProxy::current().get()); |
218 ASSERT_EQ(4U, upload->element_readers().size()); | 235 ASSERT_EQ(4U, upload->element_readers().size()); |
219 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[0], upload_element1)); | 236 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[0], upload_element1)); |
220 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[1], blob_element1)); | 237 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[1], blob_element1)); |
221 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[2], blob_element2)); | 238 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[2], blob_element2)); |
222 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[3], upload_element2)); | 239 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[3], upload_element2)); |
223 | 240 |
224 // Test having multiple blob references. | 241 // Test having multiple blob references. |
225 request_body = new ResourceRequestBody(); | 242 request_body = new ResourceRequestBody(); |
226 request_body->AppendBlob(blob_url1); | 243 request_body->AppendBlob(blob_url1); |
227 request_body->AppendBytes(upload_element1.bytes(), upload_element1.length()); | 244 request_body->AppendBytes(upload_element1.bytes(), upload_element1.length()); |
228 request_body->AppendBlob(blob_url2); | 245 request_body->AppendBlob(blob_url2); |
229 request_body->AppendBlob(blob_url3); | 246 request_body->AppendBlob(blob_url3); |
230 request_body->AppendFileRange(upload_element2.path(), | 247 request_body->AppendFileRange(upload_element2.path(), |
231 upload_element2.offset(), | 248 upload_element2.offset(), |
232 upload_element2.length(), | 249 upload_element2.length(), |
233 upload_element2.expected_modification_time()); | 250 upload_element2.expected_modification_time()); |
234 | 251 |
235 upload.reset(request_body->ResolveElementsAndCreateUploadDataStream( | 252 upload = UploadDataStreamBuilder::Build( |
236 &blob_storage_controller, NULL, base::MessageLoopProxy::current().get())); | 253 request_body, |
| 254 &blob_storage_controller, |
| 255 NULL, |
| 256 base::MessageLoopProxy::current().get()); |
237 ASSERT_EQ(8U, upload->element_readers().size()); | 257 ASSERT_EQ(8U, upload->element_readers().size()); |
238 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[0], blob_element1)); | 258 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[0], blob_element1)); |
239 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[1], blob_element2)); | 259 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[1], blob_element2)); |
240 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[2], upload_element1)); | 260 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[2], upload_element1)); |
241 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[3], blob_element1)); | 261 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[3], blob_element1)); |
242 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[4], blob_element2)); | 262 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[4], blob_element2)); |
243 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[5], blob_element1)); | 263 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[5], blob_element1)); |
244 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[6], blob_element2)); | 264 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[6], blob_element2)); |
245 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[7], upload_element2)); | 265 EXPECT_TRUE(AreElementsEqual(*upload->element_readers()[7], upload_element2)); |
246 } | 266 } |
247 | 267 |
248 } // namespace webkit_glue | 268 } // namespace content |
OLD | NEW |