Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Unified Diff: content/test/net/url_request_prepackaged_interceptor.cc

Issue 11293252: Change Interceptors into URLRequestJobFactory::ProtocolHandlers. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sync (including tedv's change) Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..dfafea30404074937c57d8ca05fa45a31537dcbd
--- /dev/null
+++ b/content/test/net/url_request_prepackaged_interceptor.cc
@@ -0,0 +1,157 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
mmenke 2012/12/18 20:32:15 Wrong year.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
mmenke 2012/12/18 20:32:15 nit: Add linebreak.
+#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_file_job.h"
+#include "net/url_request/url_request_filter.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using content::BrowserThread;
+
+namespace content {
+
+namespace {
+
+class URLRequestPrepackagedJob : public net::URLRequestFileJob {
+ public:
+ URLRequestPrepackagedJob(net::URLRequest* request,
+ net::NetworkDelegate* network_delegate,
+ const FilePath& file_path)
+ : net::URLRequestFileJob(request, network_delegate, file_path) {}
+
+ virtual int GetResponseCode() const { return 200; }
+
+ private:
+ virtual ~URLRequestPrepackagedJob() {}
+
+ DISALLOW_COPY_AND_ASSIGN(URLRequestPrepackagedJob);
+};
+
+} // namespace
+
+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 GURL& 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;
+ EXPECT_TRUE(file_util::PathExists(path));
+ if (ignore_query) {
+ ignore_query_responses_[url] = path;
+ } else {
+ responses_[url] = 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:
+ typedef std::map<GURL, FilePath> ResponseMap;
+
+ // 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));
mmenke 2012/12/18 20:32:15 Since this is test code only, this DCHECK and the
+ 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 URLRequestPrepackagedJob(request,
+ network_delegate,
+ it->second);
+ }
+
+ 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 GURL& url,
+ const FilePath& path) {
+ CHECK_EQ("http", url.scheme());
+ CHECK_EQ("localhost", url.host());
+ BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
+ base::Bind(&Delegate::SetResponse,
+ base::Unretained(delegate_), url, path,
+ false));
+}
+
+void URLRequestPrepackagedInterceptor::SetResponseIgnoreQuery(
+ const GURL& url,
+ const FilePath& path) {
+ CHECK_EQ("http", url.scheme());
+ CHECK_EQ("localhost", url.host());
+ BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
+ base::Bind(&Delegate::SetResponse,
+ base::Unretained(delegate_), url, path,
+ true));
+}
+
+int URLRequestPrepackagedInterceptor::GetHitCount() {
+ return delegate_->GetHitCount();
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698