| OLD | NEW |
| 1 // Copyright (c) 2013 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 "chrome/browser/policy/cloud/test_request_interceptor.h" | 5 #include "chrome/browser/policy/cloud/test_request_interceptor.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <queue> | 9 #include <queue> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 | 35 |
| 36 namespace { | 36 namespace { |
| 37 | 37 |
| 38 // Helper callback for jobs that should fail with a network |error|. | 38 // Helper callback for jobs that should fail with a network |error|. |
| 39 net::URLRequestJob* ErrorJobCallback(int error, | 39 net::URLRequestJob* ErrorJobCallback(int error, |
| 40 net::URLRequest* request, | 40 net::URLRequest* request, |
| 41 net::NetworkDelegate* network_delegate) { | 41 net::NetworkDelegate* network_delegate) { |
| 42 return new net::URLRequestErrorJob(request, network_delegate, error); | 42 return new net::URLRequestErrorJob(request, network_delegate, error); |
| 43 } | 43 } |
| 44 | 44 |
| 45 // Helper callback for jobs that should fail with the specified HTTP error. |
| 46 net::URLRequestJob* HttpErrorJobCallback( |
| 47 const std::string& error, |
| 48 net::URLRequest* request, |
| 49 net::NetworkDelegate* network_delegate) { |
| 50 std::string headers = |
| 51 "HTTP/1.1 " + error + "\nContent-type: application/protobuf\n\n"; |
| 52 return new net::URLRequestTestJob( |
| 53 request, network_delegate, headers, std::string(), true); |
| 54 } |
| 55 |
| 45 // Helper callback for jobs that should fail with a 400 HTTP error. | 56 // Helper callback for jobs that should fail with a 400 HTTP error. |
| 46 net::URLRequestJob* BadRequestJobCallback( | 57 net::URLRequestJob* BadRequestJobCallback( |
| 47 net::URLRequest* request, | 58 net::URLRequest* request, |
| 48 net::NetworkDelegate* network_delegate) { | 59 net::NetworkDelegate* network_delegate) { |
| 49 static const char kBadHeaders[] = | 60 return HttpErrorJobCallback("400 Bad request", request, network_delegate); |
| 50 "HTTP/1.1 400 Bad request\n" | |
| 51 "Content-type: application/protobuf\n" | |
| 52 "\n"; | |
| 53 std::string headers(kBadHeaders, arraysize(kBadHeaders)); | |
| 54 return new net::URLRequestTestJob( | |
| 55 request, network_delegate, headers, std::string(), true); | |
| 56 } | 61 } |
| 57 | 62 |
| 58 net::URLRequestJob* FileJobCallback(const base::FilePath& file_path, | 63 net::URLRequestJob* FileJobCallback(const base::FilePath& file_path, |
| 59 net::URLRequest* request, | 64 net::URLRequest* request, |
| 60 net::NetworkDelegate* network_delegate) { | 65 net::NetworkDelegate* network_delegate) { |
| 61 return new net::URLRequestMockHTTPJob( | 66 return new net::URLRequestMockHTTPJob( |
| 62 request, | 67 request, |
| 63 network_delegate, | 68 network_delegate, |
| 64 file_path, | 69 file_path, |
| 65 content::BrowserThread::GetBlockingPool() | 70 content::BrowserThread::GetBlockingPool() |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 int error) { | 315 int error) { |
| 311 return base::Bind(&ErrorJobCallback, error); | 316 return base::Bind(&ErrorJobCallback, error); |
| 312 } | 317 } |
| 313 | 318 |
| 314 // static | 319 // static |
| 315 TestRequestInterceptor::JobCallback TestRequestInterceptor::BadRequestJob() { | 320 TestRequestInterceptor::JobCallback TestRequestInterceptor::BadRequestJob() { |
| 316 return base::Bind(&BadRequestJobCallback); | 321 return base::Bind(&BadRequestJobCallback); |
| 317 } | 322 } |
| 318 | 323 |
| 319 // static | 324 // static |
| 325 TestRequestInterceptor::JobCallback TestRequestInterceptor::HttpErrorJob( |
| 326 std::string error) { |
| 327 return base::Bind(&HttpErrorJobCallback, error); |
| 328 } |
| 329 |
| 330 // static |
| 320 TestRequestInterceptor::JobCallback TestRequestInterceptor::RegisterJob( | 331 TestRequestInterceptor::JobCallback TestRequestInterceptor::RegisterJob( |
| 321 em::DeviceRegisterRequest::Type expected_type, | 332 em::DeviceRegisterRequest::Type expected_type, |
| 322 bool expect_reregister) { | 333 bool expect_reregister) { |
| 323 return base::Bind(&RegisterJobCallback, expected_type, expect_reregister); | 334 return base::Bind(&RegisterJobCallback, expected_type, expect_reregister); |
| 324 } | 335 } |
| 325 | 336 |
| 326 // static | 337 // static |
| 327 TestRequestInterceptor::JobCallback TestRequestInterceptor::FileJob( | 338 TestRequestInterceptor::JobCallback TestRequestInterceptor::FileJob( |
| 328 const base::FilePath& file_path) { | 339 const base::FilePath& file_path) { |
| 329 return base::Bind(&FileJobCallback, file_path); | 340 return base::Bind(&FileJobCallback, file_path); |
| 330 } | 341 } |
| 331 | 342 |
| 332 void TestRequestInterceptor::PostToIOAndWait(const base::Closure& task) { | 343 void TestRequestInterceptor::PostToIOAndWait(const base::Closure& task) { |
| 333 io_task_runner_->PostTask(FROM_HERE, task); | 344 io_task_runner_->PostTask(FROM_HERE, task); |
| 334 base::RunLoop run_loop; | 345 base::RunLoop run_loop; |
| 335 io_task_runner_->PostTask( | 346 io_task_runner_->PostTask( |
| 336 FROM_HERE, | 347 FROM_HERE, |
| 337 base::Bind( | 348 base::Bind( |
| 338 base::IgnoreResult(&base::TaskRunner::PostTask), | 349 base::IgnoreResult(&base::TaskRunner::PostTask), |
| 339 base::ThreadTaskRunnerHandle::Get(), | 350 base::ThreadTaskRunnerHandle::Get(), |
| 340 FROM_HERE, | 351 FROM_HERE, |
| 341 run_loop.QuitClosure())); | 352 run_loop.QuitClosure())); |
| 342 run_loop.Run(); | 353 run_loop.Run(); |
| 343 } | 354 } |
| 344 | 355 |
| 345 } // namespace policy | 356 } // namespace policy |
| OLD | NEW |