OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <string> | 5 #include <string> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "chrome/browser/chrome_plugin_service_filter.h" | 10 #include "chrome/browser/chrome_plugin_service_filter.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "net/url_request/url_request_job_factory.h" | 24 #include "net/url_request/url_request_job_factory.h" |
25 #include "net/url_request/url_request_test_job.h" | 25 #include "net/url_request/url_request_test_job.h" |
26 #include "net/url_request/url_request_test_util.h" | 26 #include "net/url_request/url_request_test_util.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
28 #include "webkit/plugins/npapi/plugin_list.h" | 28 #include "webkit/plugins/npapi/plugin_list.h" |
29 | 29 |
30 namespace chromeos { | 30 namespace chromeos { |
31 | 31 |
32 namespace { | 32 namespace { |
33 | 33 |
| 34 const char kPdfUrl[] = "http://foo.com/file.pdf"; |
| 35 const char kPptUrl[] = "http://foo.com/file.ppt"; |
| 36 const char kHtmlUrl[] = "http://foo.com/index.html"; |
| 37 const char kPdfBlob[] = "blob:blobinternal:///d17c4eef-28e7-42bd-bafa-78d5cb8"; |
| 38 |
| 39 const char kPdfUrlIntercepted[] = |
| 40 "http://docs.google.com/gview?url=http%3A//foo.com/file.pdf"; |
| 41 const char kPptUrlIntercepted[] = |
| 42 "http://docs.google.com/gview?url=http%3A//foo.com/file.ppt"; |
| 43 |
34 class GViewURLRequestTestJob : public net::URLRequestTestJob { | 44 class GViewURLRequestTestJob : public net::URLRequestTestJob { |
35 public: | 45 public: |
36 explicit GViewURLRequestTestJob(net::URLRequest* request) | 46 explicit GViewURLRequestTestJob(net::URLRequest* request) |
37 : net::URLRequestTestJob(request, true) { | 47 : net::URLRequestTestJob(request, true) { |
38 } | 48 } |
39 | 49 |
40 virtual bool GetMimeType(std::string* mime_type) const { | 50 virtual bool GetMimeType(std::string* mime_type) const { |
41 // During the course of a single test, two URLRequestJobs are | 51 // During the course of a single test, two URLRequestJobs are |
42 // created -- the first is for the viewable document URL, and the | 52 // created -- the first is for the viewable document URL, and the |
43 // second is for the rediected URL. In order to test the | 53 // second is for the rediected URL. In order to test the |
44 // interceptor, the mime type of the first request must be one of | 54 // interceptor, the mime type of the first request must be one of |
45 // the supported viewable mime types. So when the net::URLRequestJob | 55 // the supported viewable mime types. So when the net::URLRequestJob |
46 // is a request for one of the test URLs that point to viewable | 56 // is a request for one of the test URLs that point to viewable |
47 // content, return an appropraite mime type. Otherwise, return | 57 // content, return an appropraite mime type. Otherwise, return |
48 // "text/html". | 58 // "text/html". |
49 if (request_->url() == GURL("http://foo.com/file.pdf")) { | 59 const GURL& request_url = request_->url(); |
| 60 if (request_url == GURL(kPdfUrl) || request_url == GURL(kPdfBlob)) { |
50 *mime_type = "application/pdf"; | 61 *mime_type = "application/pdf"; |
51 } else if (request_->url() == GURL("http://foo.com/file.ppt")) { | 62 } else if (request_url == GURL(kPptUrl)) { |
52 *mime_type = "application/vnd.ms-powerpoint"; | 63 *mime_type = "application/vnd.ms-powerpoint"; |
53 } else { | 64 } else { |
54 *mime_type = "text/html"; | 65 *mime_type = "text/html"; |
55 } | 66 } |
56 return true; | 67 return true; |
57 } | 68 } |
58 | 69 |
59 private: | 70 private: |
60 ~GViewURLRequestTestJob() {} | 71 ~GViewURLRequestTestJob() {} |
61 }; | 72 }; |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 TestingPrefService prefs_; | 203 TestingPrefService prefs_; |
193 scoped_refptr<PluginPrefs> plugin_prefs_; | 204 scoped_refptr<PluginPrefs> plugin_prefs_; |
194 net::URLRequestJobFactory job_factory_; | 205 net::URLRequestJobFactory job_factory_; |
195 const net::URLRequestJobFactory* old_factory_; | 206 const net::URLRequestJobFactory* old_factory_; |
196 scoped_refptr<ResourceHandler> handler_; | 207 scoped_refptr<ResourceHandler> handler_; |
197 TestDelegate test_delegate_; | 208 TestDelegate test_delegate_; |
198 FilePath pdf_path_; | 209 FilePath pdf_path_; |
199 }; | 210 }; |
200 | 211 |
201 TEST_F(GViewRequestInterceptorTest, DoNotInterceptHtml) { | 212 TEST_F(GViewRequestInterceptorTest, DoNotInterceptHtml) { |
202 net::URLRequest request(GURL("http://foo.com/index.html"), &test_delegate_); | 213 net::URLRequest request(GURL(kHtmlUrl), &test_delegate_); |
203 SetupRequest(&request); | 214 SetupRequest(&request); |
204 request.Start(); | 215 request.Start(); |
205 MessageLoop::current()->Run(); | 216 MessageLoop::current()->Run(); |
206 EXPECT_EQ(0, test_delegate_.received_redirect_count()); | 217 EXPECT_EQ(0, test_delegate_.received_redirect_count()); |
207 EXPECT_EQ(GURL("http://foo.com/index.html"), request.url()); | 218 EXPECT_EQ(GURL(kHtmlUrl), request.url()); |
208 } | 219 } |
209 | 220 |
210 TEST_F(GViewRequestInterceptorTest, DoNotInterceptDownload) { | 221 TEST_F(GViewRequestInterceptorTest, DoNotInterceptDownload) { |
211 net::URLRequest request(GURL("http://foo.com/file.pdf"), &test_delegate_); | 222 net::URLRequest request(GURL(kPdfUrl), &test_delegate_); |
212 SetupRequest(&request); | 223 SetupRequest(&request); |
213 request.set_load_flags(net::LOAD_IS_DOWNLOAD); | 224 request.set_load_flags(net::LOAD_IS_DOWNLOAD); |
214 request.Start(); | 225 request.Start(); |
215 MessageLoop::current()->Run(); | 226 MessageLoop::current()->Run(); |
216 EXPECT_EQ(0, test_delegate_.received_redirect_count()); | 227 EXPECT_EQ(0, test_delegate_.received_redirect_count()); |
217 EXPECT_EQ(GURL("http://foo.com/file.pdf"), request.url()); | 228 EXPECT_EQ(GURL(kPdfUrl), request.url()); |
218 } | 229 } |
219 | 230 |
220 TEST_F(GViewRequestInterceptorTest, DoNotInterceptPdfWhenEnabled) { | 231 TEST_F(GViewRequestInterceptorTest, DoNotInterceptPdfWhenEnabled) { |
221 SetPDFPluginLoadedState(true); | 232 SetPDFPluginLoadedState(true); |
222 plugin_prefs_->EnablePlugin(true, pdf_path_); | 233 plugin_prefs_->EnablePlugin(true, pdf_path_); |
223 | 234 |
224 net::URLRequest request(GURL("http://foo.com/file.pdf"), &test_delegate_); | 235 net::URLRequest request(GURL(kPdfUrl), &test_delegate_); |
225 SetupRequest(&request); | 236 SetupRequest(&request); |
226 request.Start(); | 237 request.Start(); |
227 MessageLoop::current()->Run(); | 238 MessageLoop::current()->Run(); |
228 EXPECT_EQ(0, test_delegate_.received_redirect_count()); | 239 EXPECT_EQ(0, test_delegate_.received_redirect_count()); |
229 EXPECT_EQ(GURL("http://foo.com/file.pdf"), request.url()); | 240 EXPECT_EQ(GURL(kPdfUrl), request.url()); |
230 } | 241 } |
231 | 242 |
232 TEST_F(GViewRequestInterceptorTest, InterceptPdfWhenDisabled) { | 243 TEST_F(GViewRequestInterceptorTest, InterceptPdfWhenDisabled) { |
233 SetPDFPluginLoadedState(true); | 244 SetPDFPluginLoadedState(true); |
234 plugin_prefs_->EnablePlugin(false, pdf_path_); | 245 plugin_prefs_->EnablePlugin(false, pdf_path_); |
235 | 246 |
236 net::URLRequest request(GURL("http://foo.com/file.pdf"), &test_delegate_); | 247 net::URLRequest request(GURL(kPdfUrl), &test_delegate_); |
237 SetupRequest(&request); | 248 SetupRequest(&request); |
238 request.Start(); | 249 request.Start(); |
239 MessageLoop::current()->Run(); | 250 MessageLoop::current()->Run(); |
240 EXPECT_EQ(1, test_delegate_.received_redirect_count()); | 251 EXPECT_EQ(1, test_delegate_.received_redirect_count()); |
241 EXPECT_EQ( | 252 EXPECT_EQ(GURL(kPdfUrlIntercepted), request.url()); |
242 GURL("http://docs.google.com/gview?url=http%3A//foo.com/file.pdf"), | |
243 request.url()); | |
244 } | 253 } |
245 | 254 |
246 TEST_F(GViewRequestInterceptorTest, InterceptPdfWithNoPlugin) { | 255 TEST_F(GViewRequestInterceptorTest, InterceptPdfWithNoPlugin) { |
247 SetPDFPluginLoadedState(false); | 256 SetPDFPluginLoadedState(false); |
248 | 257 |
249 net::URLRequest request(GURL("http://foo.com/file.pdf"), &test_delegate_); | 258 net::URLRequest request(GURL(kPdfUrl), &test_delegate_); |
250 SetupRequest(&request); | 259 SetupRequest(&request); |
251 request.Start(); | 260 request.Start(); |
252 MessageLoop::current()->Run(); | 261 MessageLoop::current()->Run(); |
253 EXPECT_EQ(1, test_delegate_.received_redirect_count()); | 262 EXPECT_EQ(1, test_delegate_.received_redirect_count()); |
254 EXPECT_EQ(GURL("http://docs.google.com/gview?url=http%3A//foo.com/file.pdf"), | 263 EXPECT_EQ(GURL(kPdfUrlIntercepted), request.url()); |
255 request.url()); | |
256 } | 264 } |
257 | 265 |
258 TEST_F(GViewRequestInterceptorTest, InterceptPowerpoint) { | 266 TEST_F(GViewRequestInterceptorTest, InterceptPowerpoint) { |
259 net::URLRequest request(GURL("http://foo.com/file.ppt"), &test_delegate_); | 267 net::URLRequest request(GURL(kPptUrl), &test_delegate_); |
260 SetupRequest(&request); | 268 SetupRequest(&request); |
261 request.Start(); | 269 request.Start(); |
262 MessageLoop::current()->Run(); | 270 MessageLoop::current()->Run(); |
263 EXPECT_EQ(1, test_delegate_.received_redirect_count()); | 271 EXPECT_EQ(1, test_delegate_.received_redirect_count()); |
264 EXPECT_EQ(GURL("http://docs.google.com/gview?url=http%3A//foo.com/file.ppt"), | 272 EXPECT_EQ(GURL(kPptUrlIntercepted), request.url()); |
265 request.url()); | 273 } |
| 274 |
| 275 TEST_F(GViewRequestInterceptorTest, DoNotInterceptPost) { |
| 276 SetPDFPluginLoadedState(false); |
| 277 |
| 278 net::URLRequest request(GURL(kPdfUrl), &test_delegate_); |
| 279 SetupRequest(&request); |
| 280 request.set_method("POST"); |
| 281 request.Start(); |
| 282 MessageLoop::current()->Run(); |
| 283 EXPECT_EQ(0, test_delegate_.received_redirect_count()); |
| 284 EXPECT_EQ(GURL(kPdfUrl), request.url()); |
| 285 } |
| 286 |
| 287 TEST_F(GViewRequestInterceptorTest, DoNotInterceptBlob) { |
| 288 SetPDFPluginLoadedState(false); |
| 289 |
| 290 net::URLRequest request(GURL(kPdfBlob), &test_delegate_); |
| 291 SetupRequest(&request); |
| 292 request.Start(); |
| 293 MessageLoop::current()->Run(); |
| 294 EXPECT_EQ(0, test_delegate_.received_redirect_count()); |
| 295 EXPECT_EQ(GURL(kPdfBlob), request.url()); |
266 } | 296 } |
267 | 297 |
268 } // namespace | 298 } // namespace |
269 | 299 |
270 } // namespace chromeos | 300 } // namespace chromeos |
OLD | NEW |