00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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 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(MockTimer* timer) { timer_ = timer; }
00113
00114 private:
00115 class HttpResponse {
00116 public:
00117 HttpResponse(int64 last_modified_time, const GoogleString& etag,
00118 const ResponseHeaders& in_header, const StringPiece& in_body)
00119 : last_modified_time_(last_modified_time),
00120 etag_(etag),
00121 body_(in_body.data(), in_body.size()),
00122 success_(true) {
00123 header_.CopyFrom(in_header);
00124 }
00125
00126 const int64 last_modified_time() const { return last_modified_time_; }
00127 const GoogleString& etag() const { return etag_; }
00128 const ResponseHeaders& header() const { return header_; }
00129 ResponseHeaders* mutable_header() { return &header_; }
00130 const GoogleString& body() const { return body_; }
00131 void set_success(bool success) { success_ = success; }
00132 bool success() const { return success_; }
00133
00134 private:
00135 int64 last_modified_time_;
00136 GoogleString etag_;
00137 ResponseHeaders header_;
00138 GoogleString body_;
00139 bool success_;
00140
00141 DISALLOW_COPY_AND_ASSIGN(HttpResponse);
00142 };
00143 typedef std::map<const GoogleString, HttpResponse*> ResponseMap;
00144
00145 ResponseMap response_map_;
00146
00147 bool enabled_;
00148 bool fail_on_unexpected_;
00149 bool update_date_headers_;
00150 bool omit_empty_writes_;
00151 bool fail_after_headers_;
00152 bool verify_host_header_;
00153
00154 MockTimer* timer_;
00155
00156 DISALLOW_COPY_AND_ASSIGN(MockUrlFetcher);
00157 };
00158
00159 }
00160
00161 #endif ///< NET_INSTAWEB_HTTP_PUBLIC_MOCK_URL_FETCHER_H_