Page Speed Optimization Libraries
1.2.24.1
|
00001 /* 00002 * Copyright 2010 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http:///www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00018 00020 00021 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_TEST_BASE_H_ 00022 #define NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_TEST_BASE_H_ 00023 00024 #include <utility> 00025 #include <vector> 00026 00027 #include "net/instaweb/htmlparse/public/html_parse_test_base.h" 00028 #include "net/instaweb/http/public/mock_url_fetcher.h" 00029 #include "net/instaweb/http/public/http_cache.h" 00030 #include "net/instaweb/http/public/logging_proto.h" 00031 #include "net/instaweb/http/public/response_headers.h" 00033 #include "net/instaweb/rewriter/public/resource.h" 00034 #include "net/instaweb/rewriter/public/rewrite_driver.h" 00035 #include "net/instaweb/rewriter/public/rewrite_options.h" 00036 #include "net/instaweb/rewriter/public/server_context.h" 00037 #include "net/instaweb/rewriter/public/test_rewrite_driver_factory.h" 00038 #include "net/instaweb/util/public/basictypes.h" 00039 #include "net/instaweb/util/public/md5_hasher.h" 00040 #include "net/instaweb/util/public/mem_file_system.h" 00041 #include "net/instaweb/util/public/mock_hasher.h" 00042 #include "net/instaweb/util/public/mock_message_handler.h" 00044 #include "net/instaweb/util/public/mock_timer.h" 00045 #include "net/instaweb/util/public/scoped_ptr.h" 00046 #include "net/instaweb/util/public/string.h" 00047 #include "net/instaweb/util/public/string_util.h" 00048 #include "net/instaweb/util/public/timer.h" 00049 #include "net/instaweb/util/public/url_segment_encoder.h" 00050 00051 00052 namespace net_instaweb { 00053 00054 class CountingUrlAsyncFetcher; 00055 class DelayCache; 00056 class HTTPValue; 00057 class Hasher; 00058 class HtmlWriterFilter; 00059 class LRUCache; 00060 class MessageHandler; 00061 class MockScheduler; 00062 class PropertyCache; 00063 class ResourceNamer; 00064 class RewriteFilter; 00065 class Statistics; 00066 class WaitUrlAsyncFetcher; 00067 struct ContentType; 00068 00069 class RewriteOptionsTestBase : public HtmlParseTestBaseNoAlloc { 00070 protected: 00071 RewriteOptionsTestBase() { 00072 RewriteOptions::Initialize(); 00073 } 00074 ~RewriteOptionsTestBase() { 00075 RewriteOptions::Terminate(); 00076 } 00077 }; 00078 00079 class RewriteTestBase : public RewriteOptionsTestBase { 00080 public: 00081 static const char kTestData[]; 00082 00089 enum ActiveServerFlag { 00090 kPrimary, 00091 kSecondary, 00092 }; 00093 00094 RewriteTestBase(); 00095 explicit RewriteTestBase(Statistics* statistics); 00096 00102 explicit RewriteTestBase(std::pair<TestRewriteDriverFactory*, 00103 TestRewriteDriverFactory*> factories); 00104 virtual ~RewriteTestBase(); 00105 00106 virtual void SetUp(); 00107 virtual void TearDown(); 00108 00112 virtual bool AddBody() const { return false; } 00113 00123 virtual TestRewriteDriverFactory* MakeTestFactory(); 00124 00127 void AddRecompressImageFilters(); 00128 00130 void AddFilter(RewriteOptions::Filter filter); 00131 00133 void AddOtherFilter(RewriteOptions::Filter filter); 00134 00137 void AddRewriteFilter(RewriteFilter* filter); 00138 00141 void AddFetchOnlyRewriteFilter(RewriteFilter* filter); 00142 00145 void AddOtherRewriteFilter(RewriteFilter* filter); 00146 00149 void SetBaseUrlForFetch(const StringPiece& url); 00150 00151 ResourcePtr CreateResource(const StringPiece& base, const StringPiece& url); 00152 00157 Timer* timer() { return factory()->mock_timer(); } 00158 00160 void AppendDefaultHeaders(const ContentType& content_type, 00161 GoogleString* text); 00162 00165 void AppendDefaultHeaders(const ContentType& content_type, 00166 int64 original_content_length, 00167 GoogleString* text); 00168 00169 void ServeResourceFromManyContexts(const GoogleString& resource_url, 00170 const StringPiece& expected_content); 00171 00174 void ServeResourceFromNewContext( 00175 const GoogleString& resource_url, 00176 const StringPiece& expected_content); 00177 00181 virtual RewriteDriver* html_parse() { return rewrite_driver_; } 00182 00184 void DefaultResponseHeaders(const ContentType& content_type, int64 ttl_sec, 00185 ResponseHeaders* response_headers); 00186 00190 bool FetchResource(const StringPiece& path, const StringPiece& filter_id, 00191 const StringPiece& name, const StringPiece& ext, 00192 GoogleString* content); 00193 bool FetchResource(const StringPiece& path, const StringPiece& filter_id, 00194 const StringPiece& name, const StringPiece& ext, 00195 GoogleString* content, ResponseHeaders* response); 00196 00197 bool FetchResourceUrl(const StringPiece& url, GoogleString* content, 00198 ResponseHeaders* response); 00199 bool FetchResourceUrl(const StringPiece& url, GoogleString* content); 00200 00202 bool TryFetchResource(const StringPiece& url); 00203 00208 void SetUseManagedRewriteDrivers(bool use_managed_rewrite_drivers); 00209 00210 GoogleString CssLinkHref(const StringPiece& url) { 00211 return StrCat("<link rel=stylesheet href=", url, ">"); 00212 } 00213 00215 class CssLink { 00216 public: 00217 CssLink(const StringPiece& url, const StringPiece& content, 00218 const StringPiece& media, bool supply_mock); 00219 00221 class Vector : public std::vector<CssLink*> { 00222 public: 00223 ~Vector(); 00224 void Add(const StringPiece& url, const StringPiece& content, 00225 const StringPiece& media, bool supply_mock); 00226 }; 00227 00230 bool DecomposeCombinedUrl(GoogleString* base, StringVector* segments, 00231 MessageHandler* handler); 00232 00233 GoogleString url_; 00234 GoogleString content_; 00235 GoogleString media_; 00236 bool supply_mock_; 00237 }; 00238 00240 void CollectCssLinks(const StringPiece& id, const StringPiece& html, 00241 StringVector* css_links); 00242 00244 void CollectCssLinks(const StringPiece& id, const StringPiece& html, 00245 CssLink::Vector* css_links); 00246 00248 void EncodePathAndLeaf(const StringPiece& filter_id, 00249 const StringPiece& hash, 00250 const StringVector& name_vector, 00251 const StringPiece& ext, 00252 ResourceNamer* namer); 00253 00254 StringVector MultiUrl(const StringPiece& url1) { 00255 StringVector v; 00256 v.push_back(url1.as_string()); 00257 return v; 00258 } 00259 00260 StringVector MultiUrl(const StringPiece& url1, const StringPiece& url2) { 00261 StringVector v; 00262 v.push_back(url1.as_string()); 00263 v.push_back(url2.as_string()); 00264 return v; 00265 } 00266 00267 StringVector MultiUrl(const StringPiece& url1, const StringPiece& url2, 00268 const StringPiece& url3) { 00269 StringVector v; 00270 v.push_back(url1.as_string()); 00271 v.push_back(url2.as_string()); 00272 v.push_back(url3.as_string()); 00273 return v; 00274 } 00275 00276 StringVector MultiUrl(const StringPiece& url1, const StringPiece& url2, 00277 const StringPiece& url3, const StringPiece& url4) { 00278 StringVector v; 00279 v.push_back(url1.as_string()); 00280 v.push_back(url2.as_string()); 00281 v.push_back(url3.as_string()); 00282 v.push_back(url4.as_string()); 00283 return v; 00284 } 00285 00288 GoogleString Encode(const StringPiece& path, 00289 const StringPiece& filter_id, 00290 const StringPiece& hash, 00291 const StringPiece& name, 00292 const StringPiece& ext) { 00293 return Encode(path, filter_id, hash, MultiUrl(name), ext); 00294 } 00295 GoogleString Encode(const StringPiece& path, 00296 const StringPiece& filter_id, 00297 const StringPiece& hash, 00298 const StringVector& name_vector, 00299 const StringPiece& ext); 00300 00302 GoogleString EncodeNormal(const StringPiece& path, 00303 const StringPiece& filter_id, 00304 const StringPiece& hash, 00305 const StringPiece& name, 00306 const StringPiece& ext) { 00307 return EncodeNormal(path, filter_id, hash, MultiUrl(name), ext); 00308 } 00309 GoogleString EncodeNormal(const StringPiece& path, 00310 const StringPiece& filter_id, 00311 const StringPiece& hash, 00312 const StringVector& name_vector, 00313 const StringPiece& ext); 00314 00317 GoogleString EncodeWithBase(const StringPiece& base, 00318 const StringPiece& path, 00319 const StringPiece& filter_id, 00320 const StringPiece& hash, 00321 const StringPiece& name, 00322 const StringPiece& ext) { 00323 return EncodeWithBase(base, path, filter_id, hash, MultiUrl(name), ext); 00324 } 00325 GoogleString EncodeWithBase(const StringPiece& base, 00326 const StringPiece& path, 00327 const StringPiece& filter_id, 00328 const StringPiece& hash, 00329 const StringVector& name_vector, 00330 const StringPiece& ext); 00331 00336 static GoogleString ChangeSuffix( 00337 GoogleString old_url, bool append_new_suffix, 00338 StringPiece old_suffix, StringPiece new_suffix); 00339 00344 void SetupWaitFetcher(); 00345 void CallFetcherCallbacks(); 00346 00347 RewriteOptions* options() { return options_; } 00348 RewriteOptions* other_options() { return other_options_; } 00349 00351 void TestServeFiles(const ContentType* content_type, 00352 const StringPiece& filter_id, 00353 const StringPiece& rewritten_ext, 00354 const StringPiece& orig_name, 00355 const StringPiece& orig_content, 00356 const StringPiece& rewritten_name, 00357 const StringPiece& rewritten_content); 00358 00359 TestRewriteDriverFactory* factory() { return factory_.get(); } 00360 TestRewriteDriverFactory* other_factory() { return other_factory_.get(); } 00361 00362 void UseMd5Hasher() { 00363 server_context_->set_hasher(&md5_hasher_); 00364 server_context_->http_cache()->set_hasher(&md5_hasher_); 00365 other_server_context_->set_hasher(&md5_hasher_); 00366 other_server_context_->http_cache()->set_hasher(&md5_hasher_); 00367 } 00368 00369 00370 void SetDefaultLongCacheHeaders(const ContentType* content_type, 00371 ResponseHeaders* header) { 00372 server_context_->SetDefaultLongCacheHeaders(content_type, header); 00373 } 00374 00375 void SetFetchResponse(const StringPiece& url, 00376 const ResponseHeaders& response_header, 00377 const StringPiece& response_body) { 00378 mock_url_fetcher()->SetResponse(url, response_header, response_body); 00379 } 00380 00382 void SetResponseWithDefaultHeaders(const StringPiece& relative_url, 00383 const ContentType& content_type, 00384 const StringPiece& content, 00385 int64 ttl_sec); 00386 00388 void AddFileToMockFetcher(const StringPiece& url, 00389 const StringPiece& filename, 00390 const ContentType& content_type, int64 ttl_sec); 00391 00392 void AddToResponse(const StringPiece& url, 00393 const StringPiece& name, 00394 const StringPiece& value) { 00395 mock_url_fetcher()->AddToResponse(url, name, value); 00396 } 00397 00398 void SetFetchResponse404(const StringPiece& url); 00399 00400 void SetFetchFailOnUnexpected(bool fail) { 00401 mock_url_fetcher()->set_fail_on_unexpected(fail); 00402 } 00403 void FetcherUpdateDateHeaders() { 00404 mock_url_fetcher()->set_timer(timer()); 00405 mock_url_fetcher()->set_update_date_headers(true); 00406 } 00407 void ClearFetcherResponses() { mock_url_fetcher()->Clear(); } 00408 00409 virtual void ClearStats(); 00410 00413 void ClearRewriteDriver(); 00414 00415 MockUrlFetcher* mock_url_fetcher() { 00416 return &mock_url_fetcher_; 00417 } 00418 Hasher* hasher() { return server_context_->hasher(); } 00419 DelayCache* delay_cache() { return factory_->delay_cache(); } 00420 LRUCache* lru_cache() { return factory_->lru_cache(); } 00421 Statistics* statistics() { return factory_->statistics(); } 00422 MemFileSystem* file_system() { return factory_->mem_file_system(); } 00423 HTTPCache* http_cache() { return server_context_->http_cache(); } 00424 PropertyCache* page_property_cache() { 00425 return server_context_->page_property_cache(); 00426 } 00427 MockMessageHandler* message_handler() { 00428 return factory_->mock_message_handler(); 00429 } 00430 00438 RewriteDriver* rewrite_driver() { return rewrite_driver_; } 00439 RewriteDriver* other_rewrite_driver() { return other_rewrite_driver_; } 00440 00442 MockScheduler* mock_scheduler() { return factory_->mock_scheduler(); } 00443 00444 int64 start_time_ms() const { return factory_->kStartTimeMs; } 00445 00446 bool ReadFile(const char* filename, GoogleString* contents) { 00447 return file_system()->ReadFile(filename, contents, message_handler()); 00448 } 00449 bool WriteFile(const char* filename, const StringPiece& contents) { 00450 return file_system()->WriteFile(filename, contents, message_handler()); 00451 } 00452 00453 ServerContext* server_context() { return server_context_; } 00454 ServerContext* other_server_context() { return other_server_context_; } 00455 CountingUrlAsyncFetcher* counting_url_async_fetcher() { 00456 return factory_->counting_url_async_fetcher(); 00457 } 00458 00459 void SetMockHashValue(const GoogleString& value) { 00460 factory_->mock_hasher()->set_hash_value(value); 00461 } 00462 00463 void SetCacheDelayUs(int64 delay_us); 00464 00469 RewriteDriver* MakeDriver(ServerContext* server_context, 00470 RewriteOptions* options); 00471 00473 GoogleString AbsolutifyUrl(const StringPiece& in); 00474 00479 void TestRetainExtraHeaders(const StringPiece& name, 00480 const StringPiece& filter_id, 00481 const StringPiece& ext); 00482 00486 const UrlSegmentEncoder* FindEncoder(const StringPiece& id) const; 00487 00489 void SetUseTestUrlNamer(bool use_test_url_namer); 00490 00493 GoogleString EncodeCssName(const StringPiece& name, 00494 bool supports_webp, 00495 bool can_inline); 00496 00500 bool ReadIfCached(const ResourcePtr& resource); 00501 00506 void InitiateResourceRead(const ResourcePtr& resource); 00507 00511 HTTPCache::FindResult HttpBlockingFind( 00512 const GoogleString& key, HTTPCache* http_cache, HTTPValue* value_out, 00513 ResponseHeaders* headers); 00514 00516 void SetXhtmlMimetype() { SetMimetype("application/xhtml+xml"); } 00517 00519 void SetHtmlMimetype() { SetMimetype("text/html"); } 00520 00522 void SetMimetype(const StringPiece& mimetype); 00523 00526 void CheckFetchFromHttpCache( 00527 StringPiece url, 00528 StringPiece expected_contents, 00529 int64 expected_expiration_ms); 00530 00531 protected: 00532 void Init(); 00533 00536 void CallFetcherCallbacksForDriver(WaitUrlAsyncFetcher* fetcher, 00537 RewriteDriver* driver); 00538 00541 void PopulateDefaultHeaders(const ContentType& content_type, 00542 int64 original_content_length, 00543 ResponseHeaders* headers); 00544 00547 void SetActiveServer(ActiveServerFlag server_to_use); 00548 00551 void AdvanceTimeUs(int64 delay_ms); 00552 void AdvanceTimeMs(int64 delay_ms) { AdvanceTimeUs(delay_ms * Timer::kMsUs); } 00553 void SetTimeUs(int64 time_us); 00554 void SetTimeMs(int64 time_ms) { SetTimeUs(time_ms * Timer::kMsUs); } 00555 00557 void AdjustTimeUsWithoutWakingAlarms(int64 time_us); 00558 00563 LoggingInfo* logging_info(); 00564 00566 MockUrlFetcher mock_url_fetcher_; 00567 scoped_ptr<Statistics> statistics_; 00568 00574 scoped_ptr<TestRewriteDriverFactory> factory_; 00575 scoped_ptr<TestRewriteDriverFactory> other_factory_; 00576 ServerContext* server_context_; 00577 RewriteDriver* rewrite_driver_; 00578 ServerContext* other_server_context_; 00579 RewriteDriver* other_rewrite_driver_; 00580 scoped_ptr<HtmlWriterFilter> other_html_writer_filter_; 00581 ActiveServerFlag active_server_; 00582 bool use_managed_rewrite_drivers_; 00583 00584 MD5Hasher md5_hasher_; 00585 00586 RewriteOptions* options_; 00587 RewriteOptions* other_options_; 00588 UrlSegmentEncoder default_encoder_; 00589 ResponseHeaders response_headers_; 00590 }; 00591 00592 } 00593 00594 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_TEST_BASE_H_