OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "net/test/url_request/url_request_mock_data_job.h" | 5 #include "net/test/url_request/url_request_mock_data_job.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
13 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.h" |
14 #include "net/base/url_util.h" | 14 #include "net/base/url_util.h" |
15 #include "net/http/http_request_headers.h" | 15 #include "net/http/http_request_headers.h" |
16 #include "net/http/http_response_headers.h" | 16 #include "net/http/http_response_headers.h" |
17 #include "net/http/http_util.h" | 17 #include "net/http/http_util.h" |
| 18 #include "net/ssl/ssl_cert_request_info.h" |
18 #include "net/url_request/url_request_filter.h" | 19 #include "net/url_request/url_request_filter.h" |
19 | 20 |
20 namespace net { | 21 namespace net { |
21 namespace { | 22 namespace { |
22 | 23 |
23 const char kMockHostname[] = "mock.data"; | 24 const char kMockHostname[] = "mock.data"; |
24 | 25 |
25 // Gets the data from URL of the form: | 26 // Gets the data from URL of the form: |
26 // scheme://kMockHostname/?data=abc&repeat_count=nnn. | 27 // scheme://kMockHostname/?data=abc&repeat_count=nnn. |
27 std::string GetDataFromRequest(const URLRequest& request) { | 28 std::string GetDataFromRequest(const URLRequest& request) { |
(...skipping 12 matching lines...) Expand all Loading... |
40 | 41 |
41 int repeat_count; | 42 int repeat_count; |
42 if (!base::StringToInt(value, &repeat_count)) | 43 if (!base::StringToInt(value, &repeat_count)) |
43 return 1; | 44 return 1; |
44 | 45 |
45 DCHECK_GT(repeat_count, 0); | 46 DCHECK_GT(repeat_count, 0); |
46 | 47 |
47 return repeat_count; | 48 return repeat_count; |
48 } | 49 } |
49 | 50 |
| 51 // Gets the requestcert flag from URL. |
| 52 bool GetRequestClientCertificate(const URLRequest& request) { |
| 53 std::string ignored_value; |
| 54 return GetValueForKeyInQuery(request.url(), "requestcert", &ignored_value); |
| 55 } |
| 56 |
50 GURL GetMockUrl(const std::string& scheme, | 57 GURL GetMockUrl(const std::string& scheme, |
51 const std::string& hostname, | 58 const std::string& hostname, |
52 const std::string& data, | 59 const std::string& data, |
53 int data_repeat_count) { | 60 int data_repeat_count, |
| 61 bool request_client_certificate) { |
54 DCHECK_GT(data_repeat_count, 0); | 62 DCHECK_GT(data_repeat_count, 0); |
55 std::string url(scheme + "://" + hostname + "/"); | 63 std::string url(scheme + "://" + hostname + "/"); |
56 url.append("?data="); | 64 url.append("?data="); |
57 url.append(data); | 65 url.append(data); |
58 url.append("&repeat="); | 66 url.append("&repeat="); |
59 url.append(base::IntToString(data_repeat_count)); | 67 url.append(base::IntToString(data_repeat_count)); |
| 68 if (request_client_certificate) |
| 69 url += "&requestcert=1"; |
60 return GURL(url); | 70 return GURL(url); |
61 } | 71 } |
62 | 72 |
63 class MockJobInterceptor : public URLRequestInterceptor { | 73 class MockJobInterceptor : public URLRequestInterceptor { |
64 public: | 74 public: |
65 MockJobInterceptor() {} | 75 MockJobInterceptor() {} |
66 ~MockJobInterceptor() override {} | 76 ~MockJobInterceptor() override {} |
67 | 77 |
68 // URLRequestInterceptor implementation | 78 // URLRequestInterceptor implementation |
69 URLRequestJob* MaybeInterceptRequest( | 79 URLRequestJob* MaybeInterceptRequest( |
70 URLRequest* request, | 80 URLRequest* request, |
71 NetworkDelegate* network_delegate) const override { | 81 NetworkDelegate* network_delegate) const override { |
72 return new URLRequestMockDataJob(request, network_delegate, | 82 return new URLRequestMockDataJob(request, network_delegate, |
73 GetDataFromRequest(*request), | 83 GetDataFromRequest(*request), |
74 GetRepeatCountFromRequest(*request)); | 84 GetRepeatCountFromRequest(*request), |
| 85 GetRequestClientCertificate(*request)); |
75 } | 86 } |
76 | 87 |
77 private: | 88 private: |
78 DISALLOW_COPY_AND_ASSIGN(MockJobInterceptor); | 89 DISALLOW_COPY_AND_ASSIGN(MockJobInterceptor); |
79 }; | 90 }; |
80 | 91 |
81 } // namespace | 92 } // namespace |
82 | 93 |
83 URLRequestMockDataJob::URLRequestMockDataJob(URLRequest* request, | 94 URLRequestMockDataJob::URLRequestMockDataJob(URLRequest* request, |
84 NetworkDelegate* network_delegate, | 95 NetworkDelegate* network_delegate, |
85 const std::string& data, | 96 const std::string& data, |
86 int data_repeat_count) | 97 int data_repeat_count, |
| 98 bool request_client_certificate) |
87 : URLRequestJob(request, network_delegate), | 99 : URLRequestJob(request, network_delegate), |
88 data_offset_(0), | 100 data_offset_(0), |
| 101 request_client_certificate_(request_client_certificate), |
89 weak_factory_(this) { | 102 weak_factory_(this) { |
90 DCHECK_GT(data_repeat_count, 0); | 103 DCHECK_GT(data_repeat_count, 0); |
91 for (int i = 0; i < data_repeat_count; ++i) { | 104 for (int i = 0; i < data_repeat_count; ++i) { |
92 data_.append(data); | 105 data_.append(data); |
93 } | 106 } |
94 } | 107 } |
95 | 108 |
96 void URLRequestMockDataJob::Start() { | 109 void URLRequestMockDataJob::Start() { |
97 // Start reading asynchronously so that all error reporting and data | 110 // Start reading asynchronously so that all error reporting and data |
98 // callbacks happen as they would for network requests. | 111 // callbacks happen as they would for network requests. |
(...skipping 12 matching lines...) Expand all Loading... |
111 data_offset_ += bytes_read; | 124 data_offset_ += bytes_read; |
112 return bytes_read; | 125 return bytes_read; |
113 } | 126 } |
114 | 127 |
115 int URLRequestMockDataJob::GetResponseCode() const { | 128 int URLRequestMockDataJob::GetResponseCode() const { |
116 HttpResponseInfo info; | 129 HttpResponseInfo info; |
117 GetResponseInfoConst(&info); | 130 GetResponseInfoConst(&info); |
118 return info.headers->response_code(); | 131 return info.headers->response_code(); |
119 } | 132 } |
120 | 133 |
| 134 void URLRequestMockDataJob::ContinueWithCertificate( |
| 135 X509Certificate* client_cert, |
| 136 SSLPrivateKey* client_private_key) { |
| 137 DCHECK(request_client_certificate_); |
| 138 NotifyHeadersComplete(); |
| 139 } |
| 140 |
121 // Public virtual version. | 141 // Public virtual version. |
122 void URLRequestMockDataJob::GetResponseInfo(HttpResponseInfo* info) { | 142 void URLRequestMockDataJob::GetResponseInfo(HttpResponseInfo* info) { |
123 // Forward to private const version. | 143 // Forward to private const version. |
124 GetResponseInfoConst(info); | 144 GetResponseInfoConst(info); |
125 } | 145 } |
126 | 146 |
127 // Private const version. | 147 // Private const version. |
128 void URLRequestMockDataJob::GetResponseInfoConst(HttpResponseInfo* info) const { | 148 void URLRequestMockDataJob::GetResponseInfoConst(HttpResponseInfo* info) const { |
129 // Send back mock headers. | 149 // Send back mock headers. |
130 std::string raw_headers; | 150 std::string raw_headers; |
131 raw_headers.append( | 151 raw_headers.append( |
132 "HTTP/1.1 200 OK\n" | 152 "HTTP/1.1 200 OK\n" |
133 "Content-type: text/plain\n"); | 153 "Content-type: text/plain\n"); |
134 raw_headers.append(base::StringPrintf("Content-Length: %1d\n", | 154 raw_headers.append(base::StringPrintf("Content-Length: %1d\n", |
135 static_cast<int>(data_.length()))); | 155 static_cast<int>(data_.length()))); |
136 info->headers = new HttpResponseHeaders(HttpUtil::AssembleRawHeaders( | 156 info->headers = new HttpResponseHeaders(HttpUtil::AssembleRawHeaders( |
137 raw_headers.c_str(), static_cast<int>(raw_headers.length()))); | 157 raw_headers.c_str(), static_cast<int>(raw_headers.length()))); |
138 } | 158 } |
139 | 159 |
140 void URLRequestMockDataJob::StartAsync() { | 160 void URLRequestMockDataJob::StartAsync() { |
141 if (!request_) | 161 if (!request_) |
142 return; | 162 return; |
143 | 163 |
144 set_expected_content_size(data_.length()); | 164 set_expected_content_size(data_.length()); |
| 165 if (request_client_certificate_) { |
| 166 scoped_refptr<SSLCertRequestInfo> request_all(new SSLCertRequestInfo()); |
| 167 NotifyCertificateRequested(request_all.get()); |
| 168 return; |
| 169 } |
145 NotifyHeadersComplete(); | 170 NotifyHeadersComplete(); |
146 } | 171 } |
147 | 172 |
148 // static | 173 // static |
149 void URLRequestMockDataJob::AddUrlHandler() { | 174 void URLRequestMockDataJob::AddUrlHandler() { |
150 return AddUrlHandlerForHostname(kMockHostname); | 175 return AddUrlHandlerForHostname(kMockHostname); |
151 } | 176 } |
152 | 177 |
153 // static | 178 // static |
154 void URLRequestMockDataJob::AddUrlHandlerForHostname( | 179 void URLRequestMockDataJob::AddUrlHandlerForHostname( |
(...skipping 11 matching lines...) Expand all Loading... |
166 int repeat_count) { | 191 int repeat_count) { |
167 return GetMockHttpUrlForHostname(kMockHostname, data, repeat_count); | 192 return GetMockHttpUrlForHostname(kMockHostname, data, repeat_count); |
168 } | 193 } |
169 | 194 |
170 // static | 195 // static |
171 GURL URLRequestMockDataJob::GetMockHttpsUrl(const std::string& data, | 196 GURL URLRequestMockDataJob::GetMockHttpsUrl(const std::string& data, |
172 int repeat_count) { | 197 int repeat_count) { |
173 return GetMockHttpsUrlForHostname(kMockHostname, data, repeat_count); | 198 return GetMockHttpsUrlForHostname(kMockHostname, data, repeat_count); |
174 } | 199 } |
175 | 200 |
| 201 GURL URLRequestMockDataJob::GetMockUrlForClientCertificateRequest() { |
| 202 return GetMockUrl("https", kMockHostname, "data", 1, true); |
| 203 } |
| 204 |
176 // static | 205 // static |
177 GURL URLRequestMockDataJob::GetMockHttpUrlForHostname( | 206 GURL URLRequestMockDataJob::GetMockHttpUrlForHostname( |
178 const std::string& hostname, | 207 const std::string& hostname, |
179 const std::string& data, | 208 const std::string& data, |
180 int repeat_count) { | 209 int repeat_count) { |
181 return GetMockUrl("http", hostname, data, repeat_count); | 210 return GetMockUrl("http", hostname, data, repeat_count, false); |
182 } | 211 } |
183 | 212 |
184 // static | 213 // static |
185 GURL URLRequestMockDataJob::GetMockHttpsUrlForHostname( | 214 GURL URLRequestMockDataJob::GetMockHttpsUrlForHostname( |
186 const std::string& hostname, | 215 const std::string& hostname, |
187 const std::string& data, | 216 const std::string& data, |
188 int repeat_count) { | 217 int repeat_count) { |
189 return GetMockUrl("https", hostname, data, repeat_count); | 218 return GetMockUrl("https", hostname, data, repeat_count, false); |
190 } | 219 } |
191 | 220 |
192 } // namespace net | 221 } // namespace net |
OLD | NEW |