Page Speed Optimization Libraries
1.6.29.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 GoogleString client_id_; 00103 DISALLOW_COPY_AND_ASSIGN(MockFilter); 00104 }; 00105 00108 class CreateFilterCallback 00109 : public TestRewriteDriverFactory::CreateFilterCallback { 00110 public: 00111 CreateFilterCallback() {} 00112 virtual ~CreateFilterCallback() {} 00113 00114 virtual HtmlFilter* Done(RewriteDriver* driver) { 00115 return new MockFilter(driver); 00116 } 00117 00118 private: 00119 DISALLOW_COPY_AND_ASSIGN(CreateFilterCallback); 00120 }; 00121 00124 class BackgroundFetchCheckingAsyncFetch : public SharedAsyncFetch { 00125 public: 00126 explicit BackgroundFetchCheckingAsyncFetch(AsyncFetch* base_fetch) 00127 : SharedAsyncFetch(base_fetch), 00128 async_fetch_(base_fetch) {} 00129 virtual ~BackgroundFetchCheckingAsyncFetch() {} 00130 00131 virtual void HandleHeadersComplete() { 00132 SharedAsyncFetch::HandleHeadersComplete(); 00133 response_headers()->Add(kBackgroundFetchHeader, 00134 async_fetch_->IsBackgroundFetch() ? "1" : "0"); 00136 response_headers()->ComputeCaching(); 00137 } 00138 00139 virtual void HandleDone(bool success) { 00140 SharedAsyncFetch::HandleDone(success); 00141 delete this; 00142 } 00143 00144 private: 00145 AsyncFetch* async_fetch_; 00146 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchCheckingAsyncFetch); 00147 }; 00148 00151 class BackgroundFetchCheckingUrlAsyncFetcher : public UrlAsyncFetcher { 00152 public: 00153 explicit BackgroundFetchCheckingUrlAsyncFetcher(UrlAsyncFetcher* fetcher) 00154 : base_fetcher_(fetcher), 00155 num_background_fetches_(0) {} 00156 virtual ~BackgroundFetchCheckingUrlAsyncFetcher() {} 00157 00158 virtual void Fetch(const GoogleString& url, 00159 MessageHandler* message_handler, 00160 AsyncFetch* fetch) { 00161 if (fetch->IsBackgroundFetch()) { 00162 num_background_fetches_++; 00163 } 00164 BackgroundFetchCheckingAsyncFetch* new_fetch = 00165 new BackgroundFetchCheckingAsyncFetch(fetch); 00166 base_fetcher_->Fetch(url, message_handler, new_fetch); 00167 } 00168 00169 int num_background_fetches() { return num_background_fetches_; } 00170 void clear_num_background_fetches() { num_background_fetches_ = 0; } 00171 00172 private: 00173 UrlAsyncFetcher* base_fetcher_; 00174 int num_background_fetches_; 00175 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchCheckingUrlAsyncFetcher); 00176 }; 00177 00178 class ProxyInterfaceTestBase : public RewriteTestBase { 00179 public: 00180 void TestHeadersSetupRace(); 00181 00182 protected: 00183 static const int kHtmlCacheTimeSec = 5000; 00184 00185 ProxyInterfaceTestBase(); 00186 virtual void SetUp(); 00187 virtual void TearDown(); 00188 00189 void FetchFromProxy(const StringPiece& url, 00190 const RequestHeaders& request_headers, 00191 bool expect_success, 00192 GoogleString* string_out, 00193 ResponseHeaders* headers_out); 00194 00195 void FetchFromProxy(const StringPiece& url, 00196 bool expect_success, 00197 GoogleString* string_out, 00198 ResponseHeaders* headers_out); 00199 00200 void FetchFromProxyLoggingFlushes(const StringPiece& url, 00201 bool expect_success, 00202 GoogleString* string_out); 00203 00204 void FetchFromProxyNoWait(const StringPiece& url, 00205 const RequestHeaders& request_headers, 00206 bool expect_success, 00207 bool log_flush, 00208 ResponseHeaders* headers_out); 00209 00210 void WaitForFetch(); 00211 00212 void TestPropertyCache(const StringPiece& url, 00213 bool delay_pcache, bool thread_pcache, 00214 bool expect_success); 00215 00216 void TestPropertyCacheWithHeadersAndOutput( 00217 const StringPiece& url, bool delay_pcache, bool thread_pcache, 00218 bool expect_success, bool check_stats, bool add_create_filter_callback, 00219 bool expect_detach_before_pcache, const RequestHeaders& request_headers, 00220 ResponseHeaders* response_headers, GoogleString* output); 00221 00222 void SetCriticalImagesInFinder(StringSet* critical_images); 00223 void SetCssCriticalImagesInFinder(StringSet* css_critical_images); 00224 00225 scoped_ptr<ProxyInterface> proxy_interface_; 00226 scoped_ptr<WorkerTestBase::SyncPoint> sync_; 00227 ResponseHeaders callback_response_headers_; 00228 GoogleString callback_buffer_; 00229 bool callback_done_value_; 00230 00231 private: 00232 friend class FilterCallback; 00233 00234 MockCriticalImagesFinder* mock_critical_images_finder_; 00235 }; 00236 00237 } 00238 #endif ///< NET_INSTAWEB_AUTOMATIC_PUBLIC_PROXY_INTERFACE_TEST_BASE_H_