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.
5 #ifndef CHROME_BROWSER_POLICY_CLOUD_TEST_REQUEST_INTERCEPTOR_H_
6 #define CHROME_BROWSER_POLICY_CLOUD_TEST_REQUEST_INTERCEPTOR_H_
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"
17 class SequencedTaskRunner
;
21 class NetworkDelegate
;
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
{
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
;
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();
44 // Returns the number of pending callback jobs that haven't been used yet.
45 size_t GetPendingSize();
47 // |callback| will be invoked on the current loop once the next request
48 // intercepted by this class has been dispatched.
49 void AddRequestServicedCallback(const base::Closure
& callback
);
51 // Queues |callback| to handle a request to |hostname_|. Each callback is
52 // used only once, and in the order that they're pushed.
53 void PushJobCallback(const JobCallback
& callback
);
55 // Returns a JobCallback that will fail with the given network |error|.
56 static JobCallback
ErrorJob(int error
);
58 // Returns a JobCallback that will fail with HTTP 400 Bad Request.
59 static JobCallback
BadRequestJob();
61 // Returns a JobCallback that will process a policy register request that
62 // should succeed. The request parameters are validated, and an appropriate
63 // response is sent back.
64 // |expected_type| is the expected type in the register request.
65 // If |expect_reregister| is true then the request must have the reregister
66 // flag set; otherwise the flag must be not set.
67 static JobCallback
RegisterJob(
68 enterprise_management::DeviceRegisterRequest::Type expected_type
,
69 bool expect_reregister
);
71 // Returns a JobCallback that will send the contents of |file_path|.
72 static JobCallback
FileJob(const base::FilePath
& file_path
);
77 // Helper to execute a |task| on IO, and return only after it has completed.
78 void PostToIOAndWait(const base::Closure
& task
);
80 const std::string hostname_
;
82 scoped_refptr
<base::SequencedTaskRunner
> io_task_runner_
;
84 // Owned by URLRequestFilter. This handle is valid on IO and only while the
85 // interceptor is valid.
88 DISALLOW_COPY_AND_ASSIGN(TestRequestInterceptor
);
93 #endif // CHROME_BROWSER_POLICY_CLOUD_TEST_REQUEST_INTERCEPTOR_H_