Page Speed Optimization Libraries  1.2.24.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 #include "net/instaweb/http/public/response_headers.h"
00024 #include "net/instaweb/http/public/url_fetcher.h"
00025 #include "net/instaweb/util/public/basictypes.h"
00026 #include "net/instaweb/util/public/string.h"
00027 #include "net/instaweb/util/public/string_util.h"
00028 
00029 namespace net_instaweb {
00030 
00031 class MessageHandler;
00032 class MockTimer;
00033 class RequestHeaders;
00034 class Writer;
00035 
00038 class MockUrlFetcher : public UrlFetcher {
00039  public:
00040   MockUrlFetcher() : enabled_(true), fail_on_unexpected_(true),
00041                      update_date_headers_(false), omit_empty_writes_(false),
00042                      fail_after_headers_(false), verify_host_header_(false),
00043                      split_writes_(false), timer_(NULL) {}
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 bool StreamingFetchUrl(const GoogleString& url,
00067                                  const RequestHeaders& request_headers,
00068                                  ResponseHeaders* response_headers,
00069                                  Writer* response_writer,
00070                                  MessageHandler* message_handler);
00071 
00078   void SetResponseFailure(const StringPiece& url);
00079 
00081   void Clear();
00082 
00084   void RemoveResponse(const StringPiece& url);
00085 
00088   void Disable() { enabled_ = false; }
00089   void Enable() { enabled_ = true; }
00090 
00093   void set_fail_on_unexpected(bool x) { fail_on_unexpected_ = x; }
00094 
00097   void set_update_date_headers(bool x) { update_date_headers_ = x; }
00098 
00101   void set_omit_empty_writes(bool x) { omit_empty_writes_ = x; }
00102 
00106   void set_fail_after_headers(bool x) { fail_after_headers_ = x; }
00107 
00110   void set_verify_host_header(bool x) { verify_host_header_ = x; }
00111 
00112   void set_timer(Timer* timer) { timer_ = timer; }
00113 
00117   void set_split_writes(bool val) { split_writes_ = val; }
00118 
00119  private:
00120   class HttpResponse {
00121    public:
00122     HttpResponse(int64 last_modified_time, const GoogleString& etag,
00123                  const ResponseHeaders& in_header, const StringPiece& in_body)
00124         : last_modified_time_(last_modified_time),
00125           etag_(etag),
00126           body_(in_body.data(), in_body.size()),
00127           success_(true) {
00128       header_.CopyFrom(in_header);
00129     }
00130 
00131     const int64 last_modified_time() const { return last_modified_time_; }
00132     const GoogleString& etag() const { return etag_; }
00133     const ResponseHeaders& header() const { return header_; }
00134     ResponseHeaders* mutable_header() { return &header_; }
00135     const GoogleString& body() const { return body_; }
00136     void set_success(bool success) { success_ = success; }
00137     bool success() const { return success_; }
00138 
00139    private:
00140     int64 last_modified_time_;
00141     GoogleString etag_;
00142     ResponseHeaders header_;
00143     GoogleString body_;
00144     bool success_;
00145 
00146     DISALLOW_COPY_AND_ASSIGN(HttpResponse);
00147   };
00148   typedef std::map<const GoogleString, HttpResponse*> ResponseMap;
00149 
00150   ResponseMap response_map_;
00151 
00152   bool enabled_;
00153   bool fail_on_unexpected_; 
00154   bool update_date_headers_; 
00155   bool omit_empty_writes_; 
00156   bool fail_after_headers_; 
00157   bool verify_host_header_; 
00158   bool split_writes_; 
00159   Timer* timer_; 
00160 
00161   DISALLOW_COPY_AND_ASSIGN(MockUrlFetcher);
00162 };
00163 
00164 }  
00165 
00166 #endif  ///< NET_INSTAWEB_HTTP_PUBLIC_MOCK_URL_FETCHER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines