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

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

Issue 541743002: Move url_request_mock_http_job to net/test/url_request/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed one unmatched brace Created 6 years, 3 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
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 "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/macros.h"
mmenke 2014/09/04 19:02:38 This should be in the header file (After adding DI
xunjieli 2014/09/04 19:24:16 Done. Should have read your comment more carefully
8 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
9 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.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(request, network_delegate,
45 map_all_requests_to_base_path_ ? base_path_ : GetOnDiskPath(request)); 47 map_all_requests_to_base_path_ ? base_path_ : GetOnDiskPath(request),
48 worker_pool_);
46 } 49 }
47 50
48 private: 51 private:
49 base::FilePath GetOnDiskPath(net::URLRequest* request) const { 52 base::FilePath GetOnDiskPath(net::URLRequest* request) const {
50 // Conceptually we just want to "return base_path_ + request->url().path()". 53 // 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). 54 // 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 55 // 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. 56 // path to that, and convert the final URL back to a FilePath.
54 GURL file_url(net::FilePathToFileURL(base_path_)); 57 GURL file_url(net::FilePathToFileURL(base_path_));
55 std::string url = file_url.spec() + request->url().path(); 58 std::string url = file_url.spec() + request->url().path();
56 base::FilePath file_path; 59 base::FilePath file_path;
57 net::FileURLToFilePath(GURL(url), &file_path); 60 net::FileURLToFilePath(GURL(url), &file_path);
58 return file_path; 61 return file_path;
59 } 62 }
60 63
61 const base::FilePath base_path_; 64 const base::FilePath base_path_;
62 const bool map_all_requests_to_base_path_; 65 const bool map_all_requests_to_base_path_;
66 const scoped_refptr<base::SequencedWorkerPool>& worker_pool_;
mmenke 2014/09/04 19:02:38 IMPORTANT: Remove the &. Otherwise, we'll have a
xunjieli 2014/09/04 19:24:16 Done.
63 67
64 DISALLOW_COPY_AND_ASSIGN(MockJobInterceptor); 68 DISALLOW_COPY_AND_ASSIGN(MockJobInterceptor);
65 }; 69 };
66 70
67 } // namespace 71 } // namespace
68 72
69 // static 73 // static
70 void URLRequestMockHTTPJob::AddUrlHandler(const base::FilePath& base_path) { 74 void URLRequestMockHTTPJob::AddUrlHandler(
75 const base::FilePath& base_path,
76 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) {
71 // Add kMockHostname to net::URLRequestFilter. 77 // Add kMockHostname to net::URLRequestFilter.
72 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); 78 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
73 filter->AddHostnameInterceptor( 79 filter->AddHostnameInterceptor(
74 "http", kMockHostname, CreateInterceptor(base_path)); 80 "http", kMockHostname, CreateInterceptor(base_path, worker_pool));
75 } 81 }
76 82
77 // static 83 // static
78 void URLRequestMockHTTPJob::AddHostnameToFileHandler( 84 void URLRequestMockHTTPJob::AddHostnameToFileHandler(
79 const std::string& hostname, 85 const std::string& hostname,
80 const base::FilePath& file) { 86 const base::FilePath& file,
87 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) {
81 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); 88 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
82 filter->AddHostnameInterceptor( 89 filter->AddHostnameInterceptor(
83 "http", hostname, CreateInterceptorForSingleFile(file)); 90 "http", hostname,
91 CreateInterceptorForSingleFile(file, worker_pool));
84 } 92 }
85 93
86 // static 94 // static
87 GURL URLRequestMockHTTPJob::GetMockUrl(const base::FilePath& path) { 95 GURL URLRequestMockHTTPJob::GetMockUrl(const base::FilePath& path) {
88 std::string url = "http://"; 96 std::string url = "http://";
89 url.append(kMockHostname); 97 url.append(kMockHostname);
90 url.append("/"); 98 url.append("/");
91 std::string path_str = path.MaybeAsASCII(); 99 std::string path_str = path.MaybeAsASCII();
92 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. 100 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests.
93 url.append(path_str); 101 url.append(path_str);
94 return GURL(url); 102 return GURL(url);
95 } 103 }
96 104
97 // static 105 // static
98 GURL URLRequestMockHTTPJob::GetMockViewSourceUrl(const base::FilePath& path) {
99 std::string url = kViewSourceScheme;
100 url.append(":");
101 url.append(GetMockUrl(path).spec());
102 return GURL(url);
103 }
104
105 // static
106 scoped_ptr<net::URLRequestInterceptor> 106 scoped_ptr<net::URLRequestInterceptor>
107 URLRequestMockHTTPJob::CreateInterceptor(const base::FilePath& base_path) { 107 URLRequestMockHTTPJob::CreateInterceptor(
108 const base::FilePath& base_path,
109 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) {
108 return scoped_ptr<net::URLRequestInterceptor>( 110 return scoped_ptr<net::URLRequestInterceptor>(
109 new MockJobInterceptor(base_path, false)); 111 new MockJobInterceptor(base_path, false, worker_pool));
110 } 112 }
111 113
112 // static 114 // static
113 scoped_ptr<net::URLRequestInterceptor> 115 scoped_ptr<net::URLRequestInterceptor>
114 URLRequestMockHTTPJob::CreateInterceptorForSingleFile( 116 URLRequestMockHTTPJob::CreateInterceptorForSingleFile(
115 const base::FilePath& file) { 117 const base::FilePath& file,
118 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) {
116 return scoped_ptr<net::URLRequestInterceptor>( 119 return scoped_ptr<net::URLRequestInterceptor>(
117 new MockJobInterceptor(file, true)); 120 new MockJobInterceptor(file, true, worker_pool));
118 } 121 }
119 122
120 URLRequestMockHTTPJob::URLRequestMockHTTPJob( 123 URLRequestMockHTTPJob::URLRequestMockHTTPJob(
121 net::URLRequest* request, net::NetworkDelegate* network_delegate, 124 net::URLRequest* request, net::NetworkDelegate* network_delegate,
122 const base::FilePath& file_path) 125 const base::FilePath& file_path,
126 const scoped_refptr<base::SequencedWorkerPool>& worker_pool)
123 : net::URLRequestFileJob( 127 : net::URLRequestFileJob(
124 request, network_delegate, file_path, 128 request, network_delegate, file_path,
125 content::BrowserThread::GetBlockingPool()-> 129 worker_pool->GetTaskRunnerWithShutdownBehavior(
126 GetTaskRunnerWithShutdownBehavior( 130 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)) {}
127 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)) {}
128 131
129 URLRequestMockHTTPJob::~URLRequestMockHTTPJob() { } 132 URLRequestMockHTTPJob::~URLRequestMockHTTPJob() { }
130 133
131 // Public virtual version. 134 // Public virtual version.
132 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) { 135 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) {
133 // Forward to private const version. 136 // Forward to private const version.
134 GetResponseInfoConst(info); 137 GetResponseInfoConst(info);
135 } 138 }
136 139
137 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, 140 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location,
138 int* http_status_code) { 141 int* http_status_code) {
139 // Override the net::URLRequestFileJob implementation to invoke the default 142 // Override the net::URLRequestFileJob implementation to invoke the default
140 // one based on HttpResponseInfo. 143 // one based on HttpResponseInfo.
141 return net::URLRequestJob::IsRedirectResponse(location, http_status_code); 144 return net::URLRequestJob::IsRedirectResponse(location, http_status_code);
142 } 145 }
143 146
144 // Private const version. 147 // Private const version.
145 void URLRequestMockHTTPJob::GetResponseInfoConst( 148 void URLRequestMockHTTPJob::GetResponseInfoConst(
146 net::HttpResponseInfo* info) const { 149 net::HttpResponseInfo* info) const {
147 // We have to load our headers from disk, but we only use this class 150 // 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. 151 // from tests, so allow these IO operations to happen on any thread.
149 base::ThreadRestrictions::ScopedAllowIO allow_io; 152 base::ThreadRestrictions::SetIOAllowed(true);
150 153
151 base::FilePath header_file = 154 base::FilePath header_file =
152 base::FilePath(file_path_.value() + kMockHeaderFileSuffix); 155 base::FilePath(file_path_.value() + kMockHeaderFileSuffix);
153 std::string raw_headers; 156 std::string raw_headers;
154 if (!base::PathExists(header_file)) { 157 if (!base::PathExists(header_file)) {
155 // If there is no mock-http-headers file, fake a 200 OK. 158 // If there is no mock-http-headers file, fake a 200 OK.
156 raw_headers = "HTTP/1.0 200 OK\n"; 159 raw_headers = "HTTP/1.0 200 OK\n";
157 } else { 160 } else {
158 if (!base::ReadFileToString(header_file, &raw_headers)) 161 if (!base::ReadFileToString(header_file, &raw_headers))
159 return; 162 return;
(...skipping 20 matching lines...) Expand all
180 return info.headers->response_code(); 183 return info.headers->response_code();
181 return net::URLRequestJob::GetResponseCode(); 184 return net::URLRequestJob::GetResponseCode();
182 } 185 }
183 186
184 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { 187 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) {
185 net::HttpResponseInfo info; 188 net::HttpResponseInfo info;
186 GetResponseInfo(&info); 189 GetResponseInfo(&info);
187 return info.headers.get() && info.headers->GetCharset(charset); 190 return info.headers.get() && info.headers->GetCharset(charset);
188 } 191 }
189 192
190 } // namespace content 193 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698