Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
rewrite_context_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 
21 
22 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_CONTEXT_TEST_BASE_H_
23 #define NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_CONTEXT_TEST_BASE_H_
24 
26 
27 #include <utility>
28 #include <vector>
29 
30 #include "net/instaweb/rewriter/cached_result.pb.h"
57 
58 
59 namespace net_instaweb {
60 
61 class MockScheduler;
62 
65  public:
66  static const char kFilterId[];
67 
68  explicit TrimWhitespaceRewriter(OutputResourceKind kind) : kind_(kind) {
69  ClearStats();
70  }
71 
73  int num_rewrites() const { return num_rewrites_; }
74  void ClearStats() { num_rewrites_ = 0; }
75 
76  protected:
77  REFCOUNT_FRIEND_DECLARATION(TrimWhitespaceRewriter);
78  virtual ~TrimWhitespaceRewriter();
79 
80  virtual bool RewriteText(const StringPiece& url, const StringPiece& in,
81  GoogleString* out,
82  ServerContext* server_context);
83  virtual HtmlElement::Attribute* FindResourceAttribute(HtmlElement* element);
84  virtual OutputResourceKind kind() const { return kind_; }
85  virtual const char* id() const { return kFilterId; }
86  virtual const char* name() const { return "TrimWhitespace"; }
87 
88  private:
89  OutputResourceKind kind_;
90 
91  int num_rewrites_;
92 
93 
94 };
95 
101  public:
102  static const char kFilterId[];
103 
105  RewriteDriver* driver)
106  : SimpleTextFilter(new TrimWhitespaceRewriter(kind), driver) {
107  }
108  virtual ~TrimWhitespaceSyncFilter();
109 
110  virtual void StartElementImpl(HtmlElement* element);
111  virtual const char* id() const { return kFilterId; }
112  virtual const char* name() const { return "TrimWhitespaceSync"; }
113 
114  private:
115 
116 };
117 
120  public:
121  static const char kFilterId[];
122 
124  : kind_(kind), num_rewrites_(0) {}
125  static SimpleTextFilter* MakeFilter(OutputResourceKind kind,
126  RewriteDriver* driver,
127  UpperCaseRewriter** rewriter_out) {
128  *rewriter_out = new UpperCaseRewriter(kind);
129  return new SimpleTextFilter(*rewriter_out, driver);
130  }
131 
132  int num_rewrites() const { return num_rewrites_; }
133  void ClearStats() { num_rewrites_ = 0; }
134 
135  protected:
136  REFCOUNT_FRIEND_DECLARATION(UpperCaseRewriter);
137  virtual ~UpperCaseRewriter();
138 
139  virtual bool RewriteText(const StringPiece& url, const StringPiece& in,
140  GoogleString* out,
141  ServerContext* server_context) {
142  ++num_rewrites_;
143  in.CopyToString(out);
144  UpperString(out);
145  return in != *out;
146  }
147  virtual HtmlElement::Attribute* FindResourceAttribute(HtmlElement* element) {
148  if (element->keyword() == HtmlName::kLink) {
149  return element->FindAttribute(HtmlName::kHref);
150  }
151  return NULL;
152  }
153  virtual OutputResourceKind kind() const { return kind_; }
154  virtual const char* id() const { return kFilterId; }
155  virtual const char* name() const { return "UpperCase"; }
156 
157  private:
158  OutputResourceKind kind_;
159  int num_rewrites_;
160 
161 
162 };
163 
166 class NestedFilter : public RewriteFilter {
167  public:
169  static const bool kExpectNestedRewritesSucceed = true;
170  static const bool kExpectNestedRewritesFail = false;
171 
172  static const char kFilterId[];
173 
174  NestedFilter(RewriteDriver* driver, SimpleTextFilter* upper_filter,
175  UpperCaseRewriter* upper_rewriter, bool expected_nested_result)
176  : RewriteFilter(driver), upper_filter_(upper_filter),
177  upper_rewriter_(upper_rewriter), chain_(false),
178  check_nested_rewrite_result_(true),
179  expected_nested_rewrite_result_(expected_nested_result) {
180  ClearStats();
181  }
182 
184  int num_top_rewrites() const { return num_top_rewrites_; }
185  int num_sub_rewrites() const { return upper_rewriter_->num_rewrites(); }
186 
187  void ClearStats() {
188  num_top_rewrites_ = 0;
189  upper_rewriter_->ClearStats();
190  }
191 
193  void set_chain(bool x) { chain_ = x; }
194 
195  bool expected_nested_rewrite_result() const {
196  return expected_nested_rewrite_result_;
197  }
198 
199  void set_expected_nested_rewrite_result(bool x) {
200  expected_nested_rewrite_result_ = x;
201  }
202 
203  void set_check_nested_rewrite_result(bool x) {
204  check_nested_rewrite_result_ = x;
205  }
206 
207  protected:
208  virtual ~NestedFilter();
209 
210  class NestedSlot : public ResourceSlot {
211  public:
212  explicit NestedSlot(const ResourcePtr& resource) : ResourceSlot(resource) {}
213  virtual HtmlElement* element() const { return NULL; }
214  virtual void Render() {}
215  virtual GoogleString LocationString() const { return "nested:"; }
216  };
217 
218  class Context : public SingleRewriteContext {
219  public:
220  Context(RewriteDriver* driver, NestedFilter* filter, bool chain)
221  : SingleRewriteContext(driver, NULL, NULL),
222  filter_(filter),
223  chain_(chain) {
224  }
225  virtual ~Context();
226  virtual void RewriteSingle(
227  const ResourcePtr& input, const OutputResourcePtr& output);
228  bool PolicyPermitsRendering() const override { return true; }
229  virtual void Harvest();
230 
231  protected:
232  virtual const char* id() const { return kFilterId; }
233  virtual OutputResourceKind kind() const { return kRewrittenResource; }
234 
235  private:
236  std::vector<GoogleString*> strings_;
237  NestedFilter* filter_;
238  bool chain_;
239  ResourceSlotVector nested_slots_;
240 
241 
242  };
243 
245  return new Context(driver(), this, chain_);
246  }
247 
248  virtual void StartElementImpl(HtmlElement* element);
249  SimpleTextFilter* upper_filter() { return upper_filter_; }
250 
251  virtual const char* id() const { return kFilterId; }
252  virtual const char* Name() const { return "NestedFilter"; }
253  virtual void StartDocumentImpl() {}
254  virtual void EndElementImpl(HtmlElement* element) {}
255 
256  private:
258  SimpleTextFilter* upper_filter_;
259  UpperCaseRewriter* upper_rewriter_;
260  bool chain_;
261 
263  bool check_nested_rewrite_result_;
265  bool expected_nested_rewrite_result_;
266 
268  int num_top_rewrites_;
269 
270 
271 };
272 
278  public:
279  static const char kFilterId[];
280 
282  MockScheduler* scheduler,
283  int64 rewrite_delay_ms);
284  virtual ~CombiningFilter();
285 
286  class Combiner : public ResourceCombiner {
287  public:
288  Combiner(RewriteDriver* driver, RewriteFilter* filter)
290  driver, kContentTypeCss.file_extension() + 1, filter) {
291  }
292  OutputResourcePtr MakeOutput() {
293  return Combine(rewrite_driver_->message_handler());
294  }
295  bool Write(const ResourceVector& in, const OutputResourcePtr& out) {
296  return WriteCombination(in, out, rewrite_driver_->message_handler());
297  }
298 
299  virtual bool WritePiece(int index, int num_pieces, const Resource* input,
300  OutputResource* combination,
301  Writer* writer, MessageHandler* handler) {
302  writer->Write(prefix_, handler);
304  index, num_pieces, input, combination, writer, handler);
305  }
306 
307  void set_prefix(const GoogleString& prefix) { prefix_ = prefix; }
308 
309  private:
310  virtual const ContentType* CombinationContentType() {
311  return &kContentTypeCss;
312  }
313 
314  GoogleString prefix_;
315  };
316 
317  virtual const char* id() const { return kFilterId; }
318 
319  class Context : public RewriteContext {
320  public:
321  Context(RewriteDriver* driver, CombiningFilter* filter,
322  MockScheduler* scheduler);
323 
324  void AddElement(HtmlElement* element, HtmlElement::Attribute* href,
325  const ResourcePtr& resource) {
326  ResourceSlotPtr slot(Driver()->GetSlot(resource, element, href));
327  AddSlot(slot);
328  }
329 
330  protected:
331  virtual bool Partition(OutputPartitions* partitions,
332  OutputResourceVector* outputs);
333 
334  virtual void Rewrite(int partition_index,
335  CachedResult* partition,
336  const OutputResourcePtr& output);
337  virtual bool OptimizationOnly() const {
338  return filter_->optimization_only();
339  }
340 
341  void DoRewrite(int partition_index,
342  CachedResult* partition,
343  OutputResourcePtr output);
344  bool PolicyPermitsRendering() const override { return true; }
345  virtual void Render();
346  virtual void WillNotRender();
347  virtual void Cancel();
348  void DisableRemovedSlots(const CachedResult* partition);
349  virtual const UrlSegmentEncoder* encoder() const { return &encoder_; }
350  virtual const char* id() const { return kFilterId; }
351  virtual OutputResourceKind kind() const {
352  return filter_->on_the_fly_ ? kOnTheFlyResource : kRewrittenResource;
353  }
354 
355  private:
356  Combiner combiner_;
357  UrlMultipartEncoder encoder_;
358  MockScheduler* scheduler_;
359  int64 time_at_start_of_rewrite_us_;
360  CombiningFilter* filter_;
361  };
362 
363  virtual void StartDocumentImpl() {}
364  virtual void StartElementImpl(HtmlElement* element);
365  virtual void Flush() {
366  if (context_.get() != NULL) {
367  driver()->InitiateRewrite(context_.release());
368  }
369  }
370 
371  virtual void EndElementImpl(HtmlElement* element) {}
372  virtual const char* Name() const { return "Combining"; }
374  return new Context(driver(), this, scheduler_);
375  }
376  virtual const UrlSegmentEncoder* encoder() const { return &encoder_; }
377 
378  virtual bool ComputeOnTheFly() const { return on_the_fly_; }
379 
380  int num_rewrites() const { return num_rewrites_; }
381  int num_render() const { return num_render_; }
382  int num_will_not_render() const { return num_will_not_render_; }
383  int num_cancel() const { return num_cancel_; }
384 
385  void ClearStats() { num_rewrites_ = 0; }
386  int64 rewrite_delay_ms() const { return rewrite_delay_ms_; }
387  void set_rewrite_block_on(WorkerTestBase::SyncPoint* sync) {
388  rewrite_block_on_ = sync;
389  }
390 
391  void set_rewrite_signal_on(WorkerTestBase::SyncPoint* sync) {
392  rewrite_signal_on_ = sync;
393  }
394 
396  void set_prefix(const GoogleString& prefix) { prefix_ = prefix; }
397 
398  void set_on_the_fly(bool v) { on_the_fly_ = v; }
399 
400  void set_disable_successors(bool v) { disable_successors_ = v; }
401 
402  bool optimization_only() const { return optimization_only_; }
403  void set_optimization_only(bool o) { optimization_only_ = o; }
404 
405  private:
406  friend class Context;
407 
408  scoped_ptr<Context> context_;
409  UrlMultipartEncoder encoder_;
410  MockScheduler* scheduler_;
411  int num_rewrites_;
412  int num_render_;
413  int num_will_not_render_;
414  int num_cancel_;
415  int64 rewrite_delay_ms_;
416 
419  WorkerTestBase::SyncPoint* rewrite_block_on_;
420 
424  WorkerTestBase::SyncPoint* rewrite_signal_on_;
425  GoogleString prefix_;
426  bool on_the_fly_;
427  bool optimization_only_;
428  bool disable_successors_;
430 
432 
433 };
434 
436  protected:
437  static const int64 kRewriteDeadlineMs = 20;
438 
441  static const int64 kOriginTtlMs = 12 * Timer::kMinuteMs;
444  static const int64 kLowOriginTtlMs = 5 * Timer::kSecondMs;
445 
449  return StrCat("max-age=", Integer64ToString(
451  }
452 
453  explicit RewriteContextTestBase(
454  std::pair<TestRewriteDriverFactory*, TestRewriteDriverFactory*> factories)
455  : RewriteTestBase(factories) {}
456  RewriteContextTestBase() {}
457  virtual ~RewriteContextTestBase();
458 
459  virtual void SetUp();
460  virtual void TearDown();
461  virtual bool AddBody() const { return false; }
462  virtual void ClearStats();
463 
464  void InitResources() { InitResourcesToDomain(kTestDomain); }
465  void InitResourcesToDomain(const char* domain);
466  void InitTrimFilters(OutputResourceKind kind);
467  void InitUpperFilter(OutputResourceKind kind, RewriteDriver* rewrite_driver);
468  void InitCombiningFilter(int64 rewrite_delay_ms);
469  void InitNestedFilter(bool expected_nested_rewrite_result);
470 
471  TrimWhitespaceRewriter* trim_filter_;
472  TrimWhitespaceRewriter* other_trim_filter_;
473  CombiningFilter* combining_filter_;
474  NestedFilter* nested_filter_;
475 };
476 
477 }
478 
479 #endif
virtual bool WritePiece(int index, int num_pieces, const Resource *input, OutputResource *combination, Writer *writer, MessageHandler *handler)
Definition: rewrite_context_test_base.h:299
virtual OutputResourceKind kind() const
Definition: rewrite_context_test_base.h:233
Definition: html_element.h:66
Definition: url_multipart_encoder.h:45
virtual const UrlSegmentEncoder * encoder() const
Definition: rewrite_context_test_base.h:349
Definition: mock_scheduler.h:39
Definition: rewrite_context_test_base.h:210
virtual bool WritePiece(int index, int num_pieces, const Resource *input, OutputResource *combination, Writer *writer, MessageHandler *handler)
virtual GoogleString LocationString() const
Definition: rewrite_context_test_base.h:215
virtual const char * Name() const
The name of this filter – used for logging and debugging.
Definition: rewrite_context_test_base.h:372
SingleRewriteContext(RewriteDriver *driver, RewriteContext *parent, ResourceContext *resource_context)
Definition: rewrite_context_test_base.h:286
const char * file_extension() const
Definition: content_type.h:64
ResourceCombiner(RewriteDriver *rewrite_driver, const StringPiece &extension, RewriteFilter *filter)
Note: extension should not include the leading dot here.
virtual bool ComputeOnTheFly() const
Definition: rewrite_context_test_base.h:378
Definition: rewrite_test_base.h:90
virtual RewriteContext * MakeRewriteContext()
Definition: rewrite_context_test_base.h:244
Definition: rewrite_context_test_base.h:277
RewriteDriver * rewrite_driver()
Definition: rewrite_test_base.h:517
bool PolicyPermitsRendering() const override
Definition: rewrite_context_test_base.h:228
virtual void StartDocumentImpl()
Definition: rewrite_context_test_base.h:363
static const int64 kSecondMs
Definition: timer.h:34
Definition: resource_slot.h:54
Definition: output_resource_kind.h:28
Definition: html_element.h:42
Definition: simple_text_filter.h:46
Definition: rewrite_context_test_base.h:100
virtual void Rewrite(int partition_index, CachedResult *partition, const OutputResourcePtr &output)
Definition: resource.h:57
const Attribute * FindAttribute(HtmlName::Keyword keyword) const
A similarly structured test-filter: this one just upper-cases its text.
Definition: rewrite_context_test_base.h:119
static const int64 kOriginTtlMs
Definition: rewrite_context_test_base.h:441
static const int64 kLowOriginTtlMs
Definition: rewrite_context_test_base.h:444
virtual const char * id() const
Returns the filter ID.
Definition: rewrite_context_test_base.h:350
virtual bool Partition(OutputPartitions *partitions, OutputResourceVector *outputs)
virtual void RewriteSingle(const ResourcePtr &input, const OutputResourcePtr &output)
Subclasses of SingleRewriteContext must override this:
virtual bool AddBody() const
Definition: rewrite_context_test_base.h:461
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
virtual void Render()
Definition: rewrite_context_test_base.h:214
GoogleString OriginTtlMaxAge()
Definition: rewrite_context_test_base.h:448
HtmlName::Keyword keyword() const
Definition: html_element.h:322
Definition: rewrite_context_test_base.h:166
void set_chain(bool x)
Set this to true to create a chain of nested rewrites on the same slot.
Definition: rewrite_context_test_base.h:193
int num_rewrites() const
Stats.
Definition: rewrite_context_test_base.h:73
virtual HtmlElement * element() const
Return HTML element associated with slot, or NULL if none (CSS, IPRO)
Definition: rewrite_context_test_base.h:213
Definition: rewrite_filter.h:35
Definition: resource_combiner.h:57
virtual bool WriteCombination(const ResourceVector &combine_resources, const OutputResourcePtr &combination, MessageHandler *handler)
Interface for writing bytes to an output stream.
Definition: writer.h:29
Definition: rewrite_driver.h:100
void set_prefix(const GoogleString &prefix)
Each entry in combination will be prefixed with this.
Definition: rewrite_context_test_base.h:396
Definition: server_context.h:99
Definition: content_type.h:31
Definition: rewrite_context.h:155
virtual void Flush()
Definition: rewrite_context_test_base.h:365
virtual bool OptimizationOnly() const
Definition: rewrite_context_test_base.h:337
bool InitiateRewrite(RewriteContext *rewrite_context) LOCKS_EXCLUDED(rewrite_mutex())
Derived from some input resource URL or URLs.
Definition: output_resource_kind.h:27
static const bool kExpectNestedRewritesSucceed
For use with NestedFilter constructor.
Definition: rewrite_context_test_base.h:169
Definition: rewrite_context_test_base.h:218
virtual OutputResourceKind kind() const
Definition: rewrite_context_test_base.h:351
virtual const char * id() const
Returns the filter ID.
Definition: rewrite_context_test_base.h:232
virtual const char * Name() const
The name of this filter – used for logging and debugging.
Definition: rewrite_context_test_base.h:252
Definition: url_segment_encoder.h:33
Definition: single_rewrite_context.h:36
Definition: rewrite_context_test_base.h:319
RewriteContext * MakeRewriteContext()
Definition: rewrite_context_test_base.h:373
Definition: message_handler.h:39
Definition: rewrite_context_test_base.h:435
Definition: output_resource.h:44
OutputResourcePtr Combine(MessageHandler *handler)
Definition: simple_text_filter.h:48
virtual void StartDocumentImpl()
Definition: rewrite_context_test_base.h:253
int num_top_rewrites() const
Stats.
Definition: rewrite_context_test_base.h:184
virtual const UrlSegmentEncoder * encoder() const
Definition: rewrite_context_test_base.h:376
Simple test filter just trims whitespace from the input resource.
Definition: rewrite_context_test_base.h:64
void AddSlot(const ResourceSlotPtr &slot)
OutputResourceKind
Definition: output_resource_kind.h:26
bool PolicyPermitsRendering() const override
Definition: rewrite_context_test_base.h:344