Page Speed Optimization Libraries  1.9.32.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
proxy_interface_test_base.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef NET_INSTAWEB_AUTOMATIC_PUBLIC_PROXY_INTERFACE_TEST_BASE_H_
20 #define NET_INSTAWEB_AUTOMATIC_PUBLIC_PROXY_INTERFACE_TEST_BASE_H_
21 
23 #include "net/instaweb/htmlparse/public/empty_html_filter.h"
25 #include "net/instaweb/http/public/response_headers.h"
34 #include "net/instaweb/util/worker_test_base.h"
35 
36 namespace net_instaweb {
37 
38 class MockCriticalImagesFinder;
39 class GoogleUrl;
40 class HtmlElement;
41 class HtmlFilter;
42 class MessageHandler;
43 class PropertyValue;
44 class RequestHeaders;
45 class RewriteDriver;
46 class RewriteOptions;
47 
48 const char kPageUrl[] = "page.html";
49 const char kBackgroundFetchHeader[] = "X-Background-Fetch";
50 
53 class ProxyUrlNamer : public UrlNamer {
54  public:
55  static const char kProxyHost[];
56 
57  ProxyUrlNamer() : authorized_(true) {}
58 
60  virtual bool Decode(const GoogleUrl& gurl,
61  const RewriteOptions* rewrite_options,
62  GoogleUrl* domain,
63  GoogleString* decoded) const;
64 
65  virtual bool IsAuthorized(const GoogleUrl& gurl,
66  const RewriteOptions& options) const {
67  return authorized_;
68  }
69 
70  void set_authorized(bool authorized) { authorized_ = authorized; }
71 
72  private:
73  bool authorized_;
74  DISALLOW_COPY_AND_ASSIGN(ProxyUrlNamer);
75 };
76 
83 class MockFilter : public EmptyHtmlFilter {
84  public:
85  explicit MockFilter(RewriteDriver* driver)
86  : driver_(driver),
87  num_elements_(0),
88  num_elements_property_(NULL) {
89  }
90 
91  virtual void StartDocument();
92 
93  virtual void StartElement(HtmlElement* element);
94 
95  virtual void EndDocument();
96 
97  virtual const char* Name() const { return "MockFilter"; }
98 
99  private:
100  RewriteDriver* driver_;
101  int num_elements_;
102  PropertyValue* num_elements_property_;
103  DISALLOW_COPY_AND_ASSIGN(MockFilter);
104 };
105 
110  public:
112  virtual ~CreateFilterCallback() {}
113 
114  virtual HtmlFilter* Done(RewriteDriver* driver) {
115  return new MockFilter(driver);
116  }
117 
118  private:
119  DISALLOW_COPY_AND_ASSIGN(CreateFilterCallback);
120 };
121 
125  public:
126  explicit BackgroundFetchCheckingAsyncFetch(AsyncFetch* base_fetch)
127  : SharedAsyncFetch(base_fetch),
128  async_fetch_(base_fetch) {}
130 
131  virtual void HandleHeadersComplete() {
132  SharedAsyncFetch::HandleHeadersComplete();
133  response_headers()->Add(kBackgroundFetchHeader,
134  async_fetch_->IsBackgroundFetch() ? "1" : "0");
136  response_headers()->ComputeCaching();
137  }
138 
139  virtual void HandleDone(bool success) {
140  SharedAsyncFetch::HandleDone(success);
141  delete this;
142  }
143 
144  private:
145  AsyncFetch* async_fetch_;
146  DISALLOW_COPY_AND_ASSIGN(BackgroundFetchCheckingAsyncFetch);
147 };
148 
152  public:
154  : base_fetcher_(fetcher),
155  num_background_fetches_(0) {}
157 
158  virtual void Fetch(const GoogleString& url,
159  MessageHandler* message_handler,
160  AsyncFetch* fetch) {
161  if (fetch->IsBackgroundFetch()) {
162  num_background_fetches_++;
163  }
166  base_fetcher_->Fetch(url, message_handler, new_fetch);
167  }
168 
169  int num_background_fetches() { return num_background_fetches_; }
170  void clear_num_background_fetches() { num_background_fetches_ = 0; }
171 
172  private:
173  UrlAsyncFetcher* base_fetcher_;
174  int num_background_fetches_;
175  DISALLOW_COPY_AND_ASSIGN(BackgroundFetchCheckingUrlAsyncFetcher);
176 };
177 
179  public:
180  void TestHeadersSetupRace();
181 
182  protected:
183  static const int kHtmlCacheTimeSec = 5000;
184 
186  virtual void SetUp();
187  virtual void TearDown();
188 
189  void FetchFromProxy(
190  const StringPiece& url,
191  const RequestHeaders& request_headers,
192  bool expect_success,
193  GoogleString* string_out,
194  ResponseHeaders* headers_out,
195  bool proxy_fetch_property_callback_collector_created);
196 
197  void FetchFromProxy(const StringPiece& url,
198  const RequestHeaders& request_headers,
199  bool expect_success,
200  GoogleString* string_out,
201  ResponseHeaders* headers_out);
202 
203  void FetchFromProxy(const StringPiece& url,
204  bool expect_success,
205  GoogleString* string_out,
206  ResponseHeaders* headers_out);
207 
208  void FetchFromProxyLoggingFlushes(const StringPiece& url,
209  bool expect_success,
210  GoogleString* string_out);
211 
212  void FetchFromProxyNoWait(const StringPiece& url,
213  const RequestHeaders& request_headers,
214  bool expect_success,
215  bool log_flush,
216  ResponseHeaders* headers_out);
217 
218  void WaitForFetch(bool proxy_fetch_property_callback_collector_created);
219 
220  void TestPropertyCache(const StringPiece& url,
221  bool delay_pcache, bool thread_pcache,
222  bool expect_success);
223 
224  void TestPropertyCacheWithHeadersAndOutput(
225  const StringPiece& url, bool delay_pcache, bool thread_pcache,
226  bool expect_success, bool check_stats, bool add_create_filter_callback,
227  bool expect_detach_before_pcache, const RequestHeaders& request_headers,
228  ResponseHeaders* response_headers, GoogleString* output);
229 
230  void SetCriticalImagesInFinder(StringSet* critical_images);
231  void SetCssCriticalImagesInFinder(StringSet* css_critical_images);
232 
233  scoped_ptr<ProxyInterface> proxy_interface_;
234  scoped_ptr<WorkerTestBase::SyncPoint> sync_;
235  ResponseHeaders callback_response_headers_;
236  GoogleString callback_buffer_;
237  bool callback_done_value_;
238 
239  private:
240  friend class FilterCallback;
241 
242  MockCriticalImagesFinder* mock_critical_images_finder_;
243 };
244 
245 }
246 #endif
Definition: url_namer.h:37
virtual bool Decode(const GoogleUrl &gurl, const RewriteOptions *rewrite_options, GoogleUrl *domain, GoogleString *decoded) const
Given the request_url, generate the original url.
Definition: proxy_interface_test_base.h:53
Holds the value & stability-metadata for a property.
Definition: property_cache.h:122
virtual bool IsAuthorized(const GoogleUrl &gurl, const RewriteOptions &options) const
Definition: proxy_interface_test_base.h:65
Definition: test_rewrite_driver_factory.h:79
Definition: rewrite_test_base.h:88
ResponseHeaders * response_headers()
See doc for request_headers and set_request_headers.
Definition: async_fetch.h:267
Definition: async_fetch.h:53
Definition: proxy_interface_test_base.h:178
Definition: proxy_interface_test_base.h:83
virtual void Fetch(const GoogleString &url, MessageHandler *message_handler, AsyncFetch *fetch)=0
Definition: rewrite_driver.h:98
virtual void Fetch(const GoogleString &url, MessageHandler *message_handler, AsyncFetch *fetch)
Definition: proxy_interface_test_base.h:158
virtual bool IsBackgroundFetch() const
Definition: async_fetch.h:125
virtual void HandleHeadersComplete()
Definition: proxy_interface_test_base.h:131
Definition: proxy_interface_test_base.h:151
UrlAsyncFetcher()
Definition: url_async_fetcher.h:81
Definition: rewrite_options.h:81
Definition: proxy_interface_test_base.h:124
Definition: url_async_fetcher.h:33
Definition: mock_critical_images_finder.h:36
Definition: proxy_interface_test_base.h:108