Index: chrome/browser/policy/device_management_service_browsertest.cc |
diff --git a/chrome/browser/policy/device_management_service_browsertest.cc b/chrome/browser/policy/device_management_service_browsertest.cc |
index 1782319822f609b9ee2d4aa823346dbb79f65ccb..f927c291250a887809cc97324fdf2b7c8ba02353 100644 |
--- a/chrome/browser/policy/device_management_service_browsertest.cc |
+++ b/chrome/browser/policy/device_management_service_browsertest.cc |
@@ -2,25 +2,23 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
+#include "base/bind.h" |
+#include "base/bind_helpers.h" |
#include "base/memory/scoped_ptr.h" |
#include "base/message_loop.h" |
#include "base/stl_util.h" |
#include "chrome/browser/policy/cloud_policy_constants.h" |
#include "chrome/browser/policy/device_management_service.h" |
+#include "chrome/browser/policy/test_request_interceptor.h" |
#include "chrome/test/base/in_process_browser_test.h" |
-#include "content/public/browser/browser_thread.h" |
#include "net/base/upload_bytes_element_reader.h" |
#include "net/base/upload_data_stream.h" |
#include "net/test/test_server.h" |
-#include "net/url_request/url_fetcher.h" |
#include "net/url_request/url_request.h" |
-#include "net/url_request/url_request_filter.h" |
-#include "net/url_request/url_request_job_factory.h" |
#include "net/url_request/url_request_test_job.h" |
#include "testing/gmock/include/gmock/gmock.h" |
#include "testing/gtest/include/gtest/gtest.h" |
-using content::BrowserThread; |
using testing::DoAll; |
using testing::Invoke; |
using testing::InvokeWithoutArgs; |
@@ -30,97 +28,57 @@ namespace em = enterprise_management; |
namespace policy { |
-// Dummy service URL for testing with request interception enabled. |
-const char kServiceUrl[] = "http://example.com/device_management"; |
- |
-// During construction and destruction of CannedResponseInterceptor tasks are |
-// posted to the IO thread to add and remove an interceptor for URLRequest's of |
-// |service_url|. The interceptor returns test data back to the service. |
-class CannedResponseInterceptor { |
- public: |
- explicit CannedResponseInterceptor(const GURL& service_url) |
- : delegate_(new Delegate(service_url)) { |
- BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
- base::Bind(&Delegate::Register, |
- base::Unretained(delegate_))); |
+namespace { |
+ |
+// Parses the DeviceManagementRequest in |request_data| and writes a serialized |
+// DeviceManagementResponse to |response_data|. |
+void ConstructResponse(const char* request_data, |
+ uint64 request_data_length, |
+ std::string* response_data) { |
+ em::DeviceManagementRequest request; |
+ ASSERT_TRUE(request.ParseFromArray(request_data, request_data_length)); |
+ em::DeviceManagementResponse response; |
+ if (request.has_register_request()) { |
+ response.mutable_register_response()->set_device_management_token( |
+ "fake_token"); |
+ } else if (request.has_unregister_request()) { |
+ response.mutable_unregister_response(); |
+ } else if (request.has_policy_request()) { |
+ response.mutable_policy_response()->add_response(); |
+ } else if (request.has_auto_enrollment_request()) { |
+ response.mutable_auto_enrollment_response(); |
+ } else { |
+ FAIL() << "Failed to parse request."; |
} |
+ ASSERT_TRUE(response.SerializeToString(response_data)); |
+} |
- virtual ~CannedResponseInterceptor() { |
- BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
- base::Bind(&Delegate::Unregister)); |
+// JobCallback for the interceptor. |
+net::URLRequestJob* ResponseJob( |
+ net::URLRequest* request, |
+ net::NetworkDelegate* network_delegate) { |
+ const net::UploadDataStream* upload = request->get_upload(); |
+ if (upload != NULL && |
+ upload->element_readers().size() == 1 && |
+ upload->element_readers()[0]->AsBytesReader()) { |
+ std::string response_data; |
+ const net::UploadBytesElementReader* bytes_reader = |
+ upload->element_readers()[0]->AsBytesReader(); |
+ ConstructResponse(bytes_reader->bytes(), |
+ bytes_reader->length(), |
+ &response_data); |
+ return new net::URLRequestTestJob( |
+ request, |
+ network_delegate, |
+ net::URLRequestTestJob::test_headers(), |
+ response_data, |
+ true); |
} |
- private: |
- class Delegate : public net::URLRequestJobFactory::ProtocolHandler { |
- public: |
- explicit Delegate(const GURL& service_url) : service_url_(service_url) {} |
- virtual ~Delegate() {} |
- |
- void Register() { |
- net::URLRequestFilter::GetInstance()->AddHostnameProtocolHandler( |
- "http", "example.com", |
- scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(this)); |
- } |
- |
- static void Unregister() { |
- net::URLRequestFilter::GetInstance()->RemoveHostnameHandler( |
- "http", "example.com"); |
- } |
- |
- // net::URLRequestJobFactory::ProtocolHandler overrides. |
- virtual net::URLRequestJob* MaybeCreateJob( |
- net::URLRequest* request, |
- net::NetworkDelegate* network_delegate) const OVERRIDE { |
- const net::UploadDataStream* upload = request->get_upload(); |
- if (request->url().GetOrigin() == service_url_.GetOrigin() && |
- request->url().path() == service_url_.path() && |
- upload != NULL && |
- upload->element_readers().size() == 1 && |
- upload->element_readers()[0]->AsBytesReader()) { |
- std::string response_data; |
- const net::UploadBytesElementReader* bytes_reader = |
- upload->element_readers()[0]->AsBytesReader(); |
- ConstructResponse(bytes_reader->bytes(), |
- bytes_reader->length(), |
- &response_data); |
- return new net::URLRequestTestJob( |
- request, |
- network_delegate, |
- net::URLRequestTestJob::test_headers(), |
- response_data, |
- true); |
- } |
- |
- return NULL; |
- } |
- |
- private: |
- void ConstructResponse(const char* request_data, |
- uint64 request_data_length, |
- std::string* response_data) const { |
- em::DeviceManagementRequest request; |
- ASSERT_TRUE(request.ParseFromArray(request_data, request_data_length)); |
- em::DeviceManagementResponse response; |
- if (request.has_register_request()) { |
- response.mutable_register_response()->set_device_management_token( |
- "fake_token"); |
- } else if (request.has_unregister_request()) { |
- response.mutable_unregister_response(); |
- } else if (request.has_policy_request()) { |
- response.mutable_policy_response()->add_response(); |
- } else if (request.has_auto_enrollment_request()) { |
- response.mutable_auto_enrollment_response(); |
- } else { |
- FAIL() << "Failed to parse request."; |
- } |
- ASSERT_TRUE(response.SerializeToString(response_data)); |
- } |
- |
- const GURL service_url_; |
- }; |
- |
- Delegate* delegate_; |
-}; |
+ return NULL; |
+} |
+ |
+} // namespace |
class DeviceManagementServiceIntegrationTest |
: public InProcessBrowserTest, |
@@ -131,9 +89,8 @@ class DeviceManagementServiceIntegrationTest |
const em::DeviceManagementResponse&)); |
std::string InitCannedResponse() { |
- net::URLFetcher::SetEnableInterceptionForTests(true); |
- interceptor_.reset(new CannedResponseInterceptor(GURL(kServiceUrl))); |
- return kServiceUrl; |
+ interceptor_.reset(new TestRequestInterceptor("localhost")); |
+ return "http://localhost"; |
} |
std::string InitTestServer() { |
@@ -142,7 +99,13 @@ class DeviceManagementServiceIntegrationTest |
} |
protected: |
+ void ExpectRequest() { |
+ if (interceptor_) |
+ interceptor_->PushJobCallback(base::Bind(&ResponseJob)); |
+ } |
+ |
void PerformRegistration() { |
+ ExpectRequest(); |
EXPECT_CALL(*this, OnJobDone(DM_STATUS_SUCCESS, _)) |
.WillOnce( |
DoAll(Invoke(this, |
@@ -189,7 +152,7 @@ class DeviceManagementServiceIntegrationTest |
std::string token_; |
scoped_ptr<DeviceManagementService> service_; |
scoped_ptr<net::TestServer> test_server_; |
- scoped_ptr<CannedResponseInterceptor> interceptor_; |
+ scoped_ptr<TestRequestInterceptor> interceptor_; |
}; |
IN_PROC_BROWSER_TEST_P(DeviceManagementServiceIntegrationTest, Registration) { |
@@ -200,6 +163,7 @@ IN_PROC_BROWSER_TEST_P(DeviceManagementServiceIntegrationTest, Registration) { |
IN_PROC_BROWSER_TEST_P(DeviceManagementServiceIntegrationTest, PolicyFetch) { |
PerformRegistration(); |
+ ExpectRequest(); |
EXPECT_CALL(*this, OnJobDone(DM_STATUS_SUCCESS, _)) |
.WillOnce(InvokeWithoutArgs(MessageLoop::current(), &MessageLoop::Quit)); |
scoped_ptr<DeviceManagementRequestJob> job( |
@@ -218,6 +182,7 @@ IN_PROC_BROWSER_TEST_P(DeviceManagementServiceIntegrationTest, PolicyFetch) { |
IN_PROC_BROWSER_TEST_P(DeviceManagementServiceIntegrationTest, Unregistration) { |
PerformRegistration(); |
+ ExpectRequest(); |
EXPECT_CALL(*this, OnJobDone(DM_STATUS_SUCCESS, _)) |
.WillOnce(InvokeWithoutArgs(MessageLoop::current(), &MessageLoop::Quit)); |
scoped_ptr<DeviceManagementRequestJob> job( |
@@ -231,6 +196,7 @@ IN_PROC_BROWSER_TEST_P(DeviceManagementServiceIntegrationTest, Unregistration) { |
} |
IN_PROC_BROWSER_TEST_P(DeviceManagementServiceIntegrationTest, AutoEnrollment) { |
+ ExpectRequest(); |
EXPECT_CALL(*this, OnJobDone(DM_STATUS_SUCCESS, _)) |
.WillOnce(InvokeWithoutArgs(MessageLoop::current(), &MessageLoop::Quit)); |
scoped_ptr<DeviceManagementRequestJob> job( |