Page Speed Optimization Libraries  1.6.29.3
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/platform.h"
00028 #include "net/instaweb/util/public/scoped_ptr.h"
00029 #include "net/instaweb/util/public/string.h"
00030 #include "net/instaweb/util/public/string_util.h"
00031 #include "net/instaweb/util/public/thread_system.h"
00032 
00033 namespace net_instaweb {
00034 
00035 class AsyncFetch;
00036 class MessageHandler;
00037 class Timer;
00038 
00041 class MockUrlFetcher : public UrlAsyncFetcher {
00042  public:
00045   MockUrlFetcher() : enabled_(true), fail_on_unexpected_(true),
00046                      update_date_headers_(false), omit_empty_writes_(false),
00047                      fail_after_headers_(false), verify_host_header_(false),
00048                      split_writes_(false), supports_https_(false), timer_(NULL),
00049                      thread_system_(Platform::CreateThreadSystem()),
00050                      mutex_(thread_system_->NewMutex()) {}
00051   virtual ~MockUrlFetcher();
00052 
00053   void SetResponse(const StringPiece& url,
00054                    const ResponseHeaders& response_header,
00055                    const StringPiece& response_body);
00056 
00059   void AddToResponse(const StringPiece& url,
00060                      const StringPiece& name,
00061                      const StringPiece& value);
00062 
00066   void SetConditionalResponse(const StringPiece& url,
00067                               int64 last_modified_date,
00068                               const GoogleString& etag,
00069                               const ResponseHeaders& response_header,
00070                               const StringPiece& response_body);
00071 
00073   virtual void Fetch(const GoogleString& url,
00074                      MessageHandler* message_handler,
00075                      AsyncFetch* fetch);
00076 
00077   virtual bool SupportsHttps() const {
00078     ScopedMutex lock(mutex_.get());
00079     return supports_https_;
00080   }
00081 
00082   void set_fetcher_supports_https(bool supports_https) {
00083     ScopedMutex lock(mutex_.get());
00084     supports_https_ = supports_https;
00085   }
00086 
00088   const GoogleString& last_referer() {
00089     ScopedMutex lock(mutex_.get());
00090     return last_referer_;
00091   }
00092 
00099   void SetResponseFailure(const StringPiece& url);
00100 
00102   void Clear();
00103 
00105   void RemoveResponse(const StringPiece& url);
00106 
00109   void Disable() {
00110     ScopedMutex lock(mutex_.get());
00111     enabled_ = false;
00112   }
00113   void Enable() {
00114     ScopedMutex lock(mutex_.get());
00115     enabled_ = true;
00116   }
00117 
00120   void set_fail_on_unexpected(bool x) {
00121     ScopedMutex lock(mutex_.get());
00122     fail_on_unexpected_ = x;
00123   }
00124 
00127   void set_update_date_headers(bool x) {
00128     ScopedMutex lock(mutex_.get());
00129     update_date_headers_ = x;
00130   }
00131 
00134   void set_omit_empty_writes(bool x) {
00135     ScopedMutex lock(mutex_.get());
00136     omit_empty_writes_ = x;
00137   }
00138 
00142   void set_fail_after_headers(bool x) {
00143     ScopedMutex lock(mutex_.get());
00144     fail_after_headers_ = x;
00145   }
00146 
00149   void set_verify_host_header(bool x) {
00150     ScopedMutex lock(mutex_.get());
00151     verify_host_header_ = x;
00152   }
00153 
00154   void set_timer(Timer* timer) {
00155     ScopedMutex lock(mutex_.get());
00156     timer_ = timer;
00157   }
00158 
00162   void set_split_writes(bool val) {
00163     ScopedMutex lock(mutex_.get());
00164     split_writes_ = val;
00165   }
00166 
00168   void set_error_message(const GoogleString& msg) {
00169     ScopedMutex lock(mutex_.get());
00170     error_message_ = msg;
00171   }
00172 
00173  private:
00174   class HttpResponse {
00175    public:
00176     HttpResponse(int64 last_modified_time, const GoogleString& etag,
00177                  const ResponseHeaders& in_header, const StringPiece& in_body)
00178         : last_modified_time_(last_modified_time),
00179           etag_(etag),
00180           body_(in_body.data(), in_body.size()),
00181           success_(true) {
00182       header_.CopyFrom(in_header);
00183     }
00184 
00185     const int64 last_modified_time() const { return last_modified_time_; }
00186     const GoogleString& etag() const { return etag_; }
00187     const ResponseHeaders& header() const { return header_; }
00188     ResponseHeaders* mutable_header() { return &header_; }
00189     const GoogleString& body() const { return body_; }
00190     void set_success(bool success) { success_ = success; }
00191     bool success() const { return success_; }
00192 
00193    private:
00194     int64 last_modified_time_;
00195     GoogleString etag_;
00196     ResponseHeaders header_;
00197     GoogleString body_;
00198     bool success_;
00199 
00200     DISALLOW_COPY_AND_ASSIGN(HttpResponse);
00201   };
00202   typedef std::map<const GoogleString, HttpResponse*> ResponseMap;
00203 
00206   ResponseMap response_map_;
00207 
00208   bool enabled_;
00209   bool fail_on_unexpected_; 
00210   bool update_date_headers_; 
00211   bool omit_empty_writes_; 
00212   bool fail_after_headers_; 
00213   bool verify_host_header_; 
00214   bool split_writes_; 
00215   bool supports_https_; 
00216   GoogleString error_message_; 
00217   Timer* timer_; 
00218   GoogleString last_referer_; 
00219   scoped_ptr<ThreadSystem> thread_system_; 
00220   scoped_ptr<AbstractMutex> mutex_; 
00221 
00222   DISALLOW_COPY_AND_ASSIGN(MockUrlFetcher);
00223 };
00224 
00225 }  
00226 
00227 #endif  ///< NET_INSTAWEB_HTTP_PUBLIC_MOCK_URL_FETCHER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines