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/browser/browser_thread.h" | 8 #include "content/browser/browser_thread.h" |
9 #include "net/url_request/url_request_test_job.h" | 9 #include "net/url_request/url_request_test_job.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 ComponentUpdateInterceptor::ComponentUpdateInterceptor() { | 12 ComponentUpdateInterceptor::ComponentUpdateInterceptor() |
| 13 : hit_count_(0) { |
13 net::URLRequest::Deprecated::RegisterRequestInterceptor(this); | 14 net::URLRequest::Deprecated::RegisterRequestInterceptor(this); |
14 } | 15 } |
15 | 16 |
16 ComponentUpdateInterceptor::~ComponentUpdateInterceptor() { | 17 ComponentUpdateInterceptor::~ComponentUpdateInterceptor() { |
17 net::URLRequest::Deprecated::UnregisterRequestInterceptor(this); | 18 net::URLRequest::Deprecated::UnregisterRequestInterceptor(this); |
18 } | 19 } |
19 | 20 |
20 net::URLRequestJob* ComponentUpdateInterceptor::MaybeIntercept( | 21 net::URLRequestJob* ComponentUpdateInterceptor::MaybeIntercept( |
21 net::URLRequest* request) { | 22 net::URLRequest* request) { |
22 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 23 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
23 if (request->url().scheme() != "http" || | 24 if (request->url().scheme() != "http" || |
24 request->url().host() != "localhost") { | 25 request->url().host() != "localhost") { |
25 return NULL; | 26 return NULL; |
26 } | 27 } |
27 | 28 |
28 // It's ok to do a blocking disk access on this thread; this class | 29 // It's ok to do a blocking disk access on this thread; this class |
29 // is just used for tests. | 30 // is just used for tests. |
30 base::ThreadRestrictions::ScopedAllowIO allow_io; | 31 base::ThreadRestrictions::ScopedAllowIO allow_io; |
31 | 32 |
32 ResponseMap::iterator it = responses_.find(request->url()); | 33 ResponseMap::iterator it = responses_.find(request->url()); |
33 if (it == responses_.end()) { | 34 if (it == responses_.end()) { |
34 return NULL; | 35 return NULL; |
35 } | 36 } |
36 const Response& response = it->second; | 37 const Response& response = it->second; |
| 38 ++hit_count_; |
37 | 39 |
38 std::string contents; | 40 std::string contents; |
39 EXPECT_TRUE(file_util::ReadFileToString(response.data_path, &contents)); | 41 EXPECT_TRUE(file_util::ReadFileToString(response.data_path, &contents)); |
40 | 42 |
41 return new net::URLRequestTestJob(request, | 43 return new net::URLRequestTestJob(request, |
42 response.headers, | 44 response.headers, |
43 contents, | 45 contents, |
44 true); | 46 true); |
45 } | 47 } |
46 | 48 |
47 void ComponentUpdateInterceptor::SetResponse(const std::string& url, | 49 void ComponentUpdateInterceptor::SetResponse(const std::string& url, |
48 const std::string& headers, | 50 const std::string& headers, |
49 const FilePath& path) { | 51 const FilePath& path) { |
50 // It's ok to do a blocking disk access on this thread; this class | 52 // It's ok to do a blocking disk access on this thread; this class |
51 // is just used for tests. | 53 // is just used for tests. |
52 base::ThreadRestrictions::ScopedAllowIO allow_io; | 54 base::ThreadRestrictions::ScopedAllowIO allow_io; |
53 GURL gurl(url); | 55 GURL gurl(url); |
54 EXPECT_EQ("http", gurl.scheme()); | 56 EXPECT_EQ("http", gurl.scheme()); |
55 EXPECT_EQ("localhost", gurl.host()); | 57 EXPECT_EQ("localhost", gurl.host()); |
56 EXPECT_TRUE(file_util::PathExists(path)); | 58 EXPECT_TRUE(file_util::PathExists(path)); |
57 Response response = { path, headers }; | 59 Response response = { path, headers }; |
58 responses_[gurl] = response; | 60 responses_[gurl] = response; |
59 } | 61 } |
OLD | NEW |