Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1027)

Side by Side Diff: chrome/browser/policy/device_management_service_browsertest.cc

Issue 11293252: Change Interceptors into URLRequestJobFactory::ProtocolHandlers. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sync Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/memory/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
6 #include "base/message_loop.h" 6 #include "base/message_loop.h"
7 #include "base/stl_util.h" 7 #include "base/stl_util.h"
8 #include "chrome/browser/policy/cloud_policy_constants.h" 8 #include "chrome/browser/policy/cloud_policy_constants.h"
9 #include "chrome/browser/policy/device_management_service.h" 9 #include "chrome/browser/policy/device_management_service.h"
10 #include "chrome/test/base/in_process_browser_test.h" 10 #include "chrome/test/base/in_process_browser_test.h"
11 #include "content/public/browser/browser_thread.h"
11 #include "net/base/upload_bytes_element_reader.h" 12 #include "net/base/upload_bytes_element_reader.h"
12 #include "net/base/upload_data_stream.h" 13 #include "net/base/upload_data_stream.h"
13 #include "net/test/test_server.h" 14 #include "net/test/test_server.h"
14 #include "net/url_request/url_fetcher.h" 15 #include "net/url_request/url_fetcher.h"
15 #include "net/url_request/url_request.h" 16 #include "net/url_request/url_request.h"
17 #include "net/url_request/url_request_filter.h"
18 #include "net/url_request/url_request_job_factory.h"
16 #include "net/url_request/url_request_test_job.h" 19 #include "net/url_request/url_request_test_job.h"
17 #include "testing/gmock/include/gmock/gmock.h" 20 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
19 22
23 using content::BrowserThread;
20 using testing::DoAll; 24 using testing::DoAll;
21 using testing::Invoke; 25 using testing::Invoke;
22 using testing::InvokeWithoutArgs; 26 using testing::InvokeWithoutArgs;
23 using testing::_; 27 using testing::_;
24 28
25 namespace em = enterprise_management; 29 namespace em = enterprise_management;
26 30
27 namespace policy { 31 namespace policy {
28 32
29 // Dummy service URL for testing with request interception enabled. 33 // Dummy service URL for testing with request interception enabled.
30 const char kServiceUrl[] = "http://example.com/device_management"; 34 const char kServiceUrl[] = "http://example.com/device_management";
31 35
32 // Interceptor implementation that returns test data back to the service. 36 // During construction and destruction of CannedResponseInterceptor tasks are
33 class CannedResponseInterceptor : public net::URLRequest::Interceptor { 37 // posted to the IO thread to add and remove an interceptor for URLRequest's of
38 // |service_url|. The interceptor returns test data back to the service.
39 class CannedResponseInterceptor {
34 public: 40 public:
35 explicit CannedResponseInterceptor(const GURL& service_url) 41 explicit CannedResponseInterceptor(const GURL& service_url)
36 : service_url_(service_url) { 42 : delegate_(new Delegate(service_url)) {
37 net::URLRequest::Deprecated::RegisterRequestInterceptor(this); 43 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
44 base::Bind(&Delegate::Register,
45 base::Unretained(delegate_)));
38 } 46 }
39 47
40 virtual ~CannedResponseInterceptor() { 48 virtual ~CannedResponseInterceptor() {
41 net::URLRequest::Deprecated::UnregisterRequestInterceptor(this); 49 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, delegate_);
42 }
43
44 // net::URLRequest::Interceptor overrides.
45 virtual net::URLRequestJob* MaybeIntercept(
46 net::URLRequest* request,
47 net::NetworkDelegate* network_delegate) OVERRIDE {
48 em::DeviceManagementRequest dm_request;
49 const net::UploadDataStream* upload = request->get_upload();
50 if (request->url().GetOrigin() == service_url_.GetOrigin() &&
51 request->url().path() == service_url_.path() &&
52 upload != NULL &&
53 upload->element_readers().size() == 1 &&
54 upload->element_readers()[0]->AsBytesReader()) {
55 std::string response_data;
56 const net::UploadBytesElementReader* bytes_reader =
57 upload->element_readers()[0]->AsBytesReader();
58 ConstructResponse(bytes_reader->bytes(),
59 bytes_reader->length(),
60 &response_data);
61 return new net::URLRequestTestJob(request,
62 network_delegate,
63 net::URLRequestTestJob::test_headers(),
64 response_data,
65 true);
66 }
67
68 return NULL;
69 } 50 }
70 51
71 private: 52 private:
72 void ConstructResponse(const char* request_data, 53 class Delegate : public net::URLRequestJobFactory::ProtocolHandler {
73 uint64 request_data_length, 54 public:
74 std::string* response_data) { 55 explicit Delegate(const GURL& service_url) : service_url_(service_url) {}
75 em::DeviceManagementRequest request; 56 ~Delegate() {
76 ASSERT_TRUE(request.ParseFromArray(request_data, request_data_length)); 57 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(
77 em::DeviceManagementResponse response; 58 "http", "example.com");
78 if (request.has_register_request()) {
79 response.mutable_register_response()->set_device_management_token(
80 "fake_token");
81 } else if (request.has_unregister_request()) {
82 response.mutable_unregister_response();
83 } else if (request.has_policy_request()) {
84 response.mutable_policy_response()->add_response();
85 } else if (request.has_auto_enrollment_request()) {
86 response.mutable_auto_enrollment_response();
87 } else {
88 FAIL() << "Failed to parse request.";
89 } 59 }
90 ASSERT_TRUE(response.SerializeToString(response_data));
91 }
92 60
93 const GURL service_url_; 61 void Register() {
62 net::URLRequestFilter::GetInstance()->AddHostnameProtocolHandler(
63 "http", "example.com", this);
64 }
65
66 // net::URLRequestJobFactory::ProtocolHandler overrides.
67 virtual net::URLRequestJob* MaybeCreateJob(
68 net::URLRequest* request,
69 net::NetworkDelegate* network_delegate) const OVERRIDE {
70 const net::UploadDataStream* upload = request->get_upload();
71 if (request->url().GetOrigin() == service_url_.GetOrigin() &&
72 request->url().path() == service_url_.path() &&
73 upload != NULL &&
74 upload->element_readers().size() == 1 &&
75 upload->element_readers()[0]->AsBytesReader()) {
76 std::string response_data;
77 const net::UploadBytesElementReader* bytes_reader =
78 upload->element_readers()[0]->AsBytesReader();
79 ConstructResponse(bytes_reader->bytes(),
80 bytes_reader->length(),
81 &response_data);
82 return new net::URLRequestTestJob(
83 request,
84 network_delegate,
85 net::URLRequestTestJob::test_headers(),
86 response_data,
87 true);
88 }
89
90 return NULL;
91 }
92
93 private:
94 void ConstructResponse(const char* request_data,
95 uint64 request_data_length,
96 std::string* response_data) const {
97 em::DeviceManagementRequest request;
98 ASSERT_TRUE(request.ParseFromArray(request_data, request_data_length));
99 em::DeviceManagementResponse response;
100 if (request.has_register_request()) {
101 response.mutable_register_response()->set_device_management_token(
102 "fake_token");
103 } else if (request.has_unregister_request()) {
104 response.mutable_unregister_response();
105 } else if (request.has_policy_request()) {
106 response.mutable_policy_response()->add_response();
107 } else if (request.has_auto_enrollment_request()) {
108 response.mutable_auto_enrollment_response();
109 } else {
110 FAIL() << "Failed to parse request.";
111 }
112 ASSERT_TRUE(response.SerializeToString(response_data));
113 }
114
115 const GURL service_url_;
116 };
117
118 Delegate* delegate_;
94 }; 119 };
95 120
96 class DeviceManagementServiceIntegrationTest 121 class DeviceManagementServiceIntegrationTest
97 : public InProcessBrowserTest, 122 : public InProcessBrowserTest,
98 public testing::WithParamInterface< 123 public testing::WithParamInterface<
99 std::string (DeviceManagementServiceIntegrationTest::*)(void)> { 124 std::string (DeviceManagementServiceIntegrationTest::*)(void)> {
100 public: 125 public:
101 MOCK_METHOD2(OnJobDone, void(DeviceManagementStatus, 126 MOCK_METHOD2(OnJobDone, void(DeviceManagementStatus,
102 const em::DeviceManagementResponse&)); 127 const em::DeviceManagementResponse&));
103 128
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 MessageLoop::current()->Run(); 239 MessageLoop::current()->Run();
215 } 240 }
216 241
217 INSTANTIATE_TEST_CASE_P( 242 INSTANTIATE_TEST_CASE_P(
218 DeviceManagementServiceIntegrationTestInstance, 243 DeviceManagementServiceIntegrationTestInstance,
219 DeviceManagementServiceIntegrationTest, 244 DeviceManagementServiceIntegrationTest,
220 testing::Values(&DeviceManagementServiceIntegrationTest::InitCannedResponse, 245 testing::Values(&DeviceManagementServiceIntegrationTest::InitCannedResponse,
221 &DeviceManagementServiceIntegrationTest::InitTestServer)); 246 &DeviceManagementServiceIntegrationTest::InitTestServer));
222 247
223 } // namespace policy 248 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/net/http_intercept_job_factory.cc ('k') | chrome/browser/policy/policy_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698