| 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..c62521ad99a3d6bfb6c11f3ff5304f80996bfd73
|
| --- /dev/null
|
| +++ b/content/test/net/url_request_prepackaged_interceptor.cc
|
| @@ -0,0 +1,144 @@
|
| +// 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 "net/url_request/url_request.h"
|
| +#include "net/url_request/url_request_filter.h"
|
| +#include "net/url_request/url_request_test_job.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");
|
| + }
|
| + 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 queryless) {
|
| + // It's ok to do a blocking disk access on this thread; this class
|
| + // is just used for tests.
|
| + base::ThreadRestrictions::ScopedAllowIO allow_io;
|
| + GURL gurl(url);
|
| + EXPECT_EQ("http", gurl.scheme());
|
| + EXPECT_EQ("localhost", gurl.host());
|
| + EXPECT_TRUE(file_util::PathExists(path));
|
| + if (queryless) {
|
| + queryless_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 {
|
| + EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
| + if (request->url().scheme() != "http" ||
|
| + request->url().host() != "localhost") {
|
| + return NULL;
|
| + }
|
| +
|
| + // It's ok to do a blocking disk access on this thread; this class
|
| + // is just used for tests.
|
| + base::ThreadRestrictions::ScopedAllowIO allow_io;
|
| +
|
| + 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 = queryless_responses_.find(url);
|
| + if (it == queryless_responses_.end())
|
| + return NULL;
|
| + }
|
| + const FilePath& response = it->second;
|
| + {
|
| + base::AutoLock auto_lock(hit_count_lock_);
|
| + ++hit_count_;
|
| + }
|
| +
|
| + std::string contents;
|
| + EXPECT_TRUE(file_util::ReadFileToString(response, &contents));
|
| +
|
| + return new net::URLRequestTestJob(request,
|
| + network_delegate,
|
| + net::URLRequestTestJob::test_headers(),
|
| + contents,
|
| + true);
|
| + }
|
| +
|
| + typedef std::map<GURL, FilePath> ResponseMap;
|
| + ResponseMap responses_;
|
| + ResponseMap queryless_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) {
|
| + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
| + base::Bind(&Delegate::SetResponse,
|
| + base::Unretained(delegate_), url, path,
|
| + false));
|
| +}
|
| +
|
| +void URLRequestPrepackagedInterceptor::SetQuerylessResponse(
|
| + const std::string& url,
|
| + const FilePath& path) {
|
| + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
| + base::Bind(&Delegate::SetResponse,
|
| + base::Unretained(delegate_), url, path,
|
| + true));
|
| +}
|
| +
|
| +int URLRequestPrepackagedInterceptor::GetHitCount() {
|
| + return delegate_->GetHitCount();
|
| +}
|
| +
|
| +} // namespace content
|
|
|