OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <stack> |
| 6 #include <utility> |
| 7 |
| 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" |
| 10 #include "base/scoped_temp_dir.h" |
| 11 #include "base/task.h" |
| 12 #include "base/thread.h" |
| 13 #include "base/time.h" |
| 14 #include "base/ref_counted.h" |
| 15 #include "base/scoped_ptr.h" |
| 16 #include "base/waitable_event.h" |
| 17 #include "net/base/file_stream.h" |
| 18 #include "net/base/io_buffer.h" |
| 19 #include "net/base/net_errors.h" |
| 20 #include "net/http/http_request_headers.h" |
| 21 #include "net/http/http_response_headers.h" |
| 22 #include "net/url_request/url_request.h" |
| 23 #include "net/url_request/url_request_error_job.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "webkit/blob/blob_data.h" |
| 26 #include "webkit/blob/blob_url_request_job.h" |
| 27 |
| 28 namespace webkit_blob { |
| 29 |
| 30 static const int kBufferSize = 1024; |
| 31 static const char kTestData1[] = "Hello"; |
| 32 static const char kTestData2[] = "Here it is data."; |
| 33 static const char kTestFileData1[] = "0123456789"; |
| 34 static const char kTestFileData2[] = "This is sample file."; |
| 35 static const char kTestContentType[] = "foo/bar"; |
| 36 static const char kTestContentDisposition[] = "attachment; filename=foo.txt"; |
| 37 |
| 38 class BlobURLRequestJobTest : public testing::Test { |
| 39 public: |
| 40 |
| 41 // Test Harness ------------------------------------------------------------- |
| 42 // TODO(jianli): share this test harness with AppCacheURLRequestJobTest |
| 43 |
| 44 class MockURLRequestDelegate : public URLRequest::Delegate { |
| 45 public: |
| 46 explicit MockURLRequestDelegate(BlobURLRequestJobTest* test) |
| 47 : test_(test), |
| 48 received_data_(new net::IOBuffer(kBufferSize)) { |
| 49 } |
| 50 |
| 51 virtual void OnResponseStarted(URLRequest* request) { |
| 52 if (request->status().is_success()) { |
| 53 EXPECT_TRUE(request->response_headers()); |
| 54 ReadSome(request); |
| 55 } else { |
| 56 RequestComplete(); |
| 57 } |
| 58 } |
| 59 |
| 60 virtual void OnReadCompleted(URLRequest* request, int bytes_read) { |
| 61 if (bytes_read > 0) |
| 62 ReceiveData(request, bytes_read); |
| 63 else |
| 64 RequestComplete(); |
| 65 } |
| 66 |
| 67 const std::string& response_data() const { return response_data_; } |
| 68 |
| 69 private: |
| 70 void ReadSome(URLRequest* request) { |
| 71 if (request->job()->is_done()) { |
| 72 RequestComplete(); |
| 73 return; |
| 74 } |
| 75 |
| 76 int bytes_read = 0; |
| 77 if (!request->Read(received_data_.get(), kBufferSize, &bytes_read)) { |
| 78 if (!request->status().is_io_pending()) { |
| 79 RequestComplete(); |
| 80 } |
| 81 return; |
| 82 } |
| 83 |
| 84 ReceiveData(request, bytes_read); |
| 85 } |
| 86 |
| 87 void ReceiveData(URLRequest* request, int bytes_read) { |
| 88 if (bytes_read) { |
| 89 response_data_.append(received_data_->data(), |
| 90 static_cast<size_t>(bytes_read)); |
| 91 ReadSome(request); |
| 92 } else { |
| 93 RequestComplete(); |
| 94 } |
| 95 } |
| 96 |
| 97 void RequestComplete() { |
| 98 test_->ScheduleNextTask(); |
| 99 } |
| 100 |
| 101 BlobURLRequestJobTest* test_; |
| 102 scoped_refptr<net::IOBuffer> received_data_; |
| 103 std::string response_data_; |
| 104 }; |
| 105 |
| 106 // Helper class run a test on our io_thread. The io_thread |
| 107 // is spun up once and reused for all tests. |
| 108 template <class Method> |
| 109 class WrapperTask : public Task { |
| 110 public: |
| 111 WrapperTask(BlobURLRequestJobTest* test, Method method) |
| 112 : test_(test), method_(method) { |
| 113 } |
| 114 |
| 115 virtual void Run() { |
| 116 test_->SetUpTest(); |
| 117 (test_->*method_)(); |
| 118 } |
| 119 |
| 120 private: |
| 121 BlobURLRequestJobTest* test_; |
| 122 Method method_; |
| 123 }; |
| 124 |
| 125 static void SetUpTestCase() { |
| 126 temp_dir_.reset(new ScopedTempDir()); |
| 127 ASSERT_TRUE(temp_dir_->CreateUniqueTempDir()); |
| 128 |
| 129 temp_file1_ = temp_dir_->path().AppendASCII("BlobFile1.dat"); |
| 130 file_util::WriteFile(temp_file1_, kTestFileData1, |
| 131 arraysize(kTestFileData1) - 1); |
| 132 file_util::FileInfo file_info1; |
| 133 file_util::GetFileInfo(temp_file1_, &file_info1); |
| 134 temp_file_modification_time1_ = file_info1.last_modified; |
| 135 |
| 136 temp_file2_ = temp_dir_->path().AppendASCII("BlobFile2.dat"); |
| 137 file_util::WriteFile(temp_file2_, kTestFileData2, |
| 138 arraysize(kTestFileData2) - 1); |
| 139 file_util::FileInfo file_info2; |
| 140 file_util::GetFileInfo(temp_file2_, &file_info2); |
| 141 temp_file_modification_time2_ = file_info2.last_modified; |
| 142 |
| 143 io_thread_.reset(new base::Thread("BlobRLRequestJobTest Thread")); |
| 144 base::Thread::Options options(MessageLoop::TYPE_IO, 0); |
| 145 io_thread_->StartWithOptions(options); |
| 146 } |
| 147 |
| 148 static void TearDownTestCase() { |
| 149 io_thread_.reset(NULL); |
| 150 temp_dir_.reset(NULL); |
| 151 } |
| 152 |
| 153 static URLRequestJob* BlobURLRequestJobFactory(URLRequest* request, |
| 154 const std::string& scheme) { |
| 155 BlobURLRequestJob* temp = blob_url_request_job_; |
| 156 blob_url_request_job_ = NULL; |
| 157 return temp; |
| 158 } |
| 159 |
| 160 BlobURLRequestJobTest() |
| 161 : expected_status_code_(0) { |
| 162 } |
| 163 |
| 164 template <class Method> |
| 165 void RunTestOnIOThread(Method method) { |
| 166 test_finished_event_ .reset(new base::WaitableEvent(false, false)); |
| 167 io_thread_->message_loop()->PostTask( |
| 168 FROM_HERE, new WrapperTask<Method>(this, method)); |
| 169 test_finished_event_->Wait(); |
| 170 } |
| 171 |
| 172 void SetUpTest() { |
| 173 DCHECK(MessageLoop::current() == io_thread_->message_loop()); |
| 174 |
| 175 URLRequest::RegisterProtocolFactory("blob", &BlobURLRequestJobFactory); |
| 176 url_request_delegate_.reset(new MockURLRequestDelegate(this)); |
| 177 } |
| 178 |
| 179 void TearDownTest() { |
| 180 DCHECK(MessageLoop::current() == io_thread_->message_loop()); |
| 181 |
| 182 request_.reset(); |
| 183 url_request_delegate_.reset(); |
| 184 |
| 185 DCHECK(!blob_url_request_job_); |
| 186 URLRequest::RegisterProtocolFactory("blob", NULL); |
| 187 } |
| 188 |
| 189 void TestFinished() { |
| 190 // We unwind the stack prior to finishing up to let stack |
| 191 // based objects get deleted. |
| 192 DCHECK(MessageLoop::current() == io_thread_->message_loop()); |
| 193 MessageLoop::current()->PostTask(FROM_HERE, |
| 194 NewRunnableMethod( |
| 195 this, &BlobURLRequestJobTest::TestFinishedUnwound)); |
| 196 } |
| 197 |
| 198 void TestFinishedUnwound() { |
| 199 TearDownTest(); |
| 200 test_finished_event_->Signal(); |
| 201 } |
| 202 |
| 203 void PushNextTask(Task* task) { |
| 204 task_stack_.push(std::pair<Task*, bool>(task, false)); |
| 205 } |
| 206 |
| 207 void PushNextTaskAsImmediate(Task* task) { |
| 208 task_stack_.push(std::pair<Task*, bool>(task, true)); |
| 209 } |
| 210 |
| 211 void ScheduleNextTask() { |
| 212 DCHECK(MessageLoop::current() == io_thread_->message_loop()); |
| 213 if (task_stack_.empty()) { |
| 214 TestFinished(); |
| 215 return; |
| 216 } |
| 217 scoped_ptr<Task> task(task_stack_.top().first); |
| 218 bool immediate = task_stack_.top().second; |
| 219 task_stack_.pop(); |
| 220 if (immediate) |
| 221 task->Run(); |
| 222 else |
| 223 MessageLoop::current()->PostTask(FROM_HERE, task.release()); |
| 224 } |
| 225 |
| 226 void TestSuccessRequest(BlobData* blob_data, |
| 227 const std::string& expected_response) { |
| 228 PushNextTask(NewRunnableMethod( |
| 229 this, &BlobURLRequestJobTest::VerifyResponse)); |
| 230 expected_status_code_ = 200; |
| 231 expected_response_ = expected_response; |
| 232 return TestRequest("GET", net::HttpRequestHeaders(), blob_data); |
| 233 } |
| 234 |
| 235 void TestErrorRequest(BlobData* blob_data, |
| 236 int expected_status_code) { |
| 237 PushNextTask(NewRunnableMethod( |
| 238 this, &BlobURLRequestJobTest::VerifyResponse)); |
| 239 expected_status_code_ = expected_status_code; |
| 240 expected_response_ = ""; |
| 241 return TestRequest("GET", net::HttpRequestHeaders(), blob_data); |
| 242 } |
| 243 |
| 244 void TestRequest(const std::string& method, |
| 245 const net::HttpRequestHeaders& extra_headers, |
| 246 BlobData* blob_data) { |
| 247 // This test has async steps. |
| 248 request_.reset(new URLRequest(GURL("blob:blah"), |
| 249 url_request_delegate_.get())); |
| 250 request_->set_method(method); |
| 251 blob_url_request_job_ = new BlobURLRequestJob(request_.get(), |
| 252 blob_data, NULL); |
| 253 |
| 254 // Start the request. |
| 255 if (!extra_headers.IsEmpty()) |
| 256 request_->SetExtraRequestHeaders(extra_headers); |
| 257 request_->Start(); |
| 258 |
| 259 // Completion is async. |
| 260 } |
| 261 |
| 262 void VerifyResponse() { |
| 263 EXPECT_TRUE(request_->status().is_success()); |
| 264 EXPECT_EQ(request_->response_headers()->response_code(), |
| 265 expected_status_code_); |
| 266 EXPECT_STREQ(url_request_delegate_->response_data().c_str(), |
| 267 expected_response_.c_str()); |
| 268 TestFinished(); |
| 269 } |
| 270 |
| 271 // Test Cases --------------------------------------------------------------- |
| 272 |
| 273 void TestGetSimpleDataRequest() { |
| 274 scoped_refptr<BlobData> blob_data = new BlobData(); |
| 275 blob_data->AppendData(kTestData1); |
| 276 TestSuccessRequest(blob_data, kTestData1); |
| 277 } |
| 278 |
| 279 void TestGetSimpleFileRequest() { |
| 280 scoped_refptr<BlobData> blob_data = new BlobData(); |
| 281 blob_data->AppendFile(temp_file1_, 0, -1, base::Time()); |
| 282 TestSuccessRequest(blob_data, kTestFileData1); |
| 283 } |
| 284 |
| 285 void TestGetNonExistentFileRequest() { |
| 286 FilePath non_existent_file = |
| 287 temp_file1_.InsertBeforeExtension(FILE_PATH_LITERAL("-na")); |
| 288 scoped_refptr<BlobData> blob_data = new BlobData(); |
| 289 blob_data->AppendFile(non_existent_file, 0, -1, base::Time()); |
| 290 TestErrorRequest(blob_data, 404); |
| 291 } |
| 292 |
| 293 void TestGetChangedFileRequest() { |
| 294 scoped_refptr<BlobData> blob_data = new BlobData(); |
| 295 base::Time old_time = |
| 296 temp_file_modification_time1_ - base::TimeDelta::FromSeconds(10); |
| 297 blob_data->AppendFile(temp_file1_, 0, 3, old_time); |
| 298 TestErrorRequest(blob_data, 404); |
| 299 } |
| 300 |
| 301 void TestGetSlicedDataRequest() { |
| 302 scoped_refptr<BlobData> blob_data = new BlobData(); |
| 303 blob_data->AppendData(kTestData2, 2, 4); |
| 304 std::string result(kTestData2 + 2, 4); |
| 305 TestSuccessRequest(blob_data, result); |
| 306 } |
| 307 |
| 308 void TestGetSlicedFileRequest() { |
| 309 scoped_refptr<BlobData> blob_data = new BlobData(); |
| 310 blob_data->AppendFile(temp_file1_, 2, 4, temp_file_modification_time1_); |
| 311 std::string result(kTestFileData1 + 2, 4); |
| 312 TestSuccessRequest(blob_data, result); |
| 313 } |
| 314 |
| 315 scoped_refptr<BlobData> BuildComplicatedData(std::string* expected_result) { |
| 316 scoped_refptr<BlobData> blob_data = new BlobData(); |
| 317 blob_data->AppendData(kTestData1, 1, 2); |
| 318 blob_data->AppendFile(temp_file1_, 2, 3, temp_file_modification_time1_); |
| 319 blob_data->AppendData(kTestData2, 3, 4); |
| 320 blob_data->AppendFile(temp_file2_, 4, 5, temp_file_modification_time2_); |
| 321 *expected_result = std::string(kTestData1 + 1, 2); |
| 322 *expected_result += std::string(kTestFileData1 + 2, 3); |
| 323 *expected_result += std::string(kTestData2 + 3, 4); |
| 324 *expected_result += std::string(kTestFileData2 + 4, 5); |
| 325 return blob_data; |
| 326 } |
| 327 |
| 328 void TestGetComplicatedDataAndFileRequest() { |
| 329 std::string result; |
| 330 scoped_refptr<BlobData> blob_data = BuildComplicatedData(&result); |
| 331 TestSuccessRequest(blob_data, result); |
| 332 } |
| 333 |
| 334 void TestGetRangeRequest1() { |
| 335 std::string result; |
| 336 scoped_refptr<BlobData> blob_data = BuildComplicatedData(&result); |
| 337 net::HttpRequestHeaders extra_headers; |
| 338 extra_headers.SetHeader(net::HttpRequestHeaders::kRange, "bytes=5-10"); |
| 339 PushNextTask(NewRunnableMethod( |
| 340 this, &BlobURLRequestJobTest::VerifyResponse)); |
| 341 expected_status_code_ = 206; |
| 342 expected_response_ = result.substr(5, 10 - 5 + 1); |
| 343 return TestRequest("GET", extra_headers, blob_data); |
| 344 } |
| 345 |
| 346 void TestGetRangeRequest2() { |
| 347 std::string result; |
| 348 scoped_refptr<BlobData> blob_data = BuildComplicatedData(&result); |
| 349 net::HttpRequestHeaders extra_headers; |
| 350 extra_headers.SetHeader(net::HttpRequestHeaders::kRange, "bytes=-10"); |
| 351 PushNextTask(NewRunnableMethod( |
| 352 this, &BlobURLRequestJobTest::VerifyResponse)); |
| 353 expected_status_code_ = 206; |
| 354 expected_response_ = result.substr(result.length() - 10); |
| 355 return TestRequest("GET", extra_headers, blob_data); |
| 356 } |
| 357 |
| 358 void TestExtraHeaders() { |
| 359 scoped_refptr<BlobData> blob_data = new BlobData(); |
| 360 blob_data->set_content_type(kTestContentType); |
| 361 blob_data->set_content_disposition(kTestContentDisposition); |
| 362 blob_data->AppendData(kTestData1); |
| 363 PushNextTask(NewRunnableMethod( |
| 364 this, &BlobURLRequestJobTest::VerifyResponseForTestExtraHeaders)); |
| 365 TestRequest("GET", net::HttpRequestHeaders(), blob_data); |
| 366 } |
| 367 |
| 368 void VerifyResponseForTestExtraHeaders() { |
| 369 EXPECT_TRUE(request_->status().is_success()); |
| 370 EXPECT_EQ(request_->response_headers()->response_code(), 200); |
| 371 EXPECT_STREQ(url_request_delegate_->response_data().c_str(), kTestData1); |
| 372 std::string content_type; |
| 373 EXPECT_TRUE(request_->response_headers()->GetMimeType(&content_type)); |
| 374 EXPECT_STREQ(content_type.c_str(), kTestContentType); |
| 375 void* iter = NULL; |
| 376 std::string content_disposition; |
| 377 EXPECT_TRUE(request_->response_headers()->EnumerateHeader( |
| 378 &iter, "Content-Disposition", &content_disposition)); |
| 379 EXPECT_STREQ(content_disposition.c_str(), kTestContentDisposition); |
| 380 TestFinished(); |
| 381 } |
| 382 |
| 383 private: |
| 384 static scoped_ptr<ScopedTempDir> temp_dir_; |
| 385 static FilePath temp_file1_; |
| 386 static FilePath temp_file2_; |
| 387 static base::Time temp_file_modification_time1_; |
| 388 static base::Time temp_file_modification_time2_; |
| 389 static scoped_ptr<base::Thread> io_thread_; |
| 390 static BlobURLRequestJob* blob_url_request_job_; |
| 391 |
| 392 scoped_ptr<base::WaitableEvent> test_finished_event_; |
| 393 std::stack<std::pair<Task*, bool> > task_stack_; |
| 394 scoped_ptr<URLRequest> request_; |
| 395 scoped_ptr<MockURLRequestDelegate> url_request_delegate_; |
| 396 int expected_status_code_; |
| 397 std::string expected_response_; |
| 398 }; |
| 399 |
| 400 // static |
| 401 scoped_ptr<ScopedTempDir> BlobURLRequestJobTest::temp_dir_; |
| 402 FilePath BlobURLRequestJobTest::temp_file1_; |
| 403 FilePath BlobURLRequestJobTest::temp_file2_; |
| 404 base::Time BlobURLRequestJobTest::temp_file_modification_time1_; |
| 405 base::Time BlobURLRequestJobTest::temp_file_modification_time2_; |
| 406 scoped_ptr<base::Thread> BlobURLRequestJobTest::io_thread_; |
| 407 BlobURLRequestJob* BlobURLRequestJobTest::blob_url_request_job_ = NULL; |
| 408 |
| 409 TEST_F(BlobURLRequestJobTest, TestGetSimpleDataRequest) { |
| 410 RunTestOnIOThread(&BlobURLRequestJobTest::TestGetSimpleDataRequest); |
| 411 } |
| 412 |
| 413 TEST_F(BlobURLRequestJobTest, TestGetSimpleFileRequest) { |
| 414 RunTestOnIOThread(&BlobURLRequestJobTest::TestGetSimpleFileRequest); |
| 415 } |
| 416 |
| 417 TEST_F(BlobURLRequestJobTest, TestGetSlicedDataRequest) { |
| 418 RunTestOnIOThread(&BlobURLRequestJobTest::TestGetSlicedDataRequest); |
| 419 } |
| 420 |
| 421 TEST_F(BlobURLRequestJobTest, TestGetSlicedFileRequest) { |
| 422 RunTestOnIOThread(&BlobURLRequestJobTest::TestGetSlicedFileRequest); |
| 423 } |
| 424 |
| 425 TEST_F(BlobURLRequestJobTest, TestGetNonExistentFileRequest) { |
| 426 RunTestOnIOThread(&BlobURLRequestJobTest::TestGetNonExistentFileRequest); |
| 427 } |
| 428 |
| 429 TEST_F(BlobURLRequestJobTest, TestGetChangedFileRequest) { |
| 430 RunTestOnIOThread(&BlobURLRequestJobTest::TestGetChangedFileRequest); |
| 431 } |
| 432 |
| 433 TEST_F(BlobURLRequestJobTest, TestGetComplicatedDataAndFileRequest) { |
| 434 RunTestOnIOThread( |
| 435 &BlobURLRequestJobTest::TestGetComplicatedDataAndFileRequest); |
| 436 } |
| 437 |
| 438 TEST_F(BlobURLRequestJobTest, TestGetRangeRequest1) { |
| 439 RunTestOnIOThread(&BlobURLRequestJobTest::TestGetRangeRequest1); |
| 440 } |
| 441 |
| 442 TEST_F(BlobURLRequestJobTest, TestGetRangeRequest2) { |
| 443 RunTestOnIOThread(&BlobURLRequestJobTest::TestGetRangeRequest2); |
| 444 } |
| 445 |
| 446 TEST_F(BlobURLRequestJobTest, TestExtraHeaders) { |
| 447 RunTestOnIOThread(&BlobURLRequestJobTest::TestExtraHeaders); |
| 448 } |
| 449 |
| 450 } // namespace webkit_blob |
| 451 |
| 452 // BlobURLRequestJobTest is expected to always live longer than the |
| 453 // runnable methods. This lets us call NewRunnableMethod on its instances. |
| 454 DISABLE_RUNNABLE_METHOD_REFCOUNT(webkit_blob::BlobURLRequestJobTest); |
OLD | NEW |