Chromium Code Reviews| 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/common/url_constants.h" | 12 #include "content/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 static const char kMockHostname[] = "mock.http"; |
| 18 static const char kMockHostnameOfTempDir[] = "mock.temp.http"; | |
| 18 static const FilePath::CharType kMockHeaderFileSuffix[] = | 19 static const FilePath::CharType kMockHeaderFileSuffix[] = |
| 19 FILE_PATH_LITERAL(".mock-http-headers"); | 20 FILE_PATH_LITERAL(".mock-http-headers"); |
| 20 | 21 |
| 21 FilePath URLRequestMockHTTPJob::base_path_; | 22 FilePath URLRequestMockHTTPJob::test_dir_; |
| 23 FilePath URLRequestMockHTTPJob::temp_dir_; | |
| 22 | 24 |
| 23 // static | 25 // static |
| 24 net::URLRequestJob* URLRequestMockHTTPJob::Factory(net::URLRequest* request, | 26 net::URLRequestJob* URLRequestMockHTTPJob::Factory(net::URLRequest* request, |
| 25 const std::string& scheme) { | 27 const std::string& scheme) { |
| 26 return new URLRequestMockHTTPJob(request, | 28 return new URLRequestMockHTTPJob(request, |
| 27 GetOnDiskPath(base_path_, request, scheme)); | 29 GetOnDiskPath(test_dir_, request, scheme)); |
| 28 } | 30 } |
| 29 | 31 |
| 30 // static | 32 // static |
| 31 void URLRequestMockHTTPJob::AddUrlHandler(const FilePath& base_path) { | 33 net::URLRequestJob* URLRequestMockHTTPJob::FactoryForMockUrlOfTempDir( |
| 32 base_path_ = base_path; | 34 net::URLRequest* request, const std::string& scheme) { |
| 35 return new URLRequestMockHTTPJob(request, | |
| 36 GetOnDiskPath(temp_dir_, request, scheme)); | |
| 37 } | |
| 38 | |
| 39 // static | |
| 40 void URLRequestMockHTTPJob::AddUrlHandler(const FilePath& test_dir) { | |
| 41 test_dir_ = test_dir; | |
| 33 | 42 |
| 34 // Add kMockHostname to net::URLRequestFilter. | 43 // Add kMockHostname to net::URLRequestFilter. |
| 35 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 44 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| 36 filter->AddHostnameHandler("http", kMockHostname, | 45 filter->AddHostnameHandler("http", kMockHostname, |
| 37 URLRequestMockHTTPJob::Factory); | 46 URLRequestMockHTTPJob::Factory); |
| 38 } | 47 } |
| 39 | 48 |
| 49 // static | |
| 50 void URLRequestMockHTTPJob::AddUrlOfTempDirHandler(const FilePath& temp_dir) { | |
| 51 temp_dir_ = temp_dir; | |
| 52 | |
| 53 // Add kMockHostnameOfTempDir to net::URLRequestFilter. | |
| 54 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | |
| 55 filter->AddHostnameHandler("http", kMockHostnameOfTempDir, | |
| 56 URLRequestMockHTTPJob::FactoryForMockUrlOfTempDir); | |
| 57 } | |
| 58 | |
| 40 /* static */ | 59 /* static */ |
| 41 GURL URLRequestMockHTTPJob::GetMockUrl(const FilePath& path) { | 60 GURL URLRequestMockHTTPJob::GetMockUrl(const FilePath& path) { |
| 42 std::string url = "http://"; | 61 std::string url = "http://"; |
| 43 url.append(kMockHostname); | 62 url.append(kMockHostname); |
| 44 url.append("/"); | 63 url.append("/"); |
| 45 std::string path_str = path.MaybeAsASCII(); | 64 std::string path_str = path.MaybeAsASCII(); |
| 46 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. | 65 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. |
| 47 url.append(path_str); | 66 url.append(path_str); |
| 48 return GURL(url); | 67 return GURL(url); |
| 49 } | 68 } |
| 50 | 69 |
| 51 /* static */ | 70 /* static */ |
| 71 GURL URLRequestMockHTTPJob::GetMockUrlOfTempDir(const FilePath& path) { | |
| 72 std::string url = "http://"; | |
| 73 url.append(kMockHostnameOfTempDir); | |
| 74 url.append("/"); | |
|
eroman
2011/08/04 20:09:44
note you can also use GURL::ReplaceComponents() to
haraken1
2011/08/08 06:18:53
Thanks, but now this routine is moved into browser
| |
| 75 std::string path_str = path.MaybeAsASCII(); | |
| 76 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. | |
| 77 url.append(path_str); | |
| 78 return GURL(url); | |
| 79 } | |
| 80 | |
| 81 /* static */ | |
| 52 GURL URLRequestMockHTTPJob::GetMockViewSourceUrl(const FilePath& path) { | 82 GURL URLRequestMockHTTPJob::GetMockViewSourceUrl(const FilePath& path) { |
| 53 std::string url = chrome::kViewSourceScheme; | 83 std::string url = chrome::kViewSourceScheme; |
| 54 url.append(":"); | 84 url.append(":"); |
| 55 url.append(GetMockUrl(path).spec()); | 85 url.append(GetMockUrl(path).spec()); |
| 56 return GURL(url); | 86 return GURL(url); |
| 57 } | 87 } |
| 58 | 88 |
| 59 /* static */ | 89 /* static */ |
| 60 FilePath URLRequestMockHTTPJob::GetOnDiskPath(const FilePath& base_path, | 90 FilePath URLRequestMockHTTPJob::GetOnDiskPath(const FilePath& base_path, |
| 61 net::URLRequest* request, | 91 net::URLRequest* request, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 109 net::HttpResponseInfo info; | 139 net::HttpResponseInfo info; |
| 110 GetResponseInfoConst(&info); | 140 GetResponseInfoConst(&info); |
| 111 return info.headers && info.headers->GetMimeType(mime_type); | 141 return info.headers && info.headers->GetMimeType(mime_type); |
| 112 } | 142 } |
| 113 | 143 |
| 114 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { | 144 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { |
| 115 net::HttpResponseInfo info; | 145 net::HttpResponseInfo info; |
| 116 GetResponseInfo(&info); | 146 GetResponseInfo(&info); |
| 117 return info.headers && info.headers->GetCharset(charset); | 147 return info.headers && info.headers->GetCharset(charset); |
| 118 } | 148 } |
| OLD | NEW |