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