00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00018
00020
00021 #ifndef NET_INSTAWEB_HTTP_PUBLIC_FETCHER_TEST_H_
00022 #define NET_INSTAWEB_HTTP_PUBLIC_FETCHER_TEST_H_
00023
00024 #include <utility>
00025 #include <vector>
00026 #include "net/instaweb/util/public/basictypes.h"
00027 #include "base/logging.h"
00028 #include "net/instaweb/http/public/response_headers.h"
00029 #include "net/instaweb/http/public/url_async_fetcher.h"
00030 #include "net/instaweb/http/public/url_fetcher.h"
00031 #include "net/instaweb/util/public/google_message_handler.h"
00032 #include "net/instaweb/util/public/gtest.h"
00033 #include "net/instaweb/util/public/string.h"
00034 #include "net/instaweb/util/public/string_util.h"
00035 #include "net/instaweb/util/public/string_writer.h"
00036
00037 namespace net_instaweb {
00038
00039 class MessageHandler;
00040 class RequestHeaders;
00041 class SimpleStats;
00042 class Writer;
00043
00044 class FetcherTest : public testing::Test {
00045 protected:
00046 static const char kStartDate[];
00047 static const char kHtmlContent[];
00048 static const char kGoodUrl[];
00049 static const char kNotCachedUrl[];
00050 static const char kBadUrl[];
00051 static const char kHeaderName[];
00052 static const char kHeaderValue[];
00053 static const char kErrorMessage[];
00054
00055 FetcherTest() : mock_async_fetcher_(&mock_fetcher_) {}
00056
00057 static void SetUpTestCase();
00058 static void TearDownTestCase();
00059
00061
00064 class MockFetcher : public UrlFetcher {
00065 public:
00066 MockFetcher() : num_fetches_(0) {}
00067
00068
00069 virtual bool StreamingFetchUrl(const GoogleString& url,
00070 const RequestHeaders& request_headers,
00071 ResponseHeaders* response_headers,
00072 Writer* response_writer,
00073 MessageHandler* message_handler);
00074
00075 int num_fetches() const { return num_fetches_; }
00076
00077 private:
00078 bool Populate(const char* cache_control, ResponseHeaders* response_headers,
00079 Writer* writer, MessageHandler* message_handler);
00080
00081 int num_fetches_;
00082
00083 DISALLOW_COPY_AND_ASSIGN(MockFetcher);
00084 };
00085
00089 class MockAsyncFetcher : public UrlAsyncFetcher {
00090 public:
00091 explicit MockAsyncFetcher(UrlFetcher* url_fetcher)
00092 : url_fetcher_(url_fetcher) {}
00093
00094 virtual bool StreamingFetch(const GoogleString& url,
00095 const RequestHeaders& request_headers,
00096 ResponseHeaders* response_headers,
00097 Writer* response_writer,
00098 MessageHandler* handler,
00099 Callback* callback);
00100
00101 void CallCallbacks();
00102
00103 private:
00104 UrlFetcher* url_fetcher_;
00105 std::vector<std::pair<bool, Callback*> > deferred_callbacks_;
00106
00107 DISALLOW_COPY_AND_ASSIGN(MockAsyncFetcher);
00108 };
00109
00112 class CheckCallback : public UrlAsyncFetcher::Callback {
00113 public:
00114 explicit CheckCallback(bool expect_success, bool* callback_called)
00115 : expect_success_(expect_success),
00116 content_writer_(&content_),
00117 callback_called_(callback_called) {
00118 }
00119
00120 virtual void Done(bool success) {
00121 *callback_called_ = true;
00122 CHECK_EQ(expect_success_, success);
00123 ValidateMockFetcherResponse(success, true, content_, response_headers_);
00124 delete this;
00125 }
00126
00127 bool expect_success_;
00128 ResponseHeaders response_headers_;
00129 GoogleString content_;
00130 StringWriter content_writer_;
00131 bool* callback_called_;
00132
00133 private:
00134 DISALLOW_COPY_AND_ASSIGN(CheckCallback);
00135 };
00136
00137 static void ValidateMockFetcherResponse(
00138 bool success, bool check_error_message, const GoogleString& content,
00139 const ResponseHeaders& response_headers);
00140
00144 int CountFetchesSync(const StringPiece& url, bool expect_success,
00145 bool check_error_message);
00147 int CountFetchesSync(const StringPiece& url, UrlFetcher* fetcher,
00148 bool expect_success, bool check_error_message);
00149
00153 int CountFetchesAsync(const StringPiece& url, bool expect_success,
00154 bool* callback_called);
00155
00160 virtual UrlFetcher* sync_fetcher() {
00161 LOG(FATAL) << "sync_fetcher() must be overridden before use.";
00162 return NULL;
00163 };
00164 virtual UrlAsyncFetcher* async_fetcher() {
00165 LOG(FATAL) << "async_fetcher() must be overridden before use.";
00166 return NULL;
00167 };
00168
00169
00170 GoogleString TestFilename() {
00171 return (GTestSrcDir() +
00172 "/net/instaweb/http/testdata/google.http");
00173 }
00174
00177 void ValidateOutput(const GoogleString& content,
00178 const ResponseHeaders& response_headers);
00179
00180 GoogleMessageHandler message_handler_;
00181 MockFetcher mock_fetcher_;
00182 MockAsyncFetcher mock_async_fetcher_;
00183 static SimpleStats* statistics_;
00184
00185 private:
00186 DISALLOW_COPY_AND_ASSIGN(FetcherTest);
00187 };
00188
00189 }
00190
00191 #endif ///< NET_INSTAWEB_HTTP_PUBLIC_FETCHER_TEST_H_