| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "chrome/browser/automation/url_request_mock_http_job.h" | 5 #include "chrome/browser/automation/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 "net/base/net_util.h" | 10 #include "net/base/net_util.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 host_prefix.append(kMockHostname); | 26 host_prefix.append(kMockHostname); |
| 27 size_t host_prefix_len = host_prefix.length(); | 27 size_t host_prefix_len = host_prefix.length(); |
| 28 if (url.compare(0, host_prefix_len, host_prefix.data(), | 28 if (url.compare(0, host_prefix_len, host_prefix.data(), |
| 29 host_prefix_len) == 0) { | 29 host_prefix_len) == 0) { |
| 30 file_url += UTF8ToWide(url.substr(host_prefix_len)); | 30 file_url += UTF8ToWide(url.substr(host_prefix_len)); |
| 31 } | 31 } |
| 32 | 32 |
| 33 // Convert the file:/// URL to a path on disk. | 33 // Convert the file:/// URL to a path on disk. |
| 34 std::wstring file_path; | 34 std::wstring file_path; |
| 35 net::FileURLToFilePath(GURL(WideToUTF8(file_url)), &file_path); | 35 net::FileURLToFilePath(GURL(WideToUTF8(file_url)), &file_path); |
| 36 URLRequestMockHTTPJob* job = new URLRequestMockHTTPJob(request); | 36 return new URLRequestMockHTTPJob(request, |
| 37 job->file_path_ = FilePath::FromWStringHack(file_path); | 37 FilePath::FromWStringHack(file_path)); |
| 38 return job; | |
| 39 } | 38 } |
| 40 | 39 |
| 41 /* static */ | 40 /* static */ |
| 42 void URLRequestMockHTTPJob::AddUITestUrls(const std::wstring& base_path) { | 41 void URLRequestMockHTTPJob::AddUITestUrls(const std::wstring& base_path) { |
| 43 base_path_ = base_path; | 42 base_path_ = base_path; |
| 44 | 43 |
| 45 // Add kMockHostname to URLRequestFilter. | 44 // Add kMockHostname to URLRequestFilter. |
| 46 URLRequestFilter* filter = URLRequestFilter::GetInstance(); | 45 URLRequestFilter* filter = URLRequestFilter::GetInstance(); |
| 47 filter->AddHostnameHandler("http", kMockHostname, | 46 filter->AddHostnameHandler("http", kMockHostname, |
| 48 URLRequestMockHTTPJob::Factory); | 47 URLRequestMockHTTPJob::Factory); |
| 49 } | 48 } |
| 50 | 49 |
| 51 /* static */ | 50 /* static */ |
| 52 GURL URLRequestMockHTTPJob::GetMockUrl(const std::wstring& path) { | 51 GURL URLRequestMockHTTPJob::GetMockUrl(const std::wstring& path) { |
| 53 std::string url = "http://"; | 52 std::string url = "http://"; |
| 54 url.append(kMockHostname); | 53 url.append(kMockHostname); |
| 55 url.append("/"); | 54 url.append("/"); |
| 56 url.append(WideToUTF8(path)); | 55 url.append(WideToUTF8(path)); |
| 57 return GURL(url); | 56 return GURL(url); |
| 58 } | 57 } |
| 59 | 58 |
| 60 URLRequestMockHTTPJob::URLRequestMockHTTPJob(URLRequest* request) | 59 URLRequestMockHTTPJob::URLRequestMockHTTPJob(URLRequest* request, |
| 61 : URLRequestFileJob(request) { } | 60 const FilePath& file_path) |
| 61 : URLRequestFileJob(request, file_path) { } |
| 62 | 62 |
| 63 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) { | 63 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) { |
| 64 std::wstring header_file = file_path_.ToWStringHack() + kMockHeaderFileSuffix; | 64 std::wstring header_file = file_path_.ToWStringHack() + kMockHeaderFileSuffix; |
| 65 std::string raw_headers; | 65 std::string raw_headers; |
| 66 if (!file_util::ReadFileToString(header_file, &raw_headers)) | 66 if (!file_util::ReadFileToString(header_file, &raw_headers)) |
| 67 return; | 67 return; |
| 68 | 68 |
| 69 // ParseRawHeaders expects \0 to end each header line. | 69 // ParseRawHeaders expects \0 to end each header line. |
| 70 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); | 70 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); |
| 71 info->headers = new net::HttpResponseHeaders(raw_headers); | 71 info->headers = new net::HttpResponseHeaders(raw_headers); |
| 72 } | 72 } |
| 73 | 73 |
| 74 bool URLRequestMockHTTPJob::GetMimeType(std::string* mime_type) { | 74 bool URLRequestMockHTTPJob::GetMimeType(std::string* mime_type) { |
| 75 net::HttpResponseInfo info; | 75 net::HttpResponseInfo info; |
| 76 GetResponseInfo(&info); | 76 GetResponseInfo(&info); |
| 77 return info.headers && info.headers->GetMimeType(mime_type); | 77 return info.headers && info.headers->GetMimeType(mime_type); |
| 78 } | 78 } |
| 79 | 79 |
| 80 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { | 80 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { |
| 81 net::HttpResponseInfo info; | 81 net::HttpResponseInfo info; |
| 82 GetResponseInfo(&info); | 82 GetResponseInfo(&info); |
| 83 return info.headers && info.headers->GetCharset(charset); | 83 return info.headers && info.headers->GetCharset(charset); |
| 84 } | 84 } |
| 85 | 85 |
| OLD | NEW |