| 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 "content/test/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 const char kMockHostname[] = "mock.http"; | 17 const char kMockHostname[] = "mock.http"; |
| 18 const FilePath::CharType kMockHeaderFileSuffix[] = | 18 const base::FilePath::CharType kMockHeaderFileSuffix[] = |
| 19 FILE_PATH_LITERAL(".mock-http-headers"); | 19 FILE_PATH_LITERAL(".mock-http-headers"); |
| 20 | 20 |
| 21 namespace content { | 21 namespace content { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 class ProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler { | 25 class ProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler { |
| 26 public: | 26 public: |
| 27 // When |map_all_requests_to_base_path| is true, all request should return the | 27 // When |map_all_requests_to_base_path| is true, all request should return the |
| 28 // contents of the file at |base_path|. When |map_all_requests_to_base_path| | 28 // contents of the file at |base_path|. When |map_all_requests_to_base_path| |
| 29 // is false, |base_path| is the file path leading to the root of the directory | 29 // is false, |base_path| is the file path leading to the root of the directory |
| 30 // to use as the root of the HTTP server. | 30 // to use as the root of the HTTP server. |
| 31 explicit ProtocolHandler(const FilePath& base_path, | 31 explicit ProtocolHandler(const base::FilePath& base_path, |
| 32 bool map_all_requests_to_base_path) | 32 bool map_all_requests_to_base_path) |
| 33 : base_path_(base_path), | 33 : base_path_(base_path), |
| 34 map_all_requests_to_base_path_(map_all_requests_to_base_path) {} | 34 map_all_requests_to_base_path_(map_all_requests_to_base_path) {} |
| 35 virtual ~ProtocolHandler() {} | 35 virtual ~ProtocolHandler() {} |
| 36 | 36 |
| 37 // net::URLRequestJobFactory::ProtocolHandler implementation | 37 // net::URLRequestJobFactory::ProtocolHandler implementation |
| 38 virtual net::URLRequestJob* MaybeCreateJob( | 38 virtual net::URLRequestJob* MaybeCreateJob( |
| 39 net::URLRequest* request, | 39 net::URLRequest* request, |
| 40 net::NetworkDelegate* network_delegate) const OVERRIDE { | 40 net::NetworkDelegate* network_delegate) const OVERRIDE { |
| 41 return new URLRequestMockHTTPJob(request, network_delegate, | 41 return new URLRequestMockHTTPJob(request, network_delegate, |
| 42 map_all_requests_to_base_path_ ? base_path_ : GetOnDiskPath(request)); | 42 map_all_requests_to_base_path_ ? base_path_ : GetOnDiskPath(request)); |
| 43 } | 43 } |
| 44 | 44 |
| 45 private: | 45 private: |
| 46 FilePath GetOnDiskPath(net::URLRequest* request) const { | 46 base::FilePath GetOnDiskPath(net::URLRequest* request) const { |
| 47 // Conceptually we just want to "return base_path_ + request->url().path()". | 47 // Conceptually we just want to "return base_path_ + request->url().path()". |
| 48 // But path in the request URL is in URL space (i.e. %-encoded spaces). | 48 // But path in the request URL is in URL space (i.e. %-encoded spaces). |
| 49 // So first we convert base FilePath to a URL, then append the URL | 49 // So first we convert base FilePath to a URL, then append the URL |
| 50 // path to that, and convert the final URL back to a FilePath. | 50 // path to that, and convert the final URL back to a FilePath. |
| 51 GURL file_url(net::FilePathToFileURL(base_path_)); | 51 GURL file_url(net::FilePathToFileURL(base_path_)); |
| 52 std::string url = file_url.spec() + request->url().path(); | 52 std::string url = file_url.spec() + request->url().path(); |
| 53 FilePath file_path; | 53 base::FilePath file_path; |
| 54 net::FileURLToFilePath(GURL(url), &file_path); | 54 net::FileURLToFilePath(GURL(url), &file_path); |
| 55 return file_path; | 55 return file_path; |
| 56 } | 56 } |
| 57 | 57 |
| 58 const FilePath base_path_; | 58 const base::FilePath base_path_; |
| 59 const bool map_all_requests_to_base_path_; | 59 const bool map_all_requests_to_base_path_; |
| 60 | 60 |
| 61 DISALLOW_COPY_AND_ASSIGN(ProtocolHandler); | 61 DISALLOW_COPY_AND_ASSIGN(ProtocolHandler); |
| 62 }; | 62 }; |
| 63 | 63 |
| 64 } // namespace | 64 } // namespace |
| 65 | 65 |
| 66 // static | 66 // static |
| 67 void URLRequestMockHTTPJob::AddUrlHandler(const FilePath& base_path) { | 67 void URLRequestMockHTTPJob::AddUrlHandler(const base::FilePath& base_path) { |
| 68 // Add kMockHostname to net::URLRequestFilter. | 68 // Add kMockHostname to net::URLRequestFilter. |
| 69 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 69 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| 70 filter->AddHostnameProtocolHandler("http", kMockHostname, | 70 filter->AddHostnameProtocolHandler("http", kMockHostname, |
| 71 CreateProtocolHandler(base_path)); | 71 CreateProtocolHandler(base_path)); |
| 72 } | 72 } |
| 73 | 73 |
| 74 // static | 74 // static |
| 75 void URLRequestMockHTTPJob::AddHostnameToFileHandler( | 75 void URLRequestMockHTTPJob::AddHostnameToFileHandler( |
| 76 const std::string& hostname, | 76 const std::string& hostname, |
| 77 const FilePath& file_path) { | 77 const base::FilePath& file_path) { |
| 78 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 78 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| 79 filter->AddHostnameProtocolHandler("http", hostname, | 79 filter->AddHostnameProtocolHandler("http", hostname, |
| 80 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>( | 80 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>( |
| 81 new ProtocolHandler(file_path, true))); | 81 new ProtocolHandler(file_path, true))); |
| 82 | 82 |
| 83 } | 83 } |
| 84 | 84 |
| 85 // static | 85 // static |
| 86 GURL URLRequestMockHTTPJob::GetMockUrl(const FilePath& path) { | 86 GURL URLRequestMockHTTPJob::GetMockUrl(const base::FilePath& path) { |
| 87 std::string url = "http://"; | 87 std::string url = "http://"; |
| 88 url.append(kMockHostname); | 88 url.append(kMockHostname); |
| 89 url.append("/"); | 89 url.append("/"); |
| 90 std::string path_str = path.MaybeAsASCII(); | 90 std::string path_str = path.MaybeAsASCII(); |
| 91 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. | 91 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. |
| 92 url.append(path_str); | 92 url.append(path_str); |
| 93 return GURL(url); | 93 return GURL(url); |
| 94 } | 94 } |
| 95 | 95 |
| 96 // static | 96 // static |
| 97 GURL URLRequestMockHTTPJob::GetMockViewSourceUrl(const FilePath& path) { | 97 GURL URLRequestMockHTTPJob::GetMockViewSourceUrl(const base::FilePath& path) { |
| 98 std::string url = chrome::kViewSourceScheme; | 98 std::string url = chrome::kViewSourceScheme; |
| 99 url.append(":"); | 99 url.append(":"); |
| 100 url.append(GetMockUrl(path).spec()); | 100 url.append(GetMockUrl(path).spec()); |
| 101 return GURL(url); | 101 return GURL(url); |
| 102 } | 102 } |
| 103 | 103 |
| 104 // static | 104 // static |
| 105 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> | 105 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> |
| 106 URLRequestMockHTTPJob::CreateProtocolHandler(const FilePath& base_path) { | 106 URLRequestMockHTTPJob::CreateProtocolHandler(const base::FilePath& base_path) { |
| 107 return scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>( | 107 return scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>( |
| 108 new ProtocolHandler(base_path, false)); | 108 new ProtocolHandler(base_path, false)); |
| 109 } | 109 } |
| 110 | 110 |
| 111 URLRequestMockHTTPJob::URLRequestMockHTTPJob( | 111 URLRequestMockHTTPJob::URLRequestMockHTTPJob( |
| 112 net::URLRequest* request, | 112 net::URLRequest* request, |
| 113 net::NetworkDelegate* network_delegate, | 113 net::NetworkDelegate* network_delegate, |
| 114 const FilePath& file_path) | 114 const base::FilePath& file_path) |
| 115 : net::URLRequestFileJob(request, network_delegate, file_path) { } | 115 : net::URLRequestFileJob(request, network_delegate, file_path) { } |
| 116 | 116 |
| 117 URLRequestMockHTTPJob::~URLRequestMockHTTPJob() { } | 117 URLRequestMockHTTPJob::~URLRequestMockHTTPJob() { } |
| 118 | 118 |
| 119 // Public virtual version. | 119 // Public virtual version. |
| 120 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) { | 120 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) { |
| 121 // Forward to private const version. | 121 // Forward to private const version. |
| 122 GetResponseInfoConst(info); | 122 GetResponseInfoConst(info); |
| 123 } | 123 } |
| 124 | 124 |
| 125 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, | 125 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, |
| 126 int* http_status_code) { | 126 int* http_status_code) { |
| 127 // Override the net::URLRequestFileJob implementation to invoke the default | 127 // Override the net::URLRequestFileJob implementation to invoke the default |
| 128 // one based on HttpResponseInfo. | 128 // one based on HttpResponseInfo. |
| 129 return net::URLRequestJob::IsRedirectResponse(location, http_status_code); | 129 return net::URLRequestJob::IsRedirectResponse(location, http_status_code); |
| 130 } | 130 } |
| 131 | 131 |
| 132 // Private const version. | 132 // Private const version. |
| 133 void URLRequestMockHTTPJob::GetResponseInfoConst( | 133 void URLRequestMockHTTPJob::GetResponseInfoConst( |
| 134 net::HttpResponseInfo* info) const { | 134 net::HttpResponseInfo* info) const { |
| 135 // We have to load our headers from disk, but we only use this class | 135 // We have to load our headers from disk, but we only use this class |
| 136 // from tests, so allow these IO operations to happen on any thread. | 136 // from tests, so allow these IO operations to happen on any thread. |
| 137 base::ThreadRestrictions::ScopedAllowIO allow_io; | 137 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 138 | 138 |
| 139 FilePath header_file = FilePath(file_path_.value() + kMockHeaderFileSuffix); | 139 base::FilePath header_file = |
| 140 base::FilePath(file_path_.value() + kMockHeaderFileSuffix); |
| 140 std::string raw_headers; | 141 std::string raw_headers; |
| 141 if (!file_util::ReadFileToString(header_file, &raw_headers)) | 142 if (!file_util::ReadFileToString(header_file, &raw_headers)) |
| 142 return; | 143 return; |
| 143 | 144 |
| 144 // ParseRawHeaders expects \0 to end each header line. | 145 // ParseRawHeaders expects \0 to end each header line. |
| 145 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); | 146 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); |
| 146 info->headers = new net::HttpResponseHeaders(raw_headers); | 147 info->headers = new net::HttpResponseHeaders(raw_headers); |
| 147 } | 148 } |
| 148 | 149 |
| 149 bool URLRequestMockHTTPJob::GetMimeType(std::string* mime_type) const { | 150 bool URLRequestMockHTTPJob::GetMimeType(std::string* mime_type) const { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 161 return net::URLRequestJob::GetResponseCode(); | 162 return net::URLRequestJob::GetResponseCode(); |
| 162 } | 163 } |
| 163 | 164 |
| 164 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { | 165 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { |
| 165 net::HttpResponseInfo info; | 166 net::HttpResponseInfo info; |
| 166 GetResponseInfo(&info); | 167 GetResponseInfo(&info); |
| 167 return info.headers && info.headers->GetCharset(charset); | 168 return info.headers && info.headers->GetCharset(charset); |
| 168 } | 169 } |
| 169 | 170 |
| 170 } // namespace content | 171 } // namespace content |
| OLD | NEW |