| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 #include "chrome/browser/component_updater/component_updater_interceptor.h" | |
| 5 | |
| 6 #include "base/file_util.h" | |
| 7 #include "base/threading/thread_restrictions.h" | |
| 8 #include "content/public/browser/browser_thread.h" | |
| 9 #include "net/url_request/url_request.h" | |
| 10 #include "net/url_request/url_request_test_job.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 | |
| 13 using content::BrowserThread; | |
| 14 | |
| 15 ComponentUpdateInterceptor::ComponentUpdateInterceptor() | |
| 16 : hit_count_(0) { | |
| 17 net::URLRequest::Deprecated::RegisterRequestInterceptor(this); | |
| 18 } | |
| 19 | |
| 20 ComponentUpdateInterceptor::~ComponentUpdateInterceptor() { | |
| 21 net::URLRequest::Deprecated::UnregisterRequestInterceptor(this); | |
| 22 } | |
| 23 | |
| 24 net::URLRequestJob* ComponentUpdateInterceptor::MaybeIntercept( | |
| 25 net::URLRequest* request, net::NetworkDelegate* network_delegate) { | |
| 26 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 27 if (request->url().scheme() != "http" || | |
| 28 request->url().host() != "localhost") { | |
| 29 return NULL; | |
| 30 } | |
| 31 | |
| 32 // It's ok to do a blocking disk access on this thread; this class | |
| 33 // is just used for tests. | |
| 34 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
| 35 | |
| 36 ResponseMap::iterator it = responses_.find(request->url()); | |
| 37 if (it == responses_.end()) { | |
| 38 return NULL; | |
| 39 } | |
| 40 const Response& response = it->second; | |
| 41 ++hit_count_; | |
| 42 | |
| 43 std::string contents; | |
| 44 EXPECT_TRUE(file_util::ReadFileToString(response.data_path, &contents)); | |
| 45 | |
| 46 return new net::URLRequestTestJob(request, | |
| 47 network_delegate, | |
| 48 response.headers, | |
| 49 contents, | |
| 50 true); | |
| 51 } | |
| 52 | |
| 53 void ComponentUpdateInterceptor::SetResponse(const std::string& url, | |
| 54 const std::string& headers, | |
| 55 const FilePath& path) { | |
| 56 // It's ok to do a blocking disk access on this thread; this class | |
| 57 // is just used for tests. | |
| 58 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
| 59 GURL gurl(url); | |
| 60 EXPECT_EQ("http", gurl.scheme()); | |
| 61 EXPECT_EQ("localhost", gurl.host()); | |
| 62 EXPECT_TRUE(file_util::PathExists(path)); | |
| 63 Response response = { path, headers }; | |
| 64 responses_[gurl] = response; | |
| 65 } | |
| OLD | NEW |