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