| 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.h" |
| 15 #include "net/url_request/url_request_filter.h" | 16 #include "net/url_request/url_request_filter.h" |
| 16 | 17 |
| 17 const char kMockHostname[] = "mock.http"; | 18 const char kMockHostname[] = "mock.http"; |
| 18 const FilePath::CharType kMockHeaderFileSuffix[] = | 19 const FilePath::CharType kMockHeaderFileSuffix[] = |
| 19 FILE_PATH_LITERAL(".mock-http-headers"); | 20 FILE_PATH_LITERAL(".mock-http-headers"); |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 // This is the file path leading to the root of the directory to use as the | 24 // This is the file path leading to the root of the directory to use as the |
| 24 // root of the http server. This returns a reference that can be assigned to. | 25 // root of the http server. This returns a reference that can be assigned to. |
| 25 FilePath& BasePath() { | 26 FilePath& BasePath() { |
| 26 CR_DEFINE_STATIC_LOCAL(FilePath, base_path, ()); | 27 CR_DEFINE_STATIC_LOCAL(FilePath, base_path, ()); |
| 27 return base_path; | 28 return base_path; |
| 28 } | 29 } |
| 29 | 30 |
| 30 } // namespace | 31 } // namespace |
| 31 | 32 |
| 32 // static | 33 // static |
| 33 net::URLRequestJob* URLRequestMockHTTPJob::Factory(net::URLRequest* request, | 34 net::URLRequestJob* URLRequestMockHTTPJob::Factory( |
| 34 const std::string& scheme) { | 35 net::URLRequest* request, |
| 36 net::NetworkDelegate* network_delegate, |
| 37 const std::string& scheme) { |
| 35 return new URLRequestMockHTTPJob(request, | 38 return new URLRequestMockHTTPJob(request, |
| 39 network_delegate, |
| 36 GetOnDiskPath(BasePath(), request, scheme)); | 40 GetOnDiskPath(BasePath(), request, scheme)); |
| 37 } | 41 } |
| 38 | 42 |
| 39 // static | 43 // static |
| 40 void URLRequestMockHTTPJob::AddUrlHandler(const FilePath& base_path) { | 44 void URLRequestMockHTTPJob::AddUrlHandler(const FilePath& base_path) { |
| 41 BasePath() = base_path; | 45 BasePath() = base_path; |
| 42 | 46 |
| 43 // Add kMockHostname to net::URLRequestFilter. | 47 // Add kMockHostname to net::URLRequestFilter. |
| 44 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 48 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| 45 filter->AddHostnameHandler("http", kMockHostname, | 49 filter->AddHostnameHandler("http", kMockHostname, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 73 // But path in the request URL is in URL space (i.e. %-encoded spaces). | 77 // But path in the request URL is in URL space (i.e. %-encoded spaces). |
| 74 // So first we convert base FilePath to a URL, then append the URL | 78 // So first we convert base FilePath to a URL, then append the URL |
| 75 // path to that, and convert the final URL back to a FilePath. | 79 // path to that, and convert the final URL back to a FilePath. |
| 76 GURL file_url(net::FilePathToFileURL(base_path)); | 80 GURL file_url(net::FilePathToFileURL(base_path)); |
| 77 std::string url = file_url.spec() + request->url().path(); | 81 std::string url = file_url.spec() + request->url().path(); |
| 78 FilePath file_path; | 82 FilePath file_path; |
| 79 net::FileURLToFilePath(GURL(url), &file_path); | 83 net::FileURLToFilePath(GURL(url), &file_path); |
| 80 return file_path; | 84 return file_path; |
| 81 } | 85 } |
| 82 | 86 |
| 83 URLRequestMockHTTPJob::URLRequestMockHTTPJob(net::URLRequest* request, | 87 URLRequestMockHTTPJob::URLRequestMockHTTPJob( |
| 84 const FilePath& file_path) | 88 net::URLRequest* request, |
| 85 : net::URLRequestFileJob(request, file_path) { } | 89 net::NetworkDelegate* network_delegate, |
| 90 const FilePath& file_path) |
| 91 : net::URLRequestFileJob(request, network_delegate, file_path) { } |
| 86 | 92 |
| 87 // Public virtual version. | 93 // Public virtual version. |
| 88 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) { | 94 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) { |
| 89 // Forward to private const version. | 95 // Forward to private const version. |
| 90 GetResponseInfoConst(info); | 96 GetResponseInfoConst(info); |
| 91 } | 97 } |
| 92 | 98 |
| 93 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, | 99 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, |
| 94 int* http_status_code) { | 100 int* http_status_code) { |
| 95 // Override the net::URLRequestFileJob implementation to invoke the default | 101 // Override the net::URLRequestFileJob implementation to invoke the default |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 if (info.headers) | 133 if (info.headers) |
| 128 return info.headers->response_code(); | 134 return info.headers->response_code(); |
| 129 return net::URLRequestJob::GetResponseCode(); | 135 return net::URLRequestJob::GetResponseCode(); |
| 130 } | 136 } |
| 131 | 137 |
| 132 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { | 138 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { |
| 133 net::HttpResponseInfo info; | 139 net::HttpResponseInfo info; |
| 134 GetResponseInfo(&info); | 140 GetResponseInfo(&info); |
| 135 return info.headers && info.headers->GetCharset(charset); | 141 return info.headers && info.headers->GetCharset(charset); |
| 136 } | 142 } |
| OLD | NEW |