| 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_prepackaged_interceptor.h" | 5 #include "content/test/net/url_request_prepackaged_interceptor.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/threading/thread_restrictions.h" | 8 #include "base/threading/thread_restrictions.h" |
| 9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "net/url_request/url_request.h" | 10 #include "net/url_request/url_request.h" |
| 11 #include "net/url_request/url_request_file_job.h" | 11 #include "net/url_request/url_request_file_job.h" |
| 12 #include "net/url_request/url_request_filter.h" | 12 #include "net/url_request/url_request_filter.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 using content::BrowserThread; | 15 using content::BrowserThread; |
| 16 | 16 |
| 17 namespace content { | 17 namespace content { |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 class URLRequestPrepackagedJob : public net::URLRequestFileJob { | 21 class URLRequestPrepackagedJob : public net::URLRequestFileJob { |
| 22 public: | 22 public: |
| 23 URLRequestPrepackagedJob(net::URLRequest* request, | 23 URLRequestPrepackagedJob(net::URLRequest* request, |
| 24 net::NetworkDelegate* network_delegate, | 24 net::NetworkDelegate* network_delegate, |
| 25 const FilePath& file_path) | 25 const base::FilePath& file_path) |
| 26 : net::URLRequestFileJob(request, network_delegate, file_path) {} | 26 : net::URLRequestFileJob(request, network_delegate, file_path) {} |
| 27 | 27 |
| 28 virtual int GetResponseCode() const { return 200; } | 28 virtual int GetResponseCode() const { return 200; } |
| 29 | 29 |
| 30 private: | 30 private: |
| 31 virtual ~URLRequestPrepackagedJob() {} | 31 virtual ~URLRequestPrepackagedJob() {} |
| 32 | 32 |
| 33 DISALLOW_COPY_AND_ASSIGN(URLRequestPrepackagedJob); | 33 DISALLOW_COPY_AND_ASSIGN(URLRequestPrepackagedJob); |
| 34 }; | 34 }; |
| 35 | 35 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 49 | 49 |
| 50 static void Unregister() { | 50 static void Unregister() { |
| 51 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler("http", | 51 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler("http", |
| 52 "localhost"); | 52 "localhost"); |
| 53 } | 53 } |
| 54 | 54 |
| 55 // When requests for |url| arrive, respond with the contents of |path|. The | 55 // When requests for |url| arrive, respond with the contents of |path|. The |
| 56 // hostname of |url| must be "localhost" to avoid DNS lookups, and the scheme | 56 // hostname of |url| must be "localhost" to avoid DNS lookups, and the scheme |
| 57 // must be "http". | 57 // must be "http". |
| 58 void SetResponse(const GURL& url, | 58 void SetResponse(const GURL& url, |
| 59 const FilePath& path, | 59 const base::FilePath& path, |
| 60 bool ignore_query) { | 60 bool ignore_query) { |
| 61 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 61 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 62 // It's ok to do a blocking disk access on this thread; this class | 62 // It's ok to do a blocking disk access on this thread; this class |
| 63 // is just used for tests. | 63 // is just used for tests. |
| 64 base::ThreadRestrictions::ScopedAllowIO allow_io; | 64 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 65 EXPECT_TRUE(file_util::PathExists(path)); | 65 EXPECT_TRUE(file_util::PathExists(path)); |
| 66 if (ignore_query) { | 66 if (ignore_query) { |
| 67 ignore_query_responses_[url] = path; | 67 ignore_query_responses_[url] = path; |
| 68 } else { | 68 } else { |
| 69 responses_[url] = path; | 69 responses_[url] = path; |
| 70 } | 70 } |
| 71 } | 71 } |
| 72 | 72 |
| 73 // Returns how many requests have been issued that have a stored reply. | 73 // Returns how many requests have been issued that have a stored reply. |
| 74 int GetHitCount() const { | 74 int GetHitCount() const { |
| 75 base::AutoLock auto_lock(hit_count_lock_); | 75 base::AutoLock auto_lock(hit_count_lock_); |
| 76 return hit_count_; | 76 return hit_count_; |
| 77 } | 77 } |
| 78 | 78 |
| 79 private: | 79 private: |
| 80 typedef std::map<GURL, FilePath> ResponseMap; | 80 typedef std::map<GURL, base::FilePath> ResponseMap; |
| 81 | 81 |
| 82 // When computing matches, this ignores the query parameters of the url. | 82 // When computing matches, this ignores the query parameters of the url. |
| 83 virtual net::URLRequestJob* MaybeCreateJob( | 83 virtual net::URLRequestJob* MaybeCreateJob( |
| 84 net::URLRequest* request, | 84 net::URLRequest* request, |
| 85 net::NetworkDelegate* network_delegate) const OVERRIDE { | 85 net::NetworkDelegate* network_delegate) const OVERRIDE { |
| 86 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 86 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 87 if (request->url().scheme() != "http" || | 87 if (request->url().scheme() != "http" || |
| 88 request->url().host() != "localhost") { | 88 request->url().host() != "localhost") { |
| 89 return NULL; | 89 return NULL; |
| 90 } | 90 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 base::Bind(&Delegate::Register, | 128 base::Bind(&Delegate::Register, |
| 129 base::Unretained(delegate_))); | 129 base::Unretained(delegate_))); |
| 130 } | 130 } |
| 131 | 131 |
| 132 URLRequestPrepackagedInterceptor::~URLRequestPrepackagedInterceptor() { | 132 URLRequestPrepackagedInterceptor::~URLRequestPrepackagedInterceptor() { |
| 133 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 133 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 134 base::Bind(&Delegate::Unregister)); | 134 base::Bind(&Delegate::Unregister)); |
| 135 } | 135 } |
| 136 | 136 |
| 137 void URLRequestPrepackagedInterceptor::SetResponse(const GURL& url, | 137 void URLRequestPrepackagedInterceptor::SetResponse(const GURL& url, |
| 138 const FilePath& path) { | 138 const base::FilePath& path) { |
| 139 CHECK_EQ("http", url.scheme()); | 139 CHECK_EQ("http", url.scheme()); |
| 140 CHECK_EQ("localhost", url.host()); | 140 CHECK_EQ("localhost", url.host()); |
| 141 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 141 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 142 base::Bind(&Delegate::SetResponse, | 142 base::Bind(&Delegate::SetResponse, |
| 143 base::Unretained(delegate_), url, path, | 143 base::Unretained(delegate_), url, path, |
| 144 false)); | 144 false)); |
| 145 } | 145 } |
| 146 | 146 |
| 147 void URLRequestPrepackagedInterceptor::SetResponseIgnoreQuery( | 147 void URLRequestPrepackagedInterceptor::SetResponseIgnoreQuery( |
| 148 const GURL& url, | 148 const GURL& url, |
| 149 const FilePath& path) { | 149 const base::FilePath& path) { |
| 150 CHECK_EQ("http", url.scheme()); | 150 CHECK_EQ("http", url.scheme()); |
| 151 CHECK_EQ("localhost", url.host()); | 151 CHECK_EQ("localhost", url.host()); |
| 152 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 152 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 153 base::Bind(&Delegate::SetResponse, | 153 base::Bind(&Delegate::SetResponse, |
| 154 base::Unretained(delegate_), url, path, | 154 base::Unretained(delegate_), url, path, |
| 155 true)); | 155 true)); |
| 156 } | 156 } |
| 157 | 157 |
| 158 int URLRequestPrepackagedInterceptor::GetHitCount() { | 158 int URLRequestPrepackagedInterceptor::GetHitCount() { |
| 159 return delegate_->GetHitCount(); | 159 return delegate_->GetHitCount(); |
| 160 } | 160 } |
| 161 | 161 |
| 162 } // namespace content | 162 } // namespace content |
| OLD | NEW |