Page Speed Optimization Libraries  1.4.26.1
net/instaweb/automatic/public/proxy_interface_test_base.h
Go to the documentation of this file.
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->Done((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   virtual ~BackgroundFetchCheckingAsyncFetch() {}
00142 
00143   virtual void HandleHeadersComplete() {
00144     base_fetch()->HeadersComplete();
00145     response_headers()->Add(kBackgroundFetchHeader,
00146                             base_fetch()->IsBackgroundFetch() ? "1" : "0");
00148     response_headers()->ComputeCaching();
00149   }
00150 
00151   virtual void HandleDone(bool success) {
00152     base_fetch()->Done(success);
00153     delete this;
00154   }
00155 
00156  private:
00157   DISALLOW_COPY_AND_ASSIGN(BackgroundFetchCheckingAsyncFetch);
00158 };
00159 
00162 class BackgroundFetchCheckingUrlAsyncFetcher : public UrlAsyncFetcher {
00163  public:
00164   explicit BackgroundFetchCheckingUrlAsyncFetcher(UrlAsyncFetcher* fetcher)
00165       : base_fetcher_(fetcher),
00166         num_background_fetches_(0) {}
00167   virtual ~BackgroundFetchCheckingUrlAsyncFetcher() {}
00168 
00169   virtual void Fetch(const GoogleString& url,
00170                      MessageHandler* message_handler,
00171                      AsyncFetch* fetch) {
00172     if (fetch->IsBackgroundFetch()) {
00173       num_background_fetches_++;
00174     }
00175     BackgroundFetchCheckingAsyncFetch* new_fetch =
00176         new BackgroundFetchCheckingAsyncFetch(fetch);
00177     base_fetcher_->Fetch(url, message_handler, new_fetch);
00178   }
00179 
00180   int num_background_fetches() { return num_background_fetches_; }
00181   void clear_num_background_fetches() { num_background_fetches_ = 0; }
00182 
00183  private:
00184   UrlAsyncFetcher* base_fetcher_;
00185   int num_background_fetches_;
00186   DISALLOW_COPY_AND_ASSIGN(BackgroundFetchCheckingUrlAsyncFetcher);
00187 };
00188 
00193 class ProxyInterfaceTestBase : public RewriteTestBase {
00194  public:
00195   void TestHeadersSetupRace();
00196 
00197  protected:
00198   static const int kHtmlCacheTimeSec = 5000;
00199 
00200   ProxyInterfaceTestBase();
00201   virtual void SetUp();
00202   virtual void TearDown();
00203 
00204   void FetchFromProxy(const StringPiece& url,
00205                       const RequestHeaders& request_headers,
00206                       bool expect_success,
00207                       GoogleString* string_out,
00208                       ResponseHeaders* headers_out);
00209 
00210   void FetchFromProxy(const StringPiece& url,
00211                       bool expect_success,
00212                       GoogleString* string_out,
00213                       ResponseHeaders* headers_out);
00214 
00215   void FetchFromProxyLoggingFlushes(const StringPiece& url,
00216                                     bool expect_success,
00217                                     GoogleString* string_out);
00218 
00219   void FetchFromProxyNoWait(const StringPiece& url,
00220                             const RequestHeaders& request_headers,
00221                             bool expect_success,
00222                             bool log_flush,
00223                             ResponseHeaders* headers_out);
00224 
00225   void WaitForFetch();
00226 
00227   void TestPropertyCache(const StringPiece& url,
00228                          bool delay_pcache, bool thread_pcache,
00229                          bool expect_success);
00230 
00231   void TestPropertyCacheWithHeadersAndOutput(
00232       const StringPiece& url, bool delay_pcache, bool thread_pcache,
00233       bool expect_success, bool check_stats, bool add_create_filter_callback,
00234       bool expect_detach_before_pcache, const RequestHeaders& request_headers,
00235       ResponseHeaders* response_headers, GoogleString* output);
00236 
00237   void SetCriticalImagesInFinder(StringSet* critical_images);
00238   void SetCssCriticalImagesInFinder(StringSet* css_critical_images);
00239 
00240   scoped_ptr<ProxyInterface> proxy_interface_;
00241   scoped_ptr<WorkerTestBase::SyncPoint> sync_;
00242   ResponseHeaders callback_response_headers_;
00243   GoogleString callback_buffer_;
00244   bool callback_done_value_;
00245 
00246  private:
00247   friend class FilterCallback;
00248 
00249   CriticalImagesFinder* fake_critical_images_finder_;
00250 };
00251 
00252 }  
00253 #endif  ///< NET_INSTAWEB_AUTOMATIC_PUBLIC_PROXY_INTERFACE_TEST_BASE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines