Page Speed Optimization Libraries  1.13.35.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 PAGESPEED_AUTOMATIC_PROXY_INTERFACE_TEST_BASE_H_
20 #define PAGESPEED_AUTOMATIC_PROXY_INTERFACE_TEST_BASE_H_
21 
44 
45 namespace net_instaweb {
46 
47 class MockCriticalImagesFinder;
48 
49 const char kPageUrl[] = "page.html";
50 const char kBackgroundFetchHeader[] = "X-Background-Fetch";
51 
54 class ProxyUrlNamer : public UrlNamer {
55  public:
56  static const char kProxyHost[];
57 
58  ProxyUrlNamer() : authorized_(true) {}
59 
61  bool Decode(const GoogleUrl& gurl,
62  const RewriteOptions* rewrite_options,
63  GoogleString* decoded) const override;
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 
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 
104 };
105 
110  public:
112  virtual ~CreateFilterCallback() {}
113 
114  virtual HtmlFilter* Done(RewriteDriver* driver) {
115  return new MockFilter(driver);
116  }
117 
118  private:
119 
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");
137  }
138 
139  virtual void HandleDone(bool success) {
140  SharedAsyncFetch::HandleDone(success);
141  delete this;
142  }
143 
144  private:
145  AsyncFetch* async_fetch_;
146 
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 
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 
238 
242  void SetHeaderLatencyMs(int64 header_latency_ms) {
243  header_latency_ms_ = header_latency_ms;
244  }
245 
246  scoped_ptr<ProxyInterface> proxy_interface_;
247  RequestContextPtr request_context_;
249  ResponseHeaders callback_response_headers_;
250  GoogleString callback_buffer_;
251  bool callback_done_value_;
252  int64 header_latency_ms_;
253 
254  private:
255  friend class FilterCallback;
256 
257  MockCriticalImagesFinder* mock_critical_images_finder_;
258 };
259 
260 }
261 #endif
Definition: url_namer.h:38
class GoogleUrl
Definition: google_url.h:58
virtual const char * Name() const
The name of this filter – used for logging and debugging.
Definition: proxy_interface_test_base.h:97
Definition: html_filter.h:35
Definition: proxy_interface_test_base.h:54
virtual RequestContextPtr request_context()
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
void SetHeaderLatencyMs(int64 header_latency_ms)
Definition: proxy_interface_test_base.h:242
Read/write API for HTTP request (RequestHeaders is a misnomer).
Definition: request_headers.h:32
Definition: empty_html_filter.h:37
Definition: test_rewrite_driver_factory.h:79
virtual void EndDocument()
void Add(const StringPiece &name, const StringPiece &value)
Definition: rewrite_test_base.h:90
ResponseHeaders * response_headers()
See doc for request_headers and set_request_headers.
Definition: html_element.h:42
Read/write API for HTTP response headers.
Definition: response_headers.h:37
Definition: scoped_ptr.h:30
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
bool Decode(const GoogleUrl &gurl, const RewriteOptions *rewrite_options, GoogleString *decoded) const override
Given the request_url, generate the original url.
Definition: async_fetch.h:278
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:100
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
Definition: message_handler.h:39
virtual void HandleHeadersComplete()
Definition: proxy_interface_test_base.h:131
Definition: proxy_interface_test_base.h:151
UrlAsyncFetcher()
Definition: url_async_fetcher.h:78
Definition: rewrite_options.h:84
virtual void StartElement(HtmlElement *element)
virtual void StartDocument()
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