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 "net/test/url_request/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/files/file_util.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
89 if (!base::PathExists(header_file)) { | 89 if (!base::PathExists(header_file)) { |
90 // If there is no mock-http-headers file, fake a 200 OK. | 90 // If there is no mock-http-headers file, fake a 200 OK. |
91 return "HTTP/1.0 200 OK\n"; | 91 return "HTTP/1.0 200 OK\n"; |
92 } | 92 } |
93 | 93 |
94 std::string raw_headers; | 94 std::string raw_headers; |
95 base::ReadFileToString(header_file, &raw_headers); | 95 base::ReadFileToString(header_file, &raw_headers); |
96 return raw_headers; | 96 return raw_headers; |
97 } | 97 } |
98 | 98 |
99 // For a given file |path| and |scheme|, return the URL served by the | |
100 // URlRequestMockHTTPJob. | |
101 GURL GetMockUrlForScheme(const base::FilePath& path, | |
102 const std::string& scheme) { | |
103 std::string url = scheme + "://"; | |
104 url.append(kMockHostname); | |
105 url.append("/"); | |
mmenke
2015/01/22 20:53:51
optional: May just want to merge these into one l
Mathieu
2015/01/22 21:44:22
Done.
| |
106 std::string path_str = path.MaybeAsASCII(); | |
107 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. | |
108 url.append(path_str); | |
109 return GURL(url); | |
110 } | |
111 | |
99 } // namespace | 112 } // namespace |
100 | 113 |
101 // static | 114 // static |
102 void URLRequestMockHTTPJob::AddUrlHandler( | 115 void URLRequestMockHTTPJob::AddUrlHandlers( |
103 const base::FilePath& base_path, | 116 const base::FilePath& base_path, |
104 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { | 117 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { |
105 // Add kMockHostname to net::URLRequestFilter. | 118 // Add kMockHostname to net::URLRequestFilter, for both HTTP and HTTPS. |
106 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 119 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
107 filter->AddHostnameInterceptor( | 120 filter->AddHostnameInterceptor( |
108 "http", kMockHostname, CreateInterceptor(base_path, worker_pool)); | 121 "http", kMockHostname, CreateInterceptor(base_path, worker_pool)); |
122 filter->AddHostnameInterceptor("https", kMockHostname, | |
123 CreateInterceptor(base_path, worker_pool)); | |
109 } | 124 } |
110 | 125 |
111 // static | 126 // static |
112 void URLRequestMockHTTPJob::AddHostnameToFileHandler( | 127 void URLRequestMockHTTPJob::AddHostnameToFileHandler( |
113 const std::string& hostname, | 128 const std::string& hostname, |
114 const base::FilePath& file, | 129 const base::FilePath& file, |
115 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { | 130 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { |
116 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 131 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
117 filter->AddHostnameInterceptor( | 132 filter->AddHostnameInterceptor( |
118 "http", hostname, CreateInterceptorForSingleFile(file, worker_pool)); | 133 "http", hostname, CreateInterceptorForSingleFile(file, worker_pool)); |
119 } | 134 } |
120 | 135 |
121 // static | 136 // static |
122 GURL URLRequestMockHTTPJob::GetMockUrl(const base::FilePath& path) { | 137 GURL URLRequestMockHTTPJob::GetMockUrl(const base::FilePath& path) { |
123 std::string url = "http://"; | 138 return GetMockUrlForScheme(path, "http"); |
124 url.append(kMockHostname); | |
125 url.append("/"); | |
126 std::string path_str = path.MaybeAsASCII(); | |
127 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. | |
128 url.append(path_str); | |
129 return GURL(url); | |
130 } | 139 } |
131 | 140 |
132 // static | 141 // static |
142 GURL URLRequestMockHTTPJob::GetMockHttpsUrl(const base::FilePath& path) { | |
143 return GetMockUrlForScheme(path, "https"); | |
144 } | |
145 | |
146 // static | |
133 GURL URLRequestMockHTTPJob::GetMockUrlWithFailure(const base::FilePath& path, | 147 GURL URLRequestMockHTTPJob::GetMockUrlWithFailure(const base::FilePath& path, |
134 FailurePhase phase, | 148 FailurePhase phase, |
135 int net_error) { | 149 int net_error) { |
136 static_assert(arraysize(kFailurePhase) == MAX_FAILURE_PHASE, | 150 static_assert(arraysize(kFailurePhase) == MAX_FAILURE_PHASE, |
137 "kFailurePhase must match FailurePhase enum"); | 151 "kFailurePhase must match FailurePhase enum"); |
138 DCHECK_GE(phase, START); | 152 DCHECK_GE(phase, START); |
139 DCHECK_LE(phase, READ_SYNC); | 153 DCHECK_LE(phase, READ_SYNC); |
140 std::string url(GetMockUrl(path).spec()); | 154 std::string url(GetMockUrl(path).spec()); |
141 url.append("?"); | 155 url.append("?"); |
142 url.append(kFailurePhase[phase]); | 156 url.append(kFailurePhase[phase]); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
277 return net::URLRequestJob::GetResponseCode(); | 291 return net::URLRequestJob::GetResponseCode(); |
278 } | 292 } |
279 | 293 |
280 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { | 294 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { |
281 net::HttpResponseInfo info; | 295 net::HttpResponseInfo info; |
282 GetResponseInfo(&info); | 296 GetResponseInfo(&info); |
283 return info.headers.get() && info.headers->GetCharset(charset); | 297 return info.headers.get() && info.headers->GetCharset(charset); |
284 } | 298 } |
285 | 299 |
286 } // namespace net | 300 } // namespace net |
OLD | NEW |