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