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 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_POLICY_CLOUD_TEST_REQUEST_INTERCEPTOR_H_ 6 #define CHROME_BROWSER_POLICY_CLOUD_TEST_REQUEST_INTERCEPTOR_H_ 7 8 #include <string> 9 10 #include "base/basictypes.h" 11 #include "base/callback.h" 12 #include "base/memory/ref_counted.h" 13 #include "policy/proto/device_management_backend.pb.h" 14 15 namespace base { 16 class FilePath; 17 class SequencedTaskRunner; 18 } 19 20 namespace net { 21 class NetworkDelegate; 22 class URLRequest; 23 class URLRequestJob; 24 } 25 26 namespace policy { 27 28 // Intercepts all requests to the given hostname while in scope, and allows 29 // queuing callbacks to handle expected requests. Must be created and destroyed 30 // while the IO thread is valid. 31 class TestRequestInterceptor { 32 public: 33 // A callback that returns a new URLRequestJob given a URLRequest. 34 // This is used to queue callbacks that will handle expected requests. 35 typedef base::Callback< 36 net::URLRequestJob*(net::URLRequest*, net::NetworkDelegate*)> JobCallback; 37 38 // Will intercept request to |hostname| made over HTTP. 39 TestRequestInterceptor( 40 const std::string& hostname, 41 scoped_refptr<base::SequencedTaskRunner> io_task_runner); 42 virtual ~TestRequestInterceptor(); 43 44 // Returns the number of pending callback jobs that haven't been used yet. 45 size_t GetPendingSize(); 46 47 // Queues |callback| to handle a request to |hostname_|. Each callback is 48 // used only once, and in the order that they're pushed. 49 void PushJobCallback(const JobCallback& callback); 50 51 // Returns a JobCallback that will fail with the given network |error|. 52 static JobCallback ErrorJob(int error); 53 54 // Returns a JobCallback that will fail with HTTP 400 Bad Request. 55 static JobCallback BadRequestJob(); 56 57 // Returns a JobCallback that will process a policy register request that 58 // should succeed. The request parameters are validated, and an appropriate 59 // response is sent back. 60 // |expected_type| is the expected type in the register request. 61 // If |expect_reregister| is true then the request must have the reregister 62 // flag set; otherwise the flag must be not set. 63 static JobCallback RegisterJob( 64 enterprise_management::DeviceRegisterRequest::Type expected_type, 65 bool expect_reregister); 66 67 // Returns a JobCallback that will send the contents of |file_path|. 68 static JobCallback FileJob(const base::FilePath& file_path); 69 70 private: 71 class Delegate; 72 73 // Helper to execute a |task| on IO, and return only after it has completed. 74 void PostToIOAndWait(const base::Closure& task); 75 76 const std::string hostname_; 77 78 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; 79 80 // Owned by URLRequestFilter. This handle is valid on IO and only while the 81 // interceptor is valid. 82 Delegate* delegate_; 83 84 DISALLOW_COPY_AND_ASSIGN(TestRequestInterceptor); 85 }; 86 87 } // namespace policy 88 89 #endif // CHROME_BROWSER_POLICY_CLOUD_TEST_REQUEST_INTERCEPTOR_H_ 90