Page Speed Optimization Libraries
1.5.27.2
|
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/request_context.h" 00024 #include "net/instaweb/http/public/response_headers.h" 00025 #include "net/instaweb/http/public/url_fetcher.h" 00026 #include "net/instaweb/util/public/basictypes.h" 00027 #include "net/instaweb/util/public/string.h" 00028 #include "net/instaweb/util/public/string_util.h" 00029 00030 namespace net_instaweb { 00031 00032 class MessageHandler; 00033 class RequestHeaders; 00034 class Timer; 00035 class Writer; 00036 00039 class MockUrlFetcher : public UrlFetcher { 00040 public: 00041 MockUrlFetcher() : enabled_(true), fail_on_unexpected_(true), 00042 update_date_headers_(false), omit_empty_writes_(false), 00043 fail_after_headers_(false), verify_host_header_(false), 00044 split_writes_(false), timer_(NULL) {} 00045 virtual ~MockUrlFetcher(); 00046 00047 void SetResponse(const StringPiece& url, 00048 const ResponseHeaders& response_header, 00049 const StringPiece& response_body); 00050 00053 void AddToResponse(const StringPiece& url, 00054 const StringPiece& name, 00055 const StringPiece& value); 00056 00060 void SetConditionalResponse(const StringPiece& url, 00061 int64 last_modified_date, 00062 const GoogleString& etag, 00063 const ResponseHeaders& response_header, 00064 const StringPiece& response_body); 00065 00067 virtual bool StreamingFetchUrl(const GoogleString& url, 00068 const RequestHeaders& request_headers, 00069 ResponseHeaders* response_headers, 00070 Writer* response_writer, 00071 MessageHandler* message_handler, 00072 const RequestContextPtr& request_context); 00073 00080 void SetResponseFailure(const StringPiece& url); 00081 00083 void Clear(); 00084 00086 void RemoveResponse(const StringPiece& url); 00087 00090 void Disable() { enabled_ = false; } 00091 void Enable() { enabled_ = true; } 00092 00095 void set_fail_on_unexpected(bool x) { fail_on_unexpected_ = x; } 00096 00099 void set_update_date_headers(bool x) { update_date_headers_ = x; } 00100 00103 void set_omit_empty_writes(bool x) { omit_empty_writes_ = x; } 00104 00108 void set_fail_after_headers(bool x) { fail_after_headers_ = x; } 00109 00112 void set_verify_host_header(bool x) { verify_host_header_ = x; } 00113 00114 void set_timer(Timer* timer) { timer_ = timer; } 00115 00119 void set_split_writes(bool val) { split_writes_ = val; } 00120 00121 private: 00122 class HttpResponse { 00123 public: 00124 HttpResponse(int64 last_modified_time, const GoogleString& etag, 00125 const ResponseHeaders& in_header, const StringPiece& in_body) 00126 : last_modified_time_(last_modified_time), 00127 etag_(etag), 00128 body_(in_body.data(), in_body.size()), 00129 success_(true) { 00130 header_.CopyFrom(in_header); 00131 } 00132 00133 const int64 last_modified_time() const { return last_modified_time_; } 00134 const GoogleString& etag() const { return etag_; } 00135 const ResponseHeaders& header() const { return header_; } 00136 ResponseHeaders* mutable_header() { return &header_; } 00137 const GoogleString& body() const { return body_; } 00138 void set_success(bool success) { success_ = success; } 00139 bool success() const { return success_; } 00140 00141 private: 00142 int64 last_modified_time_; 00143 GoogleString etag_; 00144 ResponseHeaders header_; 00145 GoogleString body_; 00146 bool success_; 00147 00148 DISALLOW_COPY_AND_ASSIGN(HttpResponse); 00149 }; 00150 typedef std::map<const GoogleString, HttpResponse*> ResponseMap; 00151 00152 ResponseMap response_map_; 00153 00154 bool enabled_; 00155 bool fail_on_unexpected_; 00156 bool update_date_headers_; 00157 bool omit_empty_writes_; 00158 bool fail_after_headers_; 00159 bool verify_host_header_; 00160 bool split_writes_; 00161 Timer* timer_; 00162 00163 DISALLOW_COPY_AND_ASSIGN(MockUrlFetcher); 00164 }; 00165 00166 } 00167 00168 #endif ///< NET_INSTAWEB_HTTP_PUBLIC_MOCK_URL_FETCHER_H_