Chromium Code Reviews| Index: content/test/net/url_request_prepackaged_interceptor.cc |
| diff --git a/content/test/net/url_request_prepackaged_interceptor.cc b/content/test/net/url_request_prepackaged_interceptor.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4249b4c6ad68869900a0bbbe3b30a6c03fa88601 |
| --- /dev/null |
| +++ b/content/test/net/url_request_prepackaged_interceptor.cc |
| @@ -0,0 +1,141 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| +#include "content/test/net/url_request_prepackaged_interceptor.h" |
| + |
| +#include "base/file_util.h" |
| +#include "base/threading/thread_restrictions.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/test/net/url_request_mock_http_job.h" |
| +#include "net/url_request/url_request.h" |
| +#include "net/url_request/url_request_filter.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace content { |
| + |
| +class URLRequestPrepackagedInterceptor::Delegate |
| + : public net::URLRequestJobFactory::ProtocolHandler { |
| + public: |
| + Delegate() : hit_count_(0) {} |
| + virtual ~Delegate() { |
| + net::URLRequestFilter::GetInstance()->RemoveHostnameHandler("http", |
| + "localhost"); |
| + } |
|
mmenke
2012/12/13 16:12:04
nit: Suggest a line break here.
|
| + void Register() { |
| + net::URLRequestFilter::GetInstance()->AddHostnameProtocolHandler( |
| + "http", "localhost", this); |
| + } |
| + |
| + // When requests for |url| arrive, respond with the contents of |path|. The |
| + // hostname of |url| must be "localhost" to avoid DNS lookups, and the scheme |
| + // must be "http". |
| + void SetResponse(const std::string& url, |
| + const FilePath& path, |
| + bool ignore_query) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + // It's ok to do a blocking disk access on this thread; this class |
| + // is just used for tests. |
| + base::ThreadRestrictions::ScopedAllowIO allow_io; |
|
mmenke
2012/12/13 16:12:04
nit: Suggest doing this just before PathExists, t
|
| + GURL gurl(url); |
| + EXPECT_EQ("http", gurl.scheme()); |
| + EXPECT_EQ("localhost", gurl.host()); |
| + EXPECT_TRUE(file_util::PathExists(path)); |
| + if (ignore_query) { |
| + ignore_query_responses_[gurl] = path; |
| + } else { |
| + responses_[gurl] = path; |
| + } |
| + } |
| + |
| + // Returns how many requests have been issued that have a stored reply. |
| + int GetHitCount() const { |
| + base::AutoLock auto_lock(hit_count_lock_); |
| + return hit_count_; |
| + } |
| + |
| + private: |
| + // When computing matches, this ignores the query parameters of the url. |
| + virtual net::URLRequestJob* MaybeCreateJob( |
| + net::URLRequest* request, |
| + net::NetworkDelegate* network_delegate) const OVERRIDE { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + if (request->url().scheme() != "http" || |
| + request->url().host() != "localhost") { |
| + return NULL; |
| + } |
| + |
| + ResponseMap::const_iterator it = responses_.find(request->url()); |
| + if (it == responses_.end()) { |
| + // Search for this request's url, ignoring any query parameters. |
| + GURL url = request->url(); |
| + if (url.has_query()) { |
| + GURL::Replacements replacements; |
| + replacements.ClearQuery(); |
| + url = url.ReplaceComponents(replacements); |
| + } |
| + it = ignore_query_responses_.find(url); |
| + if (it == ignore_query_responses_.end()) |
| + return NULL; |
| + } |
| + { |
| + base::AutoLock auto_lock(hit_count_lock_); |
| + ++hit_count_; |
| + } |
| + |
| + return new URLRequestMockHTTPJob(request, |
| + network_delegate, |
| + it->second); |
| + } |
| + |
| + typedef std::map<GURL, FilePath> ResponseMap; |
|
mmenke
2012/12/13 16:12:04
nit: Private typedefs should go before private me
|
| + ResponseMap responses_; |
| + ResponseMap ignore_query_responses_; |
| + |
| + mutable base::Lock hit_count_lock_; |
| + mutable int hit_count_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(Delegate); |
| +}; |
| + |
| + |
| +URLRequestPrepackagedInterceptor::URLRequestPrepackagedInterceptor() |
| + : delegate_(new Delegate) { |
| + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| + base::Bind(&Delegate::Register, |
| + base::Unretained(delegate_))); |
| +} |
| + |
| +URLRequestPrepackagedInterceptor::~URLRequestPrepackagedInterceptor() { |
| + BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, delegate_); |
| +} |
| + |
| +void URLRequestPrepackagedInterceptor::SetResponse(const std::string& url, |
| + const FilePath& path) { |
| + GURL gurl(url); |
| + EXPECT_EQ("http", gurl.scheme()); |
| + EXPECT_EQ("localhost", gurl.host()); |
|
mmenke
2012/12/13 16:12:04
Think doing this both here and in Delegate::SetRes
mmenke
2012/12/13 16:12:04
optional: May want to do a CHECK instead of an EX
|
| + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| + base::Bind(&Delegate::SetResponse, |
| + base::Unretained(delegate_), url, path, |
| + false)); |
| +} |
| + |
| +void URLRequestPrepackagedInterceptor::SetResponseIgnoreQuery( |
| + const std::string& url, |
| + const FilePath& path) { |
| + GURL gurl(url); |
| + EXPECT_EQ("http", gurl.scheme()); |
| + EXPECT_EQ("localhost", gurl.host()); |
|
mmenke
2012/12/13 16:12:04
Same comment as above.
|
| + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| + base::Bind(&Delegate::SetResponse, |
| + base::Unretained(delegate_), url, path, |
| + true)); |
| +} |
| + |
| +int URLRequestPrepackagedInterceptor::GetHitCount() { |
| + return delegate_->GetHitCount(); |
| +} |
| + |
| +} // namespace content |