Chromium Code Reviews| 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/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" | |
| 14 #include "content/public/common/url_constants.h" | |
| 15 #include "net/base/filename_util.h" | 13 #include "net/base/filename_util.h" |
| 16 #include "net/http/http_response_headers.h" | 14 #include "net/http/http_response_headers.h" |
| 17 #include "net/url_request/url_request_filter.h" | 15 #include "net/url_request/url_request_filter.h" |
| 18 #include "net/url_request/url_request_interceptor.h" | 16 #include "net/url_request/url_request_interceptor.h" |
| 19 | 17 |
| 20 const char kMockHostname[] = "mock.http"; | 18 const char kMockHostname[] = "mock.http"; |
| 21 const base::FilePath::CharType kMockHeaderFileSuffix[] = | 19 const base::FilePath::CharType kMockHeaderFileSuffix[] = |
| 22 FILE_PATH_LITERAL(".mock-http-headers"); | 20 FILE_PATH_LITERAL(".mock-http-headers"); |
| 23 | 21 |
| 24 namespace content { | 22 namespace net { |
| 25 | 23 |
| 26 namespace { | 24 namespace { |
| 27 | 25 |
| 28 class MockJobInterceptor : public net::URLRequestInterceptor { | 26 class MockJobInterceptor : public net::URLRequestInterceptor { |
|
mmenke
2014/09/04 18:05:12
nit: Remove all the "net::", now that we're in ne
| |
| 29 public: | 27 public: |
| 30 // When |map_all_requests_to_base_path| is true, all request should return the | 28 // 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| | 29 // 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 | 30 // 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. | 31 // to use as the root of the HTTP server. |
| 34 MockJobInterceptor(const base::FilePath& base_path, | 32 MockJobInterceptor(const base::FilePath& base_path, |
| 35 bool map_all_requests_to_base_path) | 33 bool map_all_requests_to_base_path, |
| 34 base::SequencedWorkerPool* worker_pool) | |
| 36 : base_path_(base_path), | 35 : base_path_(base_path), |
| 37 map_all_requests_to_base_path_(map_all_requests_to_base_path) {} | 36 map_all_requests_to_base_path_(map_all_requests_to_base_path), |
| 37 worker_pool_(worker_pool) {} | |
| 38 virtual ~MockJobInterceptor() {} | 38 virtual ~MockJobInterceptor() {} |
| 39 | 39 |
| 40 // net::URLRequestJobFactory::ProtocolHandler implementation | 40 // net::URLRequestJobFactory::ProtocolHandler implementation |
| 41 virtual net::URLRequestJob* MaybeInterceptRequest( | 41 virtual net::URLRequestJob* MaybeInterceptRequest( |
| 42 net::URLRequest* request, | 42 net::URLRequest* request, |
| 43 net::NetworkDelegate* network_delegate) const OVERRIDE { | 43 net::NetworkDelegate* network_delegate) const OVERRIDE { |
| 44 return new URLRequestMockHTTPJob(request, network_delegate, | 44 return new URLRequestMockHTTPJob(request, network_delegate, |
| 45 map_all_requests_to_base_path_ ? base_path_ : GetOnDiskPath(request)); | 45 map_all_requests_to_base_path_ ? base_path_ : GetOnDiskPath(request), |
| 46 worker_pool_); | |
| 46 } | 47 } |
| 47 | 48 |
| 48 private: | 49 private: |
| 49 base::FilePath GetOnDiskPath(net::URLRequest* request) const { | 50 base::FilePath GetOnDiskPath(net::URLRequest* request) const { |
| 50 // Conceptually we just want to "return base_path_ + request->url().path()". | 51 // 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). | 52 // 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 | 53 // 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. | 54 // path to that, and convert the final URL back to a FilePath. |
| 54 GURL file_url(net::FilePathToFileURL(base_path_)); | 55 GURL file_url(net::FilePathToFileURL(base_path_)); |
| 55 std::string url = file_url.spec() + request->url().path(); | 56 std::string url = file_url.spec() + request->url().path(); |
| 56 base::FilePath file_path; | 57 base::FilePath file_path; |
| 57 net::FileURLToFilePath(GURL(url), &file_path); | 58 net::FileURLToFilePath(GURL(url), &file_path); |
| 58 return file_path; | 59 return file_path; |
| 59 } | 60 } |
| 60 | 61 |
| 61 const base::FilePath base_path_; | 62 const base::FilePath base_path_; |
| 62 const bool map_all_requests_to_base_path_; | 63 const bool map_all_requests_to_base_path_; |
| 64 base::SequencedWorkerPool* worker_pool_; | |
| 63 | 65 |
| 64 DISALLOW_COPY_AND_ASSIGN(MockJobInterceptor); | 66 DISALLOW_COPY_AND_ASSIGN(MockJobInterceptor); |
| 65 }; | 67 }; |
| 66 | 68 |
| 67 } // namespace | 69 } // namespace |
| 68 | 70 |
| 69 // static | 71 // static |
| 70 void URLRequestMockHTTPJob::AddUrlHandler(const base::FilePath& base_path) { | 72 void URLRequestMockHTTPJob::AddUrlHandler( |
| 73 const base::FilePath& base_path, | |
| 74 base::SequencedWorkerPool* worker_pool) { | |
| 71 // Add kMockHostname to net::URLRequestFilter. | 75 // Add kMockHostname to net::URLRequestFilter. |
| 72 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 76 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| 73 filter->AddHostnameInterceptor( | 77 filter->AddHostnameInterceptor( |
| 74 "http", kMockHostname, CreateInterceptor(base_path)); | 78 "http", kMockHostname, CreateInterceptor(base_path, worker_pool)); |
| 75 } | 79 } |
| 76 | 80 |
| 77 // static | 81 // static |
| 78 void URLRequestMockHTTPJob::AddHostnameToFileHandler( | 82 void URLRequestMockHTTPJob::AddHostnameToFileHandler( |
| 79 const std::string& hostname, | 83 const std::string& hostname, |
| 80 const base::FilePath& file) { | 84 const base::FilePath& file, |
| 85 base::SequencedWorkerPool* worker_pool) { | |
| 81 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 86 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| 82 filter->AddHostnameInterceptor( | 87 filter->AddHostnameInterceptor( |
| 83 "http", hostname, CreateInterceptorForSingleFile(file)); | 88 "http", hostname, |
| 89 CreateInterceptorForSingleFile(file, worker_pool)); | |
| 84 } | 90 } |
| 85 | 91 |
| 86 // static | 92 // static |
| 87 GURL URLRequestMockHTTPJob::GetMockUrl(const base::FilePath& path) { | 93 GURL URLRequestMockHTTPJob::GetMockUrl(const base::FilePath& path) { |
| 88 std::string url = "http://"; | 94 std::string url = "http://"; |
| 89 url.append(kMockHostname); | 95 url.append(kMockHostname); |
| 90 url.append("/"); | 96 url.append("/"); |
| 91 std::string path_str = path.MaybeAsASCII(); | 97 std::string path_str = path.MaybeAsASCII(); |
| 92 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. | 98 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. |
| 93 url.append(path_str); | 99 url.append(path_str); |
| 94 return GURL(url); | 100 return GURL(url); |
| 95 } | 101 } |
| 96 | 102 |
| 97 // static | 103 // static |
| 98 GURL URLRequestMockHTTPJob::GetMockViewSourceUrl(const base::FilePath& path) { | 104 GURL URLRequestMockHTTPJob::GetMockViewSourceUrl(const base::FilePath& path) { |
| 99 std::string url = kViewSourceScheme; | 105 std::string url = "view-source"; |
|
mmenke
2014/09/04 18:05:12
This is a layering violation. net shouldn't know
xunjieli
2014/09/04 18:44:25
Done. Thanks! That was a bad thing to do. I was pl
| |
| 100 url.append(":"); | 106 url.append(":"); |
| 101 url.append(GetMockUrl(path).spec()); | 107 url.append(GetMockUrl(path).spec()); |
| 102 return GURL(url); | 108 return GURL(url); |
| 103 } | 109 } |
| 104 | 110 |
| 105 // static | 111 // static |
| 106 scoped_ptr<net::URLRequestInterceptor> | 112 scoped_ptr<net::URLRequestInterceptor> |
| 107 URLRequestMockHTTPJob::CreateInterceptor(const base::FilePath& base_path) { | 113 URLRequestMockHTTPJob::CreateInterceptor( |
| 114 const base::FilePath& base_path, | |
| 115 base::SequencedWorkerPool* worker_pool) { | |
| 108 return scoped_ptr<net::URLRequestInterceptor>( | 116 return scoped_ptr<net::URLRequestInterceptor>( |
| 109 new MockJobInterceptor(base_path, false)); | 117 new MockJobInterceptor(base_path, false, worker_pool)); |
| 110 } | 118 } |
| 111 | 119 |
| 112 // static | 120 // static |
| 113 scoped_ptr<net::URLRequestInterceptor> | 121 scoped_ptr<net::URLRequestInterceptor> |
| 114 URLRequestMockHTTPJob::CreateInterceptorForSingleFile( | 122 URLRequestMockHTTPJob::CreateInterceptorForSingleFile( |
| 115 const base::FilePath& file) { | 123 const base::FilePath& file, |
| 124 base::SequencedWorkerPool* worker_pool) { | |
| 116 return scoped_ptr<net::URLRequestInterceptor>( | 125 return scoped_ptr<net::URLRequestInterceptor>( |
| 117 new MockJobInterceptor(file, true)); | 126 new MockJobInterceptor(file, true, worker_pool)); |
| 118 } | 127 } |
| 119 | 128 |
| 120 URLRequestMockHTTPJob::URLRequestMockHTTPJob( | 129 URLRequestMockHTTPJob::URLRequestMockHTTPJob( |
| 121 net::URLRequest* request, net::NetworkDelegate* network_delegate, | 130 net::URLRequest* request, net::NetworkDelegate* network_delegate, |
| 122 const base::FilePath& file_path) | 131 const base::FilePath& file_path, |
| 132 base::SequencedWorkerPool* worker_pool) | |
| 123 : net::URLRequestFileJob( | 133 : net::URLRequestFileJob( |
| 124 request, network_delegate, file_path, | 134 request, network_delegate, file_path, |
| 125 content::BrowserThread::GetBlockingPool()-> | 135 worker_pool->GetTaskRunnerWithShutdownBehavior( |
| 126 GetTaskRunnerWithShutdownBehavior( | 136 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)) {} |
| 127 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)) {} | |
| 128 | 137 |
| 129 URLRequestMockHTTPJob::~URLRequestMockHTTPJob() { } | 138 URLRequestMockHTTPJob::~URLRequestMockHTTPJob() { } |
| 130 | 139 |
| 131 // Public virtual version. | 140 // Public virtual version. |
| 132 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) { | 141 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) { |
| 133 // Forward to private const version. | 142 // Forward to private const version. |
| 134 GetResponseInfoConst(info); | 143 GetResponseInfoConst(info); |
| 135 } | 144 } |
| 136 | 145 |
| 137 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, | 146 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, |
| 138 int* http_status_code) { | 147 int* http_status_code) { |
| 139 // Override the net::URLRequestFileJob implementation to invoke the default | 148 // Override the net::URLRequestFileJob implementation to invoke the default |
| 140 // one based on HttpResponseInfo. | 149 // one based on HttpResponseInfo. |
| 141 return net::URLRequestJob::IsRedirectResponse(location, http_status_code); | 150 return net::URLRequestJob::IsRedirectResponse(location, http_status_code); |
| 142 } | 151 } |
| 143 | 152 |
| 144 // Private const version. | 153 // Private const version. |
| 145 void URLRequestMockHTTPJob::GetResponseInfoConst( | 154 void URLRequestMockHTTPJob::GetResponseInfoConst( |
| 146 net::HttpResponseInfo* info) const { | 155 net::HttpResponseInfo* info) const { |
| 147 // We have to load our headers from disk, but we only use this class | 156 // We have to load our headers from disk, but we only use this class |
| 148 // from tests, so allow these IO operations to happen on any thread. | 157 // from tests, so allow these IO operations to happen on any thread. |
| 149 base::ThreadRestrictions::ScopedAllowIO allow_io; | 158 base::ThreadRestrictions::SetIOAllowed(true); |
| 150 | 159 |
| 151 base::FilePath header_file = | 160 base::FilePath header_file = |
| 152 base::FilePath(file_path_.value() + kMockHeaderFileSuffix); | 161 base::FilePath(file_path_.value() + kMockHeaderFileSuffix); |
| 153 std::string raw_headers; | 162 std::string raw_headers; |
| 154 if (!base::PathExists(header_file)) { | 163 if (!base::PathExists(header_file)) { |
| 155 // If there is no mock-http-headers file, fake a 200 OK. | 164 // If there is no mock-http-headers file, fake a 200 OK. |
| 156 raw_headers = "HTTP/1.0 200 OK\n"; | 165 raw_headers = "HTTP/1.0 200 OK\n"; |
| 157 } else { | 166 } else { |
| 158 if (!base::ReadFileToString(header_file, &raw_headers)) | 167 if (!base::ReadFileToString(header_file, &raw_headers)) |
| 159 return; | 168 return; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 180 return info.headers->response_code(); | 189 return info.headers->response_code(); |
| 181 return net::URLRequestJob::GetResponseCode(); | 190 return net::URLRequestJob::GetResponseCode(); |
| 182 } | 191 } |
| 183 | 192 |
| 184 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { | 193 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { |
| 185 net::HttpResponseInfo info; | 194 net::HttpResponseInfo info; |
| 186 GetResponseInfo(&info); | 195 GetResponseInfo(&info); |
| 187 return info.headers.get() && info.headers->GetCharset(charset); | 196 return info.headers.get() && info.headers->GetCharset(charset); |
| 188 } | 197 } |
| 189 | 198 |
| 190 } // namespace content | 199 } // namespace net |
| OLD | NEW |