diff options
author | cpu@chromium.org <cpu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-16 04:27:11 +0000 |
---|---|---|
committer | cpu@chromium.org <cpu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-16 04:27:11 +0000 |
commit | f849964ae312b2a47d375e5f942fa10a93624710 (patch) | |
tree | fafa3c45b20c1d8095e2e5efdb41553ec5e8ea48 /chrome | |
parent | a796aa7527ae62528d0b252750d80ec0f0fbee33 (diff) | |
download | chromium_src-f849964ae312b2a47d375e5f942fa10a93624710.zip chromium_src-f849964ae312b2a47d375e5f942fa10a93624710.tar.gz chromium_src-f849964ae312b2a47d375e5f942fa10a93624710.tar.bz2 |
Add an url interceptor to test component updater
BUG=61602
TEST=none
Review URL: http://codereview.chromium.org/7375011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@92795 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/component_updater/component_updater_interceptor.cc | 59 | ||||
-rw-r--r-- | chrome/browser/component_updater/component_updater_interceptor.h | 55 | ||||
-rw-r--r-- | chrome/chrome_tests.gypi | 2 |
3 files changed, 116 insertions, 0 deletions
diff --git a/chrome/browser/component_updater/component_updater_interceptor.cc b/chrome/browser/component_updater/component_updater_interceptor.cc new file mode 100644 index 0000000..244d768 --- /dev/null +++ b/chrome/browser/component_updater/component_updater_interceptor.cc @@ -0,0 +1,59 @@ +// 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 "chrome/browser/component_updater/component_updater_interceptor.h" + +#include "base/file_util.h" +#include "base/threading/thread_restrictions.h" +#include "content/browser/browser_thread.h" +#include "net/url_request/url_request_test_job.h" +#include "testing/gtest/include/gtest/gtest.h" + +ComponentUpdateInterceptor::ComponentUpdateInterceptor() { + net::URLRequest::RegisterRequestInterceptor(this); +} + +ComponentUpdateInterceptor::~ComponentUpdateInterceptor() { + net::URLRequest::UnregisterRequestInterceptor(this); +} + +net::URLRequestJob* ComponentUpdateInterceptor::MaybeIntercept( + net::URLRequest* request) { + 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::iterator it = responses_.find(request->url()); + if (it == responses_.end()) { + return NULL; + } + const Response& response = it->second; + + std::string contents; + EXPECT_TRUE(file_util::ReadFileToString(response.data_path, &contents)); + + return new net::URLRequestTestJob(request, + response.headers, + contents, + true); +} + +void ComponentUpdateInterceptor::SetResponse(const std::string& url, + const std::string& headers, + const FilePath& path) { + // 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)); + Response response = { path, headers }; + responses_[gurl] = response; +} diff --git a/chrome/browser/component_updater/component_updater_interceptor.h b/chrome/browser/component_updater/component_updater_interceptor.h new file mode 100644 index 0000000..d008133 --- /dev/null +++ b/chrome/browser/component_updater/component_updater_interceptor.h @@ -0,0 +1,55 @@ +// 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. + +#ifndef CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UPDATER_INTERCEPTOR_H_ +#define CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UPDATER_INTERCEPTOR_H_ +#pragma once + +#include <map> +#include <string> + +#include "base/compiler_specific.h" +#include "base/file_path.h" +#include "googleurl/src/gurl.h" +#include "net/url_request/url_request.h" + +#if !defined(UNIT_TEST) +#error "use this class only in unit tests" +#endif + +// This url request interceptor lets us respond to localhost http request urls +// with the contents of files on disk for use in tests. +class ComponentUpdateInterceptor + : public net::URLRequest::Interceptor, + public base::RefCountedThreadSafe<ComponentUpdateInterceptor> { + public: + ComponentUpdateInterceptor(); + + // 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 std::string& headers, + const FilePath& path); + + private: + // When computing matches, this ignores the query parameters of the url. + virtual net::URLRequestJob* MaybeIntercept(net::URLRequest* request) OVERRIDE; + + friend class base::RefCountedThreadSafe<ComponentUpdateInterceptor>; + + virtual ~ComponentUpdateInterceptor(); + + struct Response { + FilePath data_path; + std::string headers; + }; + + typedef std::map<GURL, Response> ResponseMap; + ResponseMap responses_; + + DISALLOW_COPY_AND_ASSIGN(ComponentUpdateInterceptor); +}; + +#endif // CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UPDATER_INTERCEPTOR_H_ diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index c1ed27fea..5672239 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1356,6 +1356,8 @@ 'browser/chromeos/system/mock_statistics_provider.h', 'browser/chromeos/version_loader_unittest.cc', 'browser/command_updater_unittest.cc', + 'browser/component_updater/component_updater_interceptor.cc', + 'browser/component_updater/component_updater_interceptor.h', 'browser/content_settings/content_settings_mock_observer.cc', 'browser/content_settings/content_settings_mock_observer.h', 'browser/content_settings/content_settings_mock_provider.cc', |