Page Speed Optimization Libraries
1.7.30.3
|
00001 /* 00002 * Copyright 2011 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 00019 #ifndef NET_INSTAWEB_AUTOMATIC_PUBLIC_PROXY_INTERFACE_TEST_BASE_H_ 00020 #define NET_INSTAWEB_AUTOMATIC_PUBLIC_PROXY_INTERFACE_TEST_BASE_H_ 00021 00022 #include "net/instaweb/automatic/public/proxy_interface.h" 00023 #include "net/instaweb/htmlparse/public/empty_html_filter.h" 00024 #include "net/instaweb/http/public/async_fetch.h" 00025 #include "net/instaweb/http/public/response_headers.h" 00026 #include "net/instaweb/http/public/url_async_fetcher.h" 00027 #include "net/instaweb/rewriter/public/rewrite_test_base.h" 00028 #include "net/instaweb/rewriter/public/test_rewrite_driver_factory.h" 00029 #include "net/instaweb/rewriter/public/url_namer.h" 00030 #include "net/instaweb/util/public/basictypes.h" 00031 #include "net/instaweb/util/public/scoped_ptr.h" 00032 #include "net/instaweb/util/public/string_util.h" 00033 #include "net/instaweb/util/public/string.h" 00034 #include "net/instaweb/util/worker_test_base.h" 00035 00036 namespace net_instaweb { 00037 00038 class MockCriticalImagesFinder; 00039 class GoogleUrl; 00040 class HtmlElement; 00041 class HtmlFilter; 00042 class MessageHandler; 00043 class PropertyValue; 00044 class RequestHeaders; 00045 class RewriteDriver; 00046 class RewriteOptions; 00047 00048 const char kPageUrl[] = "page.html"; 00049 const char kBackgroundFetchHeader[] = "X-Background-Fetch"; 00050 00053 class ProxyUrlNamer : public UrlNamer { 00054 public: 00055 static const char kProxyHost[]; 00056 00057 ProxyUrlNamer() : authorized_(true) {} 00058 00060 virtual bool Decode(const GoogleUrl& gurl, 00061 GoogleUrl* domain, 00062 GoogleString* decoded) const; 00063 00064 virtual bool IsAuthorized(const GoogleUrl& gurl, 00065 const RewriteOptions& options) const { 00066 return authorized_; 00067 } 00068 00069 void set_authorized(bool authorized) { authorized_ = authorized; } 00070 00071 private: 00072 bool authorized_; 00073 DISALLOW_COPY_AND_ASSIGN(ProxyUrlNamer); 00074 }; 00075 00082 class MockFilter : public EmptyHtmlFilter { 00083 public: 00084 explicit MockFilter(RewriteDriver* driver) 00085 : driver_(driver), 00086 num_elements_(0), 00087 num_elements_property_(NULL) { 00088 } 00089 00090 virtual void StartDocument(); 00091 00092 virtual void StartElement(HtmlElement* element); 00093 00094 virtual void EndDocument(); 00095 00096 virtual const char* Name() const { return "MockFilter"; } 00097 00098 private: 00099 RewriteDriver* driver_; 00100 int num_elements_; 00101 PropertyValue* num_elements_property_; 00102 DISALLOW_COPY_AND_ASSIGN(MockFilter); 00103 }; 00104 00107 class CreateFilterCallback 00108 : public TestRewriteDriverFactory::CreateFilterCallback { 00109 public: 00110 CreateFilterCallback() {} 00111 virtual ~CreateFilterCallback() {} 00112 00113 virtual HtmlFilter* Done(RewriteDriver* driver) { 00114 return new MockFilter(driver); 00115 } 00116 00117 private: 00118 DISALLOW_COPY_AND_ASSIGN(CreateFilterCallback); 00119 }; 00120 00123 class BackgroundFetchCheckingAsyncFetch : public SharedAsyncFetch { 00124 public: 00125 explicit BackgroundFetchCheckingAsyncFetch(AsyncFetch* base_fetch) 00126 : SharedAsyncFetch(base_fetch), 00127 async_fetch_(base_fetch) {} 00128 virtual ~BackgroundFetchCheckingAsyncFetch() {} 00129 00130 virtual void HandleHeadersComplete() { 00131 SharedAsyncFetch::HandleHeadersComplete(); 00132 response_headers()->Add(kBackgroundFetchHeader, 00133 async_fetch_->IsBackgroundFetch() ? "1" : "0"); 00135 response_headers()->ComputeCaching(); 00136 } 00137 00138 virtual void HandleDone(bool success) { 00139 SharedAsyncFetch::HandleDone(success); 00140 delete this; 00141 } 00142 00143 private: 00144 AsyncFetch* async_fetch_; 00145 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchCheckingAsyncFetch); 00146 }; 00147 00150 class BackgroundFetchCheckingUrlAsyncFetcher : public UrlAsyncFetcher { 00151 public: 00152 explicit BackgroundFetchCheckingUrlAsyncFetcher(UrlAsyncFetcher* fetcher) 00153 : base_fetcher_(fetcher), 00154 num_background_fetches_(0) {} 00155 virtual ~BackgroundFetchCheckingUrlAsyncFetcher() {} 00156 00157 virtual void Fetch(const GoogleString& url, 00158 MessageHandler* message_handler, 00159 AsyncFetch* fetch) { 00160 if (fetch->IsBackgroundFetch()) { 00161 num_background_fetches_++; 00162 } 00163 BackgroundFetchCheckingAsyncFetch* new_fetch = 00164 new BackgroundFetchCheckingAsyncFetch(fetch); 00165 base_fetcher_->Fetch(url, message_handler, new_fetch); 00166 } 00167 00168 int num_background_fetches() { return num_background_fetches_; } 00169 void clear_num_background_fetches() { num_background_fetches_ = 0; } 00170 00171 private: 00172 UrlAsyncFetcher* base_fetcher_; 00173 int num_background_fetches_; 00174 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchCheckingUrlAsyncFetcher); 00175 }; 00176 00177 class ProxyInterfaceTestBase : public RewriteTestBase { 00178 public: 00179 void TestHeadersSetupRace(); 00180 00181 protected: 00182 static const int kHtmlCacheTimeSec = 5000; 00183 00184 ProxyInterfaceTestBase(); 00185 virtual void SetUp(); 00186 virtual void TearDown(); 00187 00188 void FetchFromProxy( 00189 const StringPiece& url, 00190 const RequestHeaders& request_headers, 00191 bool expect_success, 00192 GoogleString* string_out, 00193 ResponseHeaders* headers_out, 00194 bool proxy_fetch_property_callback_collector_created); 00195 00196 void FetchFromProxy(const StringPiece& url, 00197 const RequestHeaders& request_headers, 00198 bool expect_success, 00199 GoogleString* string_out, 00200 ResponseHeaders* headers_out); 00201 00202 void FetchFromProxy(const StringPiece& url, 00203 bool expect_success, 00204 GoogleString* string_out, 00205 ResponseHeaders* headers_out); 00206 00207 void FetchFromProxyLoggingFlushes(const StringPiece& url, 00208 bool expect_success, 00209 GoogleString* string_out); 00210 00211 void FetchFromProxyNoWait(const StringPiece& url, 00212 const RequestHeaders& request_headers, 00213 bool expect_success, 00214 bool log_flush, 00215 ResponseHeaders* headers_out); 00216 00217 void WaitForFetch(bool proxy_fetch_property_callback_collector_created); 00218 00219 void TestPropertyCache(const StringPiece& url, 00220 bool delay_pcache, bool thread_pcache, 00221 bool expect_success); 00222 00223 void TestPropertyCacheWithHeadersAndOutput( 00224 const StringPiece& url, bool delay_pcache, bool thread_pcache, 00225 bool expect_success, bool check_stats, bool add_create_filter_callback, 00226 bool expect_detach_before_pcache, const RequestHeaders& request_headers, 00227 ResponseHeaders* response_headers, GoogleString* output); 00228 00229 void SetCriticalImagesInFinder(StringSet* critical_images); 00230 void SetCssCriticalImagesInFinder(StringSet* css_critical_images); 00231 00232 scoped_ptr<ProxyInterface> proxy_interface_; 00233 scoped_ptr<WorkerTestBase::SyncPoint> sync_; 00234 ResponseHeaders callback_response_headers_; 00235 GoogleString callback_buffer_; 00236 bool callback_done_value_; 00237 00238 private: 00239 friend class FilterCallback; 00240 00241 MockCriticalImagesFinder* mock_critical_images_finder_; 00242 }; 00243 00244 } 00245 #endif ///< NET_INSTAWEB_AUTOMATIC_PUBLIC_PROXY_INTERFACE_TEST_BASE_H_