| 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" |
| 11 #include "net/http/http_response_headers.h" | 11 #include "net/http/http_response_headers.h" |
| 12 #include "net/url_request/url_request_filter.h" | 12 #include "net/url_request/url_request_filter.h" |
| 13 | 13 |
| 14 static const char kMockHostname[] = "mock.http"; | 14 static const char kMockHostname[] = "mock.http"; |
| 15 static const wchar_t kMockHeaderFileSuffix[] = L".mock-http-headers"; | 15 static const wchar_t kMockHeaderFileSuffix[] = L".mock-http-headers"; |
| 16 | 16 |
| 17 std::wstring URLRequestMockHTTPJob::base_path_ = L""; | 17 std::wstring URLRequestMockHTTPJob::base_path_ = L""; |
| 18 | 18 |
| 19 /* static */ | 19 /* static */ |
| 20 URLRequestJob* URLRequestMockHTTPJob::Factory(URLRequest* request, | 20 URLRequestJob* URLRequestMockHTTPJob::Factory(URLRequest* request, |
| 21 const std::string& scheme) { | 21 const std::string& scheme) { |
| 22 std::wstring file_url(L"file:///"); | 22 return new URLRequestMockHTTPJob(request, |
| 23 file_url += base_path_; | 23 GetOnDiskPath(base_path_, request, scheme)); |
| 24 const std::string& url = request->url().spec(); | |
| 25 // Fix up the url to be the file url we're loading from disk. | |
| 26 std::string host_prefix("http://"); | |
| 27 host_prefix.append(kMockHostname); | |
| 28 size_t host_prefix_len = host_prefix.length(); | |
| 29 if (url.compare(0, host_prefix_len, host_prefix.data(), | |
| 30 host_prefix_len) == 0) { | |
| 31 file_url += UTF8ToWide(url.substr(host_prefix_len)); | |
| 32 } | |
| 33 | |
| 34 // Convert the file:/// URL to a path on disk. | |
| 35 FilePath file_path; | |
| 36 net::FileURLToFilePath(GURL(WideToUTF8(file_url)), &file_path); | |
| 37 return new URLRequestMockHTTPJob(request, file_path); | |
| 38 } | 24 } |
| 39 | 25 |
| 40 /* static */ | 26 /* static */ |
| 41 void URLRequestMockHTTPJob::AddUITestUrls(const std::wstring& base_path) { | 27 void URLRequestMockHTTPJob::AddUITestUrls(const std::wstring& base_path) { |
| 42 base_path_ = base_path; | 28 base_path_ = base_path; |
| 43 | 29 |
| 44 // Add kMockHostname to URLRequestFilter. | 30 // Add kMockHostname to URLRequestFilter. |
| 45 URLRequestFilter* filter = URLRequestFilter::GetInstance(); | 31 URLRequestFilter* filter = URLRequestFilter::GetInstance(); |
| 46 filter->AddHostnameHandler("http", kMockHostname, | 32 filter->AddHostnameHandler("http", kMockHostname, |
| 47 URLRequestMockHTTPJob::Factory); | 33 URLRequestMockHTTPJob::Factory); |
| 48 } | 34 } |
| 49 | 35 |
| 50 /* static */ | 36 /* static */ |
| 51 GURL URLRequestMockHTTPJob::GetMockUrl(const std::wstring& path) { | 37 GURL URLRequestMockHTTPJob::GetMockUrl(const std::wstring& path) { |
| 52 std::string url = "http://"; | 38 std::string url = "http://"; |
| 53 url.append(kMockHostname); | 39 url.append(kMockHostname); |
| 54 url.append("/"); | 40 url.append("/"); |
| 55 url.append(WideToUTF8(path)); | 41 url.append(WideToUTF8(path)); |
| 56 return GURL(url); | 42 return GURL(url); |
| 57 } | 43 } |
| 58 | 44 |
| 45 /* static */ |
| 46 FilePath URLRequestMockHTTPJob::GetOnDiskPath(const std::wstring& base_path, |
| 47 URLRequest* request, |
| 48 const std::string& scheme) { |
| 49 std::wstring file_url(L"file:///"); |
| 50 file_url += base_path; |
| 51 const std::string& url = request->url().spec(); |
| 52 // Fix up the url to be the file url we're loading from disk. |
| 53 std::string host_prefix("http://"); |
| 54 host_prefix.append(kMockHostname); |
| 55 size_t host_prefix_len = host_prefix.length(); |
| 56 if (url.compare(0, host_prefix_len, host_prefix.data(), |
| 57 host_prefix_len) == 0) { |
| 58 file_url += UTF8ToWide(url.substr(host_prefix_len)); |
| 59 } |
| 60 |
| 61 // Convert the file:/// URL to a path on disk. |
| 62 FilePath file_path; |
| 63 net::FileURLToFilePath(GURL(WideToUTF8(file_url)), &file_path); |
| 64 return file_path; |
| 65 } |
| 66 |
| 59 URLRequestMockHTTPJob::URLRequestMockHTTPJob(URLRequest* request, | 67 URLRequestMockHTTPJob::URLRequestMockHTTPJob(URLRequest* request, |
| 60 const FilePath& file_path) | 68 const FilePath& file_path) |
| 61 : URLRequestFileJob(request, file_path) { } | 69 : URLRequestFileJob(request, file_path) { } |
| 62 | 70 |
| 63 // Public virtual version. | 71 // Public virtual version. |
| 64 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) { | 72 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) { |
| 65 // Forward to private const version. | 73 // Forward to private const version. |
| 66 GetResponseInfoConst(info); | 74 GetResponseInfoConst(info); |
| 67 } | 75 } |
| 68 | 76 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 83 net::HttpResponseInfo info; | 91 net::HttpResponseInfo info; |
| 84 GetResponseInfoConst(&info); | 92 GetResponseInfoConst(&info); |
| 85 return info.headers && info.headers->GetMimeType(mime_type); | 93 return info.headers && info.headers->GetMimeType(mime_type); |
| 86 } | 94 } |
| 87 | 95 |
| 88 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { | 96 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { |
| 89 net::HttpResponseInfo info; | 97 net::HttpResponseInfo info; |
| 90 GetResponseInfo(&info); | 98 GetResponseInfo(&info); |
| 91 return info.headers && info.headers->GetCharset(charset); | 99 return info.headers && info.headers->GetCharset(charset); |
| 92 } | 100 } |
| OLD | NEW |