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

Side by Side Diff: content/test/net/url_request_mock_http_job.cc

Issue 300693005: Make URLRequestFilter use URLRequestInterceptors. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix merge Created 6 years, 6 months 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 "content/test/net/url_request_mock_http_job.h" 5 #include "content/test/net/url_request_mock_http_job.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "base/threading/sequenced_worker_pool.h" 11 #include "base/threading/sequenced_worker_pool.h"
12 #include "base/threading/thread_restrictions.h" 12 #include "base/threading/thread_restrictions.h"
13 #include "content/public/browser/browser_thread.h" 13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/common/url_constants.h" 14 #include "content/public/common/url_constants.h"
15 #include "net/base/filename_util.h" 15 #include "net/base/filename_util.h"
16 #include "net/http/http_response_headers.h" 16 #include "net/http/http_response_headers.h"
17 #include "net/url_request/url_request_filter.h" 17 #include "net/url_request/url_request_filter.h"
18 #include "net/url_request/url_request_interceptor.h"
18 19
19 const char kMockHostname[] = "mock.http"; 20 const char kMockHostname[] = "mock.http";
20 const base::FilePath::CharType kMockHeaderFileSuffix[] = 21 const base::FilePath::CharType kMockHeaderFileSuffix[] =
21 FILE_PATH_LITERAL(".mock-http-headers"); 22 FILE_PATH_LITERAL(".mock-http-headers");
22 23
23 namespace content { 24 namespace content {
24 25
25 namespace { 26 namespace {
26 27
27 class ProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler { 28 class MockJobInterceptor : public net::URLRequestInterceptor {
28 public: 29 public:
29 // When |map_all_requests_to_base_path| is true, all request should return the 30 // When |map_all_requests_to_base_path| is true, all request should return the
30 // contents of the file at |base_path|. When |map_all_requests_to_base_path| 31 // contents of the file at |base_path|. When |map_all_requests_to_base_path|
31 // is false, |base_path| is the file path leading to the root of the directory 32 // is false, |base_path| is the file path leading to the root of the directory
32 // to use as the root of the HTTP server. 33 // to use as the root of the HTTP server.
33 explicit ProtocolHandler(const base::FilePath& base_path, 34 MockJobInterceptor(const base::FilePath& base_path,
34 bool map_all_requests_to_base_path) 35 bool map_all_requests_to_base_path)
35 : base_path_(base_path), 36 : base_path_(base_path),
36 map_all_requests_to_base_path_(map_all_requests_to_base_path) {} 37 map_all_requests_to_base_path_(map_all_requests_to_base_path) {}
37 virtual ~ProtocolHandler() {} 38 virtual ~MockJobInterceptor() {}
38 39
39 // net::URLRequestJobFactory::ProtocolHandler implementation 40 // net::URLRequestJobFactory::ProtocolHandler implementation
40 virtual net::URLRequestJob* MaybeCreateJob( 41 virtual net::URLRequestJob* MaybeInterceptRequest(
41 net::URLRequest* request, 42 net::URLRequest* request,
42 net::NetworkDelegate* network_delegate) const OVERRIDE { 43 net::NetworkDelegate* network_delegate) const OVERRIDE {
43 return new URLRequestMockHTTPJob(request, network_delegate, 44 return new URLRequestMockHTTPJob(request, network_delegate,
44 map_all_requests_to_base_path_ ? base_path_ : GetOnDiskPath(request)); 45 map_all_requests_to_base_path_ ? base_path_ : GetOnDiskPath(request));
45 } 46 }
46 47
47 private: 48 private:
48 base::FilePath GetOnDiskPath(net::URLRequest* request) const { 49 base::FilePath GetOnDiskPath(net::URLRequest* request) const {
49 // Conceptually we just want to "return base_path_ + request->url().path()". 50 // Conceptually we just want to "return base_path_ + request->url().path()".
50 // But path in the request URL is in URL space (i.e. %-encoded spaces). 51 // But path in the request URL is in URL space (i.e. %-encoded spaces).
51 // So first we convert base FilePath to a URL, then append the URL 52 // So first we convert base FilePath to a URL, then append the URL
52 // path to that, and convert the final URL back to a FilePath. 53 // path to that, and convert the final URL back to a FilePath.
53 GURL file_url(net::FilePathToFileURL(base_path_)); 54 GURL file_url(net::FilePathToFileURL(base_path_));
54 std::string url = file_url.spec() + request->url().path(); 55 std::string url = file_url.spec() + request->url().path();
55 base::FilePath file_path; 56 base::FilePath file_path;
56 net::FileURLToFilePath(GURL(url), &file_path); 57 net::FileURLToFilePath(GURL(url), &file_path);
57 return file_path; 58 return file_path;
58 } 59 }
59 60
60 const base::FilePath base_path_; 61 const base::FilePath base_path_;
61 const bool map_all_requests_to_base_path_; 62 const bool map_all_requests_to_base_path_;
62 63
63 DISALLOW_COPY_AND_ASSIGN(ProtocolHandler); 64 DISALLOW_COPY_AND_ASSIGN(MockJobInterceptor);
64 }; 65 };
65 66
66 } // namespace 67 } // namespace
67 68
68 // static 69 // static
69 void URLRequestMockHTTPJob::AddUrlHandler(const base::FilePath& base_path) { 70 void URLRequestMockHTTPJob::AddUrlHandler(const base::FilePath& base_path) {
70 // Add kMockHostname to net::URLRequestFilter. 71 // Add kMockHostname to net::URLRequestFilter.
71 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); 72 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
72 filter->AddHostnameProtocolHandler("http", kMockHostname, 73 filter->AddHostnameInterceptor(
73 CreateProtocolHandler(base_path)); 74 "http", kMockHostname, CreateInterceptor(base_path));
74 } 75 }
75 76
76 // static 77 // static
77 void URLRequestMockHTTPJob::AddHostnameToFileHandler( 78 void URLRequestMockHTTPJob::AddHostnameToFileHandler(
78 const std::string& hostname, 79 const std::string& hostname,
79 const base::FilePath& file) { 80 const base::FilePath& file) {
80 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); 81 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
81 filter->AddHostnameProtocolHandler( 82 filter->AddHostnameInterceptor(
82 "http", hostname, CreateProtocolHandlerForSingleFile(file)); 83 "http", hostname, CreateInterceptorForSingleFile(file));
83 } 84 }
84 85
85 // static 86 // static
86 GURL URLRequestMockHTTPJob::GetMockUrl(const base::FilePath& path) { 87 GURL URLRequestMockHTTPJob::GetMockUrl(const base::FilePath& path) {
87 std::string url = "http://"; 88 std::string url = "http://";
88 url.append(kMockHostname); 89 url.append(kMockHostname);
89 url.append("/"); 90 url.append("/");
90 std::string path_str = path.MaybeAsASCII(); 91 std::string path_str = path.MaybeAsASCII();
91 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. 92 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests.
92 url.append(path_str); 93 url.append(path_str);
93 return GURL(url); 94 return GURL(url);
94 } 95 }
95 96
96 // static 97 // static
97 GURL URLRequestMockHTTPJob::GetMockViewSourceUrl(const base::FilePath& path) { 98 GURL URLRequestMockHTTPJob::GetMockViewSourceUrl(const base::FilePath& path) {
98 std::string url = kViewSourceScheme; 99 std::string url = kViewSourceScheme;
99 url.append(":"); 100 url.append(":");
100 url.append(GetMockUrl(path).spec()); 101 url.append(GetMockUrl(path).spec());
101 return GURL(url); 102 return GURL(url);
102 } 103 }
103 104
104 // static 105 // static
105 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> 106 scoped_ptr<net::URLRequestInterceptor>
106 URLRequestMockHTTPJob::CreateProtocolHandler(const base::FilePath& base_path) { 107 URLRequestMockHTTPJob::CreateInterceptor(const base::FilePath& base_path) {
107 return scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>( 108 return scoped_ptr<net::URLRequestInterceptor>(
108 new ProtocolHandler(base_path, false)); 109 new MockJobInterceptor(base_path, false));
109 } 110 }
110 111
111 // static 112 // static
112 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> 113 scoped_ptr<net::URLRequestInterceptor>
113 URLRequestMockHTTPJob::CreateProtocolHandlerForSingleFile( 114 URLRequestMockHTTPJob::CreateInterceptorForSingleFile(
114 const base::FilePath& file) { 115 const base::FilePath& file) {
115 return scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>( 116 return scoped_ptr<net::URLRequestInterceptor>(
116 new ProtocolHandler(file, true)); 117 new MockJobInterceptor(file, true));
117 } 118 }
118 119
119 URLRequestMockHTTPJob::URLRequestMockHTTPJob( 120 URLRequestMockHTTPJob::URLRequestMockHTTPJob(
120 net::URLRequest* request, net::NetworkDelegate* network_delegate, 121 net::URLRequest* request, net::NetworkDelegate* network_delegate,
121 const base::FilePath& file_path) 122 const base::FilePath& file_path)
122 : net::URLRequestFileJob( 123 : net::URLRequestFileJob(
123 request, network_delegate, file_path, 124 request, network_delegate, file_path,
124 content::BrowserThread::GetBlockingPool()-> 125 content::BrowserThread::GetBlockingPool()->
125 GetTaskRunnerWithShutdownBehavior( 126 GetTaskRunnerWithShutdownBehavior(
126 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)) {} 127 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)) {}
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 return net::URLRequestJob::GetResponseCode(); 181 return net::URLRequestJob::GetResponseCode();
181 } 182 }
182 183
183 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { 184 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) {
184 net::HttpResponseInfo info; 185 net::HttpResponseInfo info;
185 GetResponseInfo(&info); 186 GetResponseInfo(&info);
186 return info.headers.get() && info.headers->GetCharset(charset); 187 return info.headers.get() && info.headers->GetCharset(charset);
187 } 188 }
188 189
189 } // namespace content 190 } // namespace content
OLDNEW
« no previous file with comments | « content/test/net/url_request_mock_http_job.h ('k') | content/test/net/url_request_prepackaged_interceptor.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698