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/browser/component_updater/component_updater_interceptor.cc | |
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/browser/component_updater/component_updater_interceptor.cc')
-rw-r--r-- | chrome/browser/component_updater/component_updater_interceptor.cc | 59 |
1 files changed, 59 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; +} |