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