OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/browser/net/url_request_mock_http_job.h" | 5 #include "content/browser/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.h" | 8 #include "base/message_loop.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "content/public/common/url_constants.h" | 12 #include "content/public/common/url_constants.h" |
13 #include "net/base/net_util.h" | 13 #include "net/base/net_util.h" |
14 #include "net/http/http_response_headers.h" | 14 #include "net/http/http_response_headers.h" |
15 #include "net/url_request/url_request_filter.h" | 15 #include "net/url_request/url_request_filter.h" |
16 | 16 |
17 static const char kMockHostname[] = "mock.http"; | 17 const char kMockHostname[] = "mock.http"; |
18 static const FilePath::CharType kMockHeaderFileSuffix[] = | 18 const FilePath::CharType kMockHeaderFileSuffix[] = |
19 FILE_PATH_LITERAL(".mock-http-headers"); | 19 FILE_PATH_LITERAL(".mock-http-headers"); |
20 | 20 |
21 FilePath URLRequestMockHTTPJob::base_path_; | 21 // static |
| 22 FilePath& URLRequestMockHTTPJob::BasePath() { |
| 23 CR_DEFINE_STATIC_LOCAL(FilePath, base_path, ()); |
| 24 return base_path; |
| 25 } |
22 | 26 |
23 // static | 27 // static |
24 net::URLRequestJob* URLRequestMockHTTPJob::Factory(net::URLRequest* request, | 28 net::URLRequestJob* URLRequestMockHTTPJob::Factory(net::URLRequest* request, |
25 const std::string& scheme) { | 29 const std::string& scheme) { |
26 return new URLRequestMockHTTPJob(request, | 30 return new URLRequestMockHTTPJob(request, |
27 GetOnDiskPath(base_path_, request, scheme)); | 31 GetOnDiskPath(BasePath(), request, scheme)); |
28 } | 32 } |
29 | 33 |
30 // static | 34 // static |
31 void URLRequestMockHTTPJob::AddUrlHandler(const FilePath& base_path) { | 35 void URLRequestMockHTTPJob::AddUrlHandler(const FilePath& base_path) { |
32 base_path_ = base_path; | 36 BasePath() = base_path; |
33 | 37 |
34 // Add kMockHostname to net::URLRequestFilter. | 38 // Add kMockHostname to net::URLRequestFilter. |
35 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 39 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
36 filter->AddHostnameHandler("http", kMockHostname, | 40 filter->AddHostnameHandler("http", kMockHostname, |
37 URLRequestMockHTTPJob::Factory); | 41 URLRequestMockHTTPJob::Factory); |
38 } | 42 } |
39 | 43 |
40 /* static */ | 44 // static |
41 GURL URLRequestMockHTTPJob::GetMockUrl(const FilePath& path) { | 45 GURL URLRequestMockHTTPJob::GetMockUrl(const FilePath& path) { |
42 std::string url = "http://"; | 46 std::string url = "http://"; |
43 url.append(kMockHostname); | 47 url.append(kMockHostname); |
44 url.append("/"); | 48 url.append("/"); |
45 std::string path_str = path.MaybeAsASCII(); | 49 std::string path_str = path.MaybeAsASCII(); |
46 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. | 50 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. |
47 url.append(path_str); | 51 url.append(path_str); |
48 return GURL(url); | 52 return GURL(url); |
49 } | 53 } |
50 | 54 |
51 /* static */ | 55 // static |
52 GURL URLRequestMockHTTPJob::GetMockViewSourceUrl(const FilePath& path) { | 56 GURL URLRequestMockHTTPJob::GetMockViewSourceUrl(const FilePath& path) { |
53 std::string url = chrome::kViewSourceScheme; | 57 std::string url = chrome::kViewSourceScheme; |
54 url.append(":"); | 58 url.append(":"); |
55 url.append(GetMockUrl(path).spec()); | 59 url.append(GetMockUrl(path).spec()); |
56 return GURL(url); | 60 return GURL(url); |
57 } | 61 } |
58 | 62 |
59 /* static */ | 63 // static |
60 FilePath URLRequestMockHTTPJob::GetOnDiskPath(const FilePath& base_path, | 64 FilePath URLRequestMockHTTPJob::GetOnDiskPath(const FilePath& base_path, |
61 net::URLRequest* request, | 65 net::URLRequest* request, |
62 const std::string& scheme) { | 66 const std::string& scheme) { |
63 // Conceptually we just want to "return base_path + request->url().path()". | 67 // Conceptually we just want to "return base_path + request->url().path()". |
64 // But path in the request URL is in URL space (i.e. %-encoded spaces). | 68 // But path in the request URL is in URL space (i.e. %-encoded spaces). |
65 // So first we convert base FilePath to a URL, then append the URL | 69 // So first we convert base FilePath to a URL, then append the URL |
66 // path to that, and convert the final URL back to a FilePath. | 70 // path to that, and convert the final URL back to a FilePath. |
67 GURL file_url(net::FilePathToFileURL(base_path)); | 71 GURL file_url(net::FilePathToFileURL(base_path)); |
68 std::string url = file_url.spec() + request->url().path(); | 72 std::string url = file_url.spec() + request->url().path(); |
69 FilePath file_path; | 73 FilePath file_path; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 net::HttpResponseInfo info; | 113 net::HttpResponseInfo info; |
110 GetResponseInfoConst(&info); | 114 GetResponseInfoConst(&info); |
111 return info.headers && info.headers->GetMimeType(mime_type); | 115 return info.headers && info.headers->GetMimeType(mime_type); |
112 } | 116 } |
113 | 117 |
114 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { | 118 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { |
115 net::HttpResponseInfo info; | 119 net::HttpResponseInfo info; |
116 GetResponseInfo(&info); | 120 GetResponseInfo(&info); |
117 return info.headers && info.headers->GetCharset(charset); | 121 return info.headers && info.headers->GetCharset(charset); |
118 } | 122 } |
OLD | NEW |