Page Speed Optimization Libraries  1.7.30.4
net/instaweb/http/public/mock_url_fetcher.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 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_HTTP_PUBLIC_MOCK_URL_FETCHER_H_
00020 #define NET_INSTAWEB_HTTP_PUBLIC_MOCK_URL_FETCHER_H_
00021 
00022 #include <map>
00023 
00024 #include "net/instaweb/http/public/response_headers.h"
00025 #include "net/instaweb/http/public/url_async_fetcher.h"
00026 #include "net/instaweb/util/public/basictypes.h"
00027 #include "net/instaweb/util/public/scoped_ptr.h"
00028 #include "net/instaweb/util/public/string.h"
00029 #include "net/instaweb/util/public/string_util.h"
00030 #include "pagespeed/kernel/base/abstract_mutex.h"
00031 
00032 namespace net_instaweb {
00033 
00034 class AsyncFetch;
00035 class MessageHandler;
00036 class ThreadSystem;
00037 class Timer;
00038 
00041 class MockUrlFetcher : public UrlAsyncFetcher {
00042  public:
00043   MockUrlFetcher();
00044   virtual ~MockUrlFetcher();
00045 
00046   void SetResponse(const StringPiece& url,
00047                    const ResponseHeaders& response_header,
00048                    const StringPiece& response_body);
00049 
00052   void AddToResponse(const StringPiece& url,
00053                      const StringPiece& name,
00054                      const StringPiece& value);
00055 
00059   void SetConditionalResponse(const StringPiece& url,
00060                               int64 last_modified_date,
00061                               const GoogleString& etag,
00062                               const ResponseHeaders& response_header,
00063                               const StringPiece& response_body);
00064 
00066   virtual void Fetch(const GoogleString& url,
00067                      MessageHandler* message_handler,
00068                      AsyncFetch* fetch);
00069 
00070   virtual bool SupportsHttps() const {
00071     ScopedMutex lock(mutex_.get());
00072     return supports_https_;
00073   }
00074 
00075   void set_fetcher_supports_https(bool supports_https) {
00076     ScopedMutex lock(mutex_.get());
00077     supports_https_ = supports_https;
00078   }
00079 
00081   const GoogleString& last_referer() {
00082     ScopedMutex lock(mutex_.get());
00083     return last_referer_;
00084   }
00085 
00092   void SetResponseFailure(const StringPiece& url);
00093 
00095   void Clear();
00096 
00098   void RemoveResponse(const StringPiece& url);
00099 
00102   void Disable() {
00103     ScopedMutex lock(mutex_.get());
00104     enabled_ = false;
00105   }
00106   void Enable() {
00107     ScopedMutex lock(mutex_.get());
00108     enabled_ = true;
00109   }
00110 
00113   void set_fail_on_unexpected(bool x) {
00114     ScopedMutex lock(mutex_.get());
00115     fail_on_unexpected_ = x;
00116   }
00117 
00120   void set_update_date_headers(bool x) {
00121     ScopedMutex lock(mutex_.get());
00122     update_date_headers_ = x;
00123   }
00124 
00127   void set_omit_empty_writes(bool x) {
00128     ScopedMutex lock(mutex_.get());
00129     omit_empty_writes_ = x;
00130   }
00131 
00135   void set_fail_after_headers(bool x) {
00136     ScopedMutex lock(mutex_.get());
00137     fail_after_headers_ = x;
00138   }
00139 
00142   void set_verify_host_header(bool x) {
00143     ScopedMutex lock(mutex_.get());
00144     verify_host_header_ = x;
00145   }
00146 
00147   void set_verify_pagespeed_header_off(bool x) {
00148     ScopedMutex lock(mutex_.get());
00149     verify_pagespeed_header_off_ = x;
00150   }
00151 
00152   void set_timer(Timer* timer) {
00153     ScopedMutex lock(mutex_.get());
00154     timer_ = timer;
00155   }
00156 
00160   void set_split_writes(bool val) {
00161     ScopedMutex lock(mutex_.get());
00162     split_writes_ = val;
00163   }
00164 
00166   void set_error_message(const GoogleString& msg) {
00167     ScopedMutex lock(mutex_.get());
00168     error_message_ = msg;
00169   }
00170 
00171   void set_strip_query_params(bool strip_query_params) {
00172     ScopedMutex lock(mutex_.get());
00173     strip_query_params_ = strip_query_params;
00174   }
00175 
00176  private:
00177   class HttpResponse {
00178    public:
00179     HttpResponse(int64 last_modified_time, const GoogleString& etag,
00180                  const ResponseHeaders& in_header, const StringPiece& in_body)
00181         : last_modified_time_(last_modified_time),
00182           etag_(etag),
00183           body_(in_body.data(), in_body.size()),
00184           success_(true) {
00185       header_.CopyFrom(in_header);
00186     }
00187 
00188     const int64 last_modified_time() const { return last_modified_time_; }
00189     const GoogleString& etag() const { return etag_; }
00190     const ResponseHeaders& header() const { return header_; }
00191     ResponseHeaders* mutable_header() { return &header_; }
00192     const GoogleString& body() const { return body_; }
00193     void set_success(bool success) { success_ = success; }
00194     bool success() const { return success_; }
00195 
00196    private:
00197     int64 last_modified_time_;
00198     GoogleString etag_;
00199     ResponseHeaders header_;
00200     GoogleString body_;
00201     bool success_;
00202 
00203     DISALLOW_COPY_AND_ASSIGN(HttpResponse);
00204   };
00205   typedef std::map<const GoogleString, HttpResponse*> ResponseMap;
00206 
00209   ResponseMap response_map_;
00210 
00211   bool enabled_;
00212   bool fail_on_unexpected_; 
00213   bool update_date_headers_; 
00214   bool omit_empty_writes_; 
00215   bool fail_after_headers_; 
00216   bool verify_host_header_; 
00217   bool verify_pagespeed_header_off_; 
00218   bool split_writes_; 
00219   bool supports_https_; 
00220   bool strip_query_params_; 
00221 
00222   GoogleString error_message_; 
00223   Timer* timer_; 
00224   GoogleString last_referer_; 
00225   scoped_ptr<ThreadSystem> thread_system_; 
00226   scoped_ptr<AbstractMutex> mutex_; 
00227 
00228   DISALLOW_COPY_AND_ASSIGN(MockUrlFetcher);
00229 };
00230 
00231 }  
00232 
00233 #endif  ///< NET_INSTAWEB_HTTP_PUBLIC_MOCK_URL_FETCHER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines