Page Speed Optimization Libraries
1.3.25.1
|
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 GoogleUrl; 00041 class HtmlElement; 00042 class HtmlFilter; 00043 class MessageHandler; 00044 class PropertyValue; 00045 class RequestHeaders; 00046 class RewriteDriver; 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), options_(NULL) {} 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 00070 virtual void DecodeOptions(const GoogleUrl& request_url, 00071 const RequestHeaders& request_headers, 00072 Callback* callback, 00073 MessageHandler* handler) const { 00074 callback->Done((options_ == NULL) ? NULL : options_->Clone()); 00075 } 00076 00077 void set_authorized(bool authorized) { authorized_ = authorized; } 00078 void set_options(RewriteOptions* options) { options_ = options; } 00079 00080 private: 00081 bool authorized_; 00082 RewriteOptions* options_; 00083 DISALLOW_COPY_AND_ASSIGN(ProxyUrlNamer); 00084 }; 00085 00092 class MockFilter : public EmptyHtmlFilter { 00093 public: 00094 explicit MockFilter(RewriteDriver* driver) 00095 : driver_(driver), 00096 num_elements_(0), 00097 num_elements_property_(NULL), 00098 client_state_(NULL) { 00099 } 00100 00101 virtual void StartDocument(); 00102 00103 virtual void StartElement(HtmlElement* element); 00104 00105 virtual void EndDocument(); 00106 00107 virtual const char* Name() const { return "MockFilter"; } 00108 00109 private: 00110 RewriteDriver* driver_; 00111 int num_elements_; 00112 PropertyValue* num_elements_property_; 00113 GoogleString client_id_; 00114 AbstractClientState* client_state_; 00115 DISALLOW_COPY_AND_ASSIGN(MockFilter); 00116 }; 00117 00120 class CreateFilterCallback 00121 : public TestRewriteDriverFactory::CreateFilterCallback { 00122 public: 00123 CreateFilterCallback() {} 00124 virtual ~CreateFilterCallback() {} 00125 00126 virtual HtmlFilter* Done(RewriteDriver* driver) { 00127 return new MockFilter(driver); 00128 } 00129 00130 private: 00131 DISALLOW_COPY_AND_ASSIGN(CreateFilterCallback); 00132 }; 00133 00136 class BackgroundFetchCheckingAsyncFetch : public SharedAsyncFetch { 00137 public: 00138 explicit BackgroundFetchCheckingAsyncFetch(AsyncFetch* base_fetch) 00139 : SharedAsyncFetch(base_fetch) {} 00140 virtual ~BackgroundFetchCheckingAsyncFetch() {} 00141 00142 virtual void HandleHeadersComplete() { 00143 base_fetch()->HeadersComplete(); 00144 response_headers()->Add(kBackgroundFetchHeader, 00145 base_fetch()->IsBackgroundFetch() ? "1" : "0"); 00147 response_headers()->ComputeCaching(); 00148 } 00149 00150 virtual void HandleDone(bool success) { 00151 base_fetch()->Done(success); 00152 delete this; 00153 } 00154 00155 private: 00156 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchCheckingAsyncFetch); 00157 }; 00158 00161 class BackgroundFetchCheckingUrlAsyncFetcher : public UrlAsyncFetcher { 00162 public: 00163 explicit BackgroundFetchCheckingUrlAsyncFetcher(UrlAsyncFetcher* fetcher) 00164 : base_fetcher_(fetcher), 00165 num_background_fetches_(0) {} 00166 virtual ~BackgroundFetchCheckingUrlAsyncFetcher() {} 00167 00168 virtual void Fetch(const GoogleString& url, 00169 MessageHandler* message_handler, 00170 AsyncFetch* fetch) { 00171 if (fetch->IsBackgroundFetch()) { 00172 num_background_fetches_++; 00173 } 00174 BackgroundFetchCheckingAsyncFetch* new_fetch = 00175 new BackgroundFetchCheckingAsyncFetch(fetch); 00176 base_fetcher_->Fetch(url, message_handler, new_fetch); 00177 } 00178 00179 int num_background_fetches() { return num_background_fetches_; } 00180 void clear_num_background_fetches() { num_background_fetches_ = 0; } 00181 00182 private: 00183 UrlAsyncFetcher* base_fetcher_; 00184 int num_background_fetches_; 00185 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchCheckingUrlAsyncFetcher); 00186 }; 00187 00192 class ProxyInterfaceTestBase : public RewriteTestBase { 00193 public: 00194 void TestHeadersSetupRace(); 00195 00196 protected: 00197 static const int kHtmlCacheTimeSec = 5000; 00198 00199 ProxyInterfaceTestBase() : callback_done_value_(false) {} 00200 virtual void SetUp(); 00201 virtual void TearDown(); 00202 00203 void FetchFromProxy(const StringPiece& url, 00204 const RequestHeaders& request_headers, 00205 bool expect_success, 00206 GoogleString* string_out, 00207 ResponseHeaders* headers_out); 00208 00209 void FetchFromProxy(const StringPiece& url, 00210 bool expect_success, 00211 GoogleString* string_out, 00212 ResponseHeaders* headers_out); 00213 00214 void FetchFromProxyLoggingFlushes(const StringPiece& url, 00215 bool expect_success, 00216 GoogleString* string_out); 00217 00218 void FetchFromProxyNoWait(const StringPiece& url, 00219 const RequestHeaders& request_headers, 00220 bool expect_success, 00221 bool log_flush, 00222 ResponseHeaders* headers_out); 00223 00224 void WaitForFetch(); 00225 00226 void TestPropertyCache(const StringPiece& url, 00227 bool delay_pcache, bool thread_pcache, 00228 bool expect_success); 00229 00230 void TestPropertyCacheWithHeadersAndOutput( 00231 const StringPiece& url, bool delay_pcache, bool thread_pcache, 00232 bool expect_success, bool check_stats, bool add_create_filter_callback, 00233 bool expect_detach_before_pcache, const RequestHeaders& request_headers, 00234 ResponseHeaders* response_headers, GoogleString* output); 00235 00236 scoped_ptr<ProxyInterface> proxy_interface_; 00237 scoped_ptr<WorkerTestBase::SyncPoint> sync_; 00238 ResponseHeaders callback_response_headers_; 00239 GoogleString callback_buffer_; 00240 bool callback_done_value_; 00241 00242 private: 00243 friend class FilterCallback; 00244 }; 00245 00246 } 00247 #endif ///< NET_INSTAWEB_AUTOMATIC_PUBLIC_PROXY_INTERFACE_TEST_BASE_H_