OLD | NEW |
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 "net/test/url_request/url_request_mock_http_job.h" |
6 | 6 |
7 #include "base/files/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/task_runner_util.h" |
11 #include "base/threading/sequenced_worker_pool.h" | 12 #include "base/threading/sequenced_worker_pool.h" |
12 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
13 #include "content/public/browser/browser_thread.h" | |
14 #include "content/public/common/url_constants.h" | |
15 #include "net/base/filename_util.h" | 14 #include "net/base/filename_util.h" |
16 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
17 #include "net/url_request/url_request_filter.h" | 16 #include "net/url_request/url_request_filter.h" |
18 #include "net/url_request/url_request_interceptor.h" | 17 #include "net/url_request/url_request_interceptor.h" |
19 | 18 |
20 const char kMockHostname[] = "mock.http"; | 19 const char kMockHostname[] = "mock.http"; |
21 const base::FilePath::CharType kMockHeaderFileSuffix[] = | 20 const base::FilePath::CharType kMockHeaderFileSuffix[] = |
22 FILE_PATH_LITERAL(".mock-http-headers"); | 21 FILE_PATH_LITERAL(".mock-http-headers"); |
23 | 22 |
24 namespace content { | 23 namespace net { |
25 | 24 |
26 namespace { | 25 namespace { |
27 | 26 |
28 class MockJobInterceptor : public net::URLRequestInterceptor { | 27 class MockJobInterceptor : public net::URLRequestInterceptor { |
29 public: | 28 public: |
30 // When |map_all_requests_to_base_path| is true, all request should return the | 29 // When |map_all_requests_to_base_path| is true, all request should return the |
31 // contents of the file at |base_path|. When |map_all_requests_to_base_path| | 30 // contents of the file at |base_path|. When |map_all_requests_to_base_path| |
32 // is false, |base_path| is the file path leading to the root of the directory | 31 // is false, |base_path| is the file path leading to the root of the directory |
33 // to use as the root of the HTTP server. | 32 // to use as the root of the HTTP server. |
34 MockJobInterceptor(const base::FilePath& base_path, | 33 MockJobInterceptor( |
35 bool map_all_requests_to_base_path) | 34 const base::FilePath& base_path, |
| 35 bool map_all_requests_to_base_path, |
| 36 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) |
36 : base_path_(base_path), | 37 : base_path_(base_path), |
37 map_all_requests_to_base_path_(map_all_requests_to_base_path) {} | 38 map_all_requests_to_base_path_(map_all_requests_to_base_path), |
| 39 worker_pool_(worker_pool) {} |
38 virtual ~MockJobInterceptor() {} | 40 virtual ~MockJobInterceptor() {} |
39 | 41 |
40 // net::URLRequestJobFactory::ProtocolHandler implementation | 42 // net::URLRequestJobFactory::ProtocolHandler implementation |
41 virtual net::URLRequestJob* MaybeInterceptRequest( | 43 virtual net::URLRequestJob* MaybeInterceptRequest( |
42 net::URLRequest* request, | 44 net::URLRequest* request, |
43 net::NetworkDelegate* network_delegate) const OVERRIDE { | 45 net::NetworkDelegate* network_delegate) const OVERRIDE { |
44 return new URLRequestMockHTTPJob(request, network_delegate, | 46 return new URLRequestMockHTTPJob( |
45 map_all_requests_to_base_path_ ? base_path_ : GetOnDiskPath(request)); | 47 request, |
| 48 network_delegate, |
| 49 map_all_requests_to_base_path_ ? base_path_ : GetOnDiskPath(request), |
| 50 worker_pool_->GetTaskRunnerWithShutdownBehavior( |
| 51 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)); |
46 } | 52 } |
47 | 53 |
48 private: | 54 private: |
49 base::FilePath GetOnDiskPath(net::URLRequest* request) const { | 55 base::FilePath GetOnDiskPath(net::URLRequest* request) const { |
50 // Conceptually we just want to "return base_path_ + request->url().path()". | 56 // Conceptually we just want to "return base_path_ + request->url().path()". |
51 // But path in the request URL is in URL space (i.e. %-encoded spaces). | 57 // But path in the request URL is in URL space (i.e. %-encoded spaces). |
52 // So first we convert base FilePath to a URL, then append the URL | 58 // So first we convert base FilePath to a URL, then append the URL |
53 // path to that, and convert the final URL back to a FilePath. | 59 // path to that, and convert the final URL back to a FilePath. |
54 GURL file_url(net::FilePathToFileURL(base_path_)); | 60 GURL file_url(net::FilePathToFileURL(base_path_)); |
55 std::string url = file_url.spec() + request->url().path(); | 61 std::string url = file_url.spec() + request->url().path(); |
56 base::FilePath file_path; | 62 base::FilePath file_path; |
57 net::FileURLToFilePath(GURL(url), &file_path); | 63 net::FileURLToFilePath(GURL(url), &file_path); |
58 return file_path; | 64 return file_path; |
59 } | 65 } |
60 | 66 |
61 const base::FilePath base_path_; | 67 const base::FilePath base_path_; |
62 const bool map_all_requests_to_base_path_; | 68 const bool map_all_requests_to_base_path_; |
| 69 const scoped_refptr<base::SequencedWorkerPool> worker_pool_; |
63 | 70 |
64 DISALLOW_COPY_AND_ASSIGN(MockJobInterceptor); | 71 DISALLOW_COPY_AND_ASSIGN(MockJobInterceptor); |
65 }; | 72 }; |
66 | 73 |
| 74 std::string DoFileIO(const base::FilePath& file_path) { |
| 75 base::FilePath header_file = |
| 76 base::FilePath(file_path.value() + kMockHeaderFileSuffix); |
| 77 |
| 78 if (!base::PathExists(header_file)) { |
| 79 // If there is no mock-http-headers file, fake a 200 OK. |
| 80 return "HTTP/1.0 200 OK\n"; |
| 81 } |
| 82 |
| 83 std::string raw_headers; |
| 84 base::ReadFileToString(header_file, &raw_headers); |
| 85 return raw_headers; |
| 86 } |
| 87 |
67 } // namespace | 88 } // namespace |
68 | 89 |
69 // static | 90 // static |
70 void URLRequestMockHTTPJob::AddUrlHandler(const base::FilePath& base_path) { | 91 void URLRequestMockHTTPJob::AddUrlHandler( |
| 92 const base::FilePath& base_path, |
| 93 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { |
71 // Add kMockHostname to net::URLRequestFilter. | 94 // Add kMockHostname to net::URLRequestFilter. |
72 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 95 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
73 filter->AddHostnameInterceptor( | 96 filter->AddHostnameInterceptor( |
74 "http", kMockHostname, CreateInterceptor(base_path)); | 97 "http", kMockHostname, CreateInterceptor(base_path, worker_pool)); |
75 } | 98 } |
76 | 99 |
77 // static | 100 // static |
78 void URLRequestMockHTTPJob::AddHostnameToFileHandler( | 101 void URLRequestMockHTTPJob::AddHostnameToFileHandler( |
79 const std::string& hostname, | 102 const std::string& hostname, |
80 const base::FilePath& file) { | 103 const base::FilePath& file, |
| 104 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { |
81 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 105 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
82 filter->AddHostnameInterceptor( | 106 filter->AddHostnameInterceptor( |
83 "http", hostname, CreateInterceptorForSingleFile(file)); | 107 "http", hostname, CreateInterceptorForSingleFile(file, worker_pool)); |
84 } | 108 } |
85 | 109 |
86 // static | 110 // static |
87 GURL URLRequestMockHTTPJob::GetMockUrl(const base::FilePath& path) { | 111 GURL URLRequestMockHTTPJob::GetMockUrl(const base::FilePath& path) { |
88 std::string url = "http://"; | 112 std::string url = "http://"; |
89 url.append(kMockHostname); | 113 url.append(kMockHostname); |
90 url.append("/"); | 114 url.append("/"); |
91 std::string path_str = path.MaybeAsASCII(); | 115 std::string path_str = path.MaybeAsASCII(); |
92 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. | 116 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. |
93 url.append(path_str); | 117 url.append(path_str); |
94 return GURL(url); | 118 return GURL(url); |
95 } | 119 } |
96 | 120 |
97 // static | 121 // static |
98 GURL URLRequestMockHTTPJob::GetMockViewSourceUrl(const base::FilePath& path) { | 122 scoped_ptr<net::URLRequestInterceptor> URLRequestMockHTTPJob::CreateInterceptor( |
99 std::string url = kViewSourceScheme; | 123 const base::FilePath& base_path, |
100 url.append(":"); | 124 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { |
101 url.append(GetMockUrl(path).spec()); | |
102 return GURL(url); | |
103 } | |
104 | |
105 // static | |
106 scoped_ptr<net::URLRequestInterceptor> | |
107 URLRequestMockHTTPJob::CreateInterceptor(const base::FilePath& base_path) { | |
108 return scoped_ptr<net::URLRequestInterceptor>( | 125 return scoped_ptr<net::URLRequestInterceptor>( |
109 new MockJobInterceptor(base_path, false)); | 126 new MockJobInterceptor(base_path, false, worker_pool)); |
110 } | 127 } |
111 | 128 |
112 // static | 129 // static |
113 scoped_ptr<net::URLRequestInterceptor> | 130 scoped_ptr<net::URLRequestInterceptor> |
114 URLRequestMockHTTPJob::CreateInterceptorForSingleFile( | 131 URLRequestMockHTTPJob::CreateInterceptorForSingleFile( |
115 const base::FilePath& file) { | 132 const base::FilePath& file, |
| 133 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { |
116 return scoped_ptr<net::URLRequestInterceptor>( | 134 return scoped_ptr<net::URLRequestInterceptor>( |
117 new MockJobInterceptor(file, true)); | 135 new MockJobInterceptor(file, true, worker_pool)); |
118 } | 136 } |
119 | 137 |
120 URLRequestMockHTTPJob::URLRequestMockHTTPJob( | 138 URLRequestMockHTTPJob::URLRequestMockHTTPJob( |
121 net::URLRequest* request, net::NetworkDelegate* network_delegate, | 139 net::URLRequest* request, |
122 const base::FilePath& file_path) | 140 net::NetworkDelegate* network_delegate, |
123 : net::URLRequestFileJob( | 141 const base::FilePath& file_path, |
124 request, network_delegate, file_path, | 142 const scoped_refptr<base::TaskRunner>& task_runner) |
125 content::BrowserThread::GetBlockingPool()-> | 143 : net::URLRequestFileJob(request, network_delegate, file_path, task_runner), |
126 GetTaskRunnerWithShutdownBehavior( | 144 task_runner_(task_runner), |
127 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)) {} | 145 weak_ptr_factory_(this) { |
| 146 } |
128 | 147 |
129 URLRequestMockHTTPJob::~URLRequestMockHTTPJob() { } | 148 URLRequestMockHTTPJob::~URLRequestMockHTTPJob() { |
| 149 } |
130 | 150 |
131 // Public virtual version. | 151 // Public virtual version. |
132 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) { | 152 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) { |
133 // Forward to private const version. | 153 // Forward to private const version. |
134 GetResponseInfoConst(info); | 154 GetResponseInfoConst(info); |
135 } | 155 } |
136 | 156 |
137 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, | 157 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, |
138 int* http_status_code) { | 158 int* http_status_code) { |
139 // Override the net::URLRequestFileJob implementation to invoke the default | 159 // Override the net::URLRequestFileJob implementation to invoke the default |
140 // one based on HttpResponseInfo. | 160 // one based on HttpResponseInfo. |
141 return net::URLRequestJob::IsRedirectResponse(location, http_status_code); | 161 return net::URLRequestJob::IsRedirectResponse(location, http_status_code); |
142 } | 162 } |
143 | 163 |
| 164 // Public virtual version. |
| 165 void URLRequestMockHTTPJob::Start() { |
| 166 base::PostTaskAndReplyWithResult( |
| 167 task_runner_.get(), |
| 168 FROM_HERE, |
| 169 base::Bind(&DoFileIO, file_path_), |
| 170 base::Bind(&URLRequestMockHTTPJob::GetRawHeaders, |
| 171 weak_ptr_factory_.GetWeakPtr())); |
| 172 } |
| 173 |
| 174 void URLRequestMockHTTPJob::GetRawHeaders(std::string raw_headers) { |
| 175 // Handle CRLF line-endings. |
| 176 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\r\n", "\n"); |
| 177 // ParseRawHeaders expects \0 to end each header line. |
| 178 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); |
| 179 raw_headers_ = raw_headers; |
| 180 URLRequestFileJob::Start(); |
| 181 } |
| 182 |
144 // Private const version. | 183 // Private const version. |
145 void URLRequestMockHTTPJob::GetResponseInfoConst( | 184 void URLRequestMockHTTPJob::GetResponseInfoConst( |
146 net::HttpResponseInfo* info) const { | 185 net::HttpResponseInfo* info) const { |
147 // We have to load our headers from disk, but we only use this class | 186 info->headers = new net::HttpResponseHeaders(raw_headers_); |
148 // from tests, so allow these IO operations to happen on any thread. | |
149 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
150 | |
151 base::FilePath header_file = | |
152 base::FilePath(file_path_.value() + kMockHeaderFileSuffix); | |
153 std::string raw_headers; | |
154 if (!base::PathExists(header_file)) { | |
155 // If there is no mock-http-headers file, fake a 200 OK. | |
156 raw_headers = "HTTP/1.0 200 OK\n"; | |
157 } else { | |
158 if (!base::ReadFileToString(header_file, &raw_headers)) | |
159 return; | |
160 } | |
161 | |
162 // Handle CRLF line-endings. | |
163 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\r\n", "\n"); | |
164 // ParseRawHeaders expects \0 to end each header line. | |
165 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); | |
166 info->headers = new net::HttpResponseHeaders(raw_headers); | |
167 } | 187 } |
168 | 188 |
169 bool URLRequestMockHTTPJob::GetMimeType(std::string* mime_type) const { | 189 bool URLRequestMockHTTPJob::GetMimeType(std::string* mime_type) const { |
170 net::HttpResponseInfo info; | 190 net::HttpResponseInfo info; |
171 GetResponseInfoConst(&info); | 191 GetResponseInfoConst(&info); |
172 return info.headers.get() && info.headers->GetMimeType(mime_type); | 192 return info.headers.get() && info.headers->GetMimeType(mime_type); |
173 } | 193 } |
174 | 194 |
175 int URLRequestMockHTTPJob::GetResponseCode() const { | 195 int URLRequestMockHTTPJob::GetResponseCode() const { |
176 net::HttpResponseInfo info; | 196 net::HttpResponseInfo info; |
177 GetResponseInfoConst(&info); | 197 GetResponseInfoConst(&info); |
178 // If we have headers, get the response code from them. | 198 // If we have headers, get the response code from them. |
179 if (info.headers.get()) | 199 if (info.headers.get()) |
180 return info.headers->response_code(); | 200 return info.headers->response_code(); |
181 return net::URLRequestJob::GetResponseCode(); | 201 return net::URLRequestJob::GetResponseCode(); |
182 } | 202 } |
183 | 203 |
184 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { | 204 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { |
185 net::HttpResponseInfo info; | 205 net::HttpResponseInfo info; |
186 GetResponseInfo(&info); | 206 GetResponseInfo(&info); |
187 return info.headers.get() && info.headers->GetCharset(charset); | 207 return info.headers.get() && info.headers->GetCharset(charset); |
188 } | 208 } |
189 | 209 |
190 } // namespace content | 210 } // namespace net |
OLD | NEW |