OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 | 4 |
5 #include "content/test/net/url_request_prepackaged_interceptor.h" | 5 #include "content/test/net/url_request_prepackaged_interceptor.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/threading/thread_restrictions.h" | 8 #include "base/threading/thread_restrictions.h" |
9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
10 #include "net/url_request/url_request.h" | 10 #include "net/url_request/url_request.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 virtual ~URLRequestPrepackagedJob() {} | 31 virtual ~URLRequestPrepackagedJob() {} |
32 | 32 |
33 DISALLOW_COPY_AND_ASSIGN(URLRequestPrepackagedJob); | 33 DISALLOW_COPY_AND_ASSIGN(URLRequestPrepackagedJob); |
34 }; | 34 }; |
35 | 35 |
36 } // namespace | 36 } // namespace |
37 | 37 |
38 class URLRequestPrepackagedInterceptor::Delegate | 38 class URLRequestPrepackagedInterceptor::Delegate |
39 : public net::URLRequestJobFactory::ProtocolHandler { | 39 : public net::URLRequestJobFactory::ProtocolHandler { |
40 public: | 40 public: |
41 Delegate() : hit_count_(0) {} | 41 Delegate(const std::string& scheme, const std::string& hostname) |
| 42 : scheme_(scheme), hostname_(hostname), hit_count_(0) {} |
42 virtual ~Delegate() {} | 43 virtual ~Delegate() {} |
43 | 44 |
44 void Register() { | 45 void Register() { |
45 net::URLRequestFilter::GetInstance()->AddHostnameProtocolHandler( | 46 net::URLRequestFilter::GetInstance()->AddHostnameProtocolHandler( |
46 "http", "localhost", | 47 scheme_, hostname_, |
47 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(this)); | 48 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(this)); |
48 } | 49 } |
49 | 50 |
50 static void Unregister() { | 51 static void Unregister( |
51 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler("http", | 52 const std::string& scheme, |
52 "localhost"); | 53 const std::string& hostname) { |
| 54 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme, |
| 55 hostname); |
53 } | 56 } |
54 | 57 |
55 // When requests for |url| arrive, respond with the contents of |path|. The | 58 // When requests for |url| arrive, respond with the contents of |path|. The |
56 // hostname of |url| must be "localhost" to avoid DNS lookups, and the scheme | 59 // hostname and scheme of |url| must match the corresponding parameters |
57 // must be "http". | 60 // passed as constructor arguments. |
58 void SetResponse(const GURL& url, | 61 void SetResponse(const GURL& url, |
59 const base::FilePath& path, | 62 const base::FilePath& path, |
60 bool ignore_query) { | 63 bool ignore_query) { |
61 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 64 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
62 // It's ok to do a blocking disk access on this thread; this class | 65 // It's ok to do a blocking disk access on this thread; this class |
63 // is just used for tests. | 66 // is just used for tests. |
64 base::ThreadRestrictions::ScopedAllowIO allow_io; | 67 base::ThreadRestrictions::ScopedAllowIO allow_io; |
65 EXPECT_TRUE(file_util::PathExists(path)); | 68 EXPECT_TRUE(file_util::PathExists(path)); |
66 if (ignore_query) { | 69 if (ignore_query) { |
67 ignore_query_responses_[url] = path; | 70 ignore_query_responses_[url] = path; |
68 } else { | 71 } else { |
69 responses_[url] = path; | 72 responses_[url] = path; |
70 } | 73 } |
71 } | 74 } |
72 | 75 |
73 // Returns how many requests have been issued that have a stored reply. | 76 // Returns how many requests have been issued that have a stored reply. |
74 int GetHitCount() const { | 77 int GetHitCount() const { |
75 base::AutoLock auto_lock(hit_count_lock_); | 78 base::AutoLock auto_lock(hit_count_lock_); |
76 return hit_count_; | 79 return hit_count_; |
77 } | 80 } |
78 | 81 |
79 private: | 82 private: |
80 typedef std::map<GURL, base::FilePath> ResponseMap; | 83 typedef std::map<GURL, base::FilePath> ResponseMap; |
81 | 84 |
82 // When computing matches, this ignores the query parameters of the url. | 85 // When computing matches, this ignores the query parameters of the url. |
83 virtual net::URLRequestJob* MaybeCreateJob( | 86 virtual net::URLRequestJob* MaybeCreateJob( |
84 net::URLRequest* request, | 87 net::URLRequest* request, |
85 net::NetworkDelegate* network_delegate) const OVERRIDE { | 88 net::NetworkDelegate* network_delegate) const OVERRIDE { |
86 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 89 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
87 if (request->url().scheme() != "http" || | 90 if (request->url().scheme() != scheme_ || |
88 request->url().host() != "localhost") { | 91 request->url().host() != hostname_) { |
89 return NULL; | 92 return NULL; |
90 } | 93 } |
91 | 94 |
92 ResponseMap::const_iterator it = responses_.find(request->url()); | 95 ResponseMap::const_iterator it = responses_.find(request->url()); |
93 if (it == responses_.end()) { | 96 if (it == responses_.end()) { |
94 // Search for this request's url, ignoring any query parameters. | 97 // Search for this request's url, ignoring any query parameters. |
95 GURL url = request->url(); | 98 GURL url = request->url(); |
96 if (url.has_query()) { | 99 if (url.has_query()) { |
97 GURL::Replacements replacements; | 100 GURL::Replacements replacements; |
98 replacements.ClearQuery(); | 101 replacements.ClearQuery(); |
99 url = url.ReplaceComponents(replacements); | 102 url = url.ReplaceComponents(replacements); |
100 } | 103 } |
101 it = ignore_query_responses_.find(url); | 104 it = ignore_query_responses_.find(url); |
102 if (it == ignore_query_responses_.end()) | 105 if (it == ignore_query_responses_.end()) |
103 return NULL; | 106 return NULL; |
104 } | 107 } |
105 { | 108 { |
106 base::AutoLock auto_lock(hit_count_lock_); | 109 base::AutoLock auto_lock(hit_count_lock_); |
107 ++hit_count_; | 110 ++hit_count_; |
108 } | 111 } |
109 | 112 |
110 return new URLRequestPrepackagedJob(request, | 113 return new URLRequestPrepackagedJob(request, |
111 network_delegate, | 114 network_delegate, |
112 it->second); | 115 it->second); |
113 } | 116 } |
114 | 117 |
| 118 const std::string scheme_; |
| 119 const std::string hostname_; |
| 120 |
115 ResponseMap responses_; | 121 ResponseMap responses_; |
116 ResponseMap ignore_query_responses_; | 122 ResponseMap ignore_query_responses_; |
117 | 123 |
118 mutable base::Lock hit_count_lock_; | 124 mutable base::Lock hit_count_lock_; |
119 mutable int hit_count_; | 125 mutable int hit_count_; |
120 | 126 |
121 DISALLOW_COPY_AND_ASSIGN(Delegate); | 127 DISALLOW_COPY_AND_ASSIGN(Delegate); |
122 }; | 128 }; |
123 | 129 |
124 | 130 |
125 URLRequestPrepackagedInterceptor::URLRequestPrepackagedInterceptor() | 131 URLRequestPrepackagedInterceptor::URLRequestPrepackagedInterceptor( |
126 : delegate_(new Delegate) { | 132 const std::string& scheme, |
| 133 const std::string& hostname) |
| 134 : scheme_(scheme), |
| 135 hostname_(hostname), |
| 136 delegate_(new Delegate(scheme, hostname)) { |
127 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 137 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
128 base::Bind(&Delegate::Register, | 138 base::Bind(&Delegate::Register, |
129 base::Unretained(delegate_))); | 139 base::Unretained(delegate_))); |
130 } | 140 } |
131 | 141 |
132 URLRequestPrepackagedInterceptor::~URLRequestPrepackagedInterceptor() { | 142 URLRequestPrepackagedInterceptor::~URLRequestPrepackagedInterceptor() { |
133 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 143 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
134 base::Bind(&Delegate::Unregister)); | 144 base::Bind(&Delegate::Unregister, |
| 145 scheme_, |
| 146 hostname_)); |
135 } | 147 } |
136 | 148 |
137 void URLRequestPrepackagedInterceptor::SetResponse(const GURL& url, | 149 void URLRequestPrepackagedInterceptor::SetResponse( |
138 const base::FilePath& path) { | 150 const GURL& url, |
139 CHECK_EQ("http", url.scheme()); | 151 const base::FilePath& path) { |
140 CHECK_EQ("localhost", url.host()); | 152 CHECK_EQ(scheme_, url.scheme()); |
| 153 CHECK_EQ(hostname_, url.host()); |
141 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 154 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
142 base::Bind(&Delegate::SetResponse, | 155 base::Bind(&Delegate::SetResponse, |
143 base::Unretained(delegate_), url, path, | 156 base::Unretained(delegate_), url, path, |
144 false)); | 157 false)); |
145 } | 158 } |
146 | 159 |
147 void URLRequestPrepackagedInterceptor::SetResponseIgnoreQuery( | 160 void URLRequestPrepackagedInterceptor::SetResponseIgnoreQuery( |
148 const GURL& url, | 161 const GURL& url, |
149 const base::FilePath& path) { | 162 const base::FilePath& path) { |
150 CHECK_EQ("http", url.scheme()); | 163 CHECK_EQ(scheme_, url.scheme()); |
151 CHECK_EQ("localhost", url.host()); | 164 CHECK_EQ(hostname_, url.host()); |
152 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 165 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
153 base::Bind(&Delegate::SetResponse, | 166 base::Bind(&Delegate::SetResponse, |
154 base::Unretained(delegate_), url, path, | 167 base::Unretained(delegate_), url, path, |
155 true)); | 168 true)); |
156 } | 169 } |
157 | 170 |
158 int URLRequestPrepackagedInterceptor::GetHitCount() { | 171 int URLRequestPrepackagedInterceptor::GetHitCount() { |
159 return delegate_->GetHitCount(); | 172 return delegate_->GetHitCount(); |
160 } | 173 } |
161 | 174 |
| 175 |
| 176 URLLocalHostRequestPrepackagedInterceptor |
| 177 ::URLLocalHostRequestPrepackagedInterceptor() |
| 178 : URLRequestPrepackagedInterceptor("http", "localhost") { |
| 179 } |
| 180 |
162 } // namespace content | 181 } // namespace content |
OLD | NEW |