Page Speed Optimization Libraries
1.2.24.1
|
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 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 00027 #include "base/logging.h" 00028 #include "net/instaweb/http/public/async_fetch.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/basictypes.h" 00032 #include "net/instaweb/util/public/google_message_handler.h" 00033 #include "net/instaweb/util/public/gtest.h" 00034 #include "net/instaweb/util/public/string.h" 00035 #include "net/instaweb/util/public/string_util.h" 00036 00037 namespace net_instaweb { 00038 00039 class MessageHandler; 00040 class RequestHeaders; 00041 class ResponseHeaders; 00042 class SimpleStats; 00043 class Writer; 00044 00045 class FetcherTest : public testing::Test { 00046 protected: 00047 static const char kStartDate[]; 00048 static const char kHtmlContent[]; 00049 static const char kGoodUrl[]; 00050 static const char kNotCachedUrl[]; 00051 static const char kBadUrl[]; 00052 static const char kHeaderName[]; 00053 static const char kHeaderValue[]; 00054 static const char kErrorMessage[]; 00055 00056 FetcherTest() : mock_async_fetcher_(&mock_fetcher_) {} 00057 00058 static void SetUpTestCase(); 00059 static void TearDownTestCase(); 00060 00062 00065 class MockFetcher : public UrlFetcher { 00066 public: 00067 MockFetcher() : num_fetches_(0) {} 00068 00069 00070 virtual bool StreamingFetchUrl(const GoogleString& url, 00071 const RequestHeaders& request_headers, 00072 ResponseHeaders* response_headers, 00073 Writer* response_writer, 00074 MessageHandler* message_handler); 00075 00076 int num_fetches() const { return num_fetches_; } 00077 00078 private: 00079 bool Populate(const char* cache_control, ResponseHeaders* response_headers, 00080 Writer* writer, MessageHandler* message_handler); 00081 00082 int num_fetches_; 00083 00084 DISALLOW_COPY_AND_ASSIGN(MockFetcher); 00085 }; 00086 00090 class MockAsyncFetcher : public UrlAsyncFetcher { 00091 public: 00092 explicit MockAsyncFetcher(UrlFetcher* url_fetcher) 00093 : url_fetcher_(url_fetcher) {} 00094 00095 virtual void Fetch(const GoogleString& url, 00096 MessageHandler* handler, 00097 AsyncFetch* fetch); 00098 00099 void CallCallbacks(); 00100 00101 private: 00102 UrlFetcher* url_fetcher_; 00103 std::vector<std::pair<bool, AsyncFetch*> > deferred_callbacks_; 00104 00105 DISALLOW_COPY_AND_ASSIGN(MockAsyncFetcher); 00106 }; 00107 00110 class CheckCallback : public StringAsyncFetch { 00111 public: 00112 explicit CheckCallback(bool expect_success, bool* callback_called) 00113 : expect_success_(expect_success), callback_called_(callback_called) { 00114 } 00115 00116 virtual void HandleDone(bool success) { 00117 *callback_called_ = true; 00118 CHECK_EQ(expect_success_, success); 00119 ValidateMockFetcherResponse(success, true, buffer(), *response_headers()); 00120 delete this; 00121 } 00122 00123 bool expect_success_; 00124 bool* callback_called_; 00125 00126 private: 00127 DISALLOW_COPY_AND_ASSIGN(CheckCallback); 00128 }; 00129 00130 static void ValidateMockFetcherResponse( 00131 bool success, bool check_error_message, const GoogleString& content, 00132 const ResponseHeaders& response_headers); 00133 00137 int CountFetchesSync(const StringPiece& url, bool expect_success, 00138 bool check_error_message); 00140 int CountFetchesSync(const StringPiece& url, UrlFetcher* fetcher, 00141 bool expect_success, bool check_error_message); 00142 00146 int CountFetchesAsync(const StringPiece& url, bool expect_success, 00147 bool* callback_called); 00148 00153 virtual UrlFetcher* sync_fetcher() { 00154 LOG(FATAL) << "sync_fetcher() must be overridden before use."; 00155 return NULL; 00156 }; 00157 virtual UrlAsyncFetcher* async_fetcher() { 00158 LOG(FATAL) << "async_fetcher() must be overridden before use."; 00159 return NULL; 00160 }; 00161 00162 00163 GoogleString TestFilename() { 00164 return (GTestSrcDir() + 00165 "/net/instaweb/http/testdata/google.http"); 00166 } 00167 00170 void ValidateOutput(const GoogleString& content, 00171 const ResponseHeaders& response_headers); 00172 00173 GoogleMessageHandler message_handler_; 00174 MockFetcher mock_fetcher_; 00175 MockAsyncFetcher mock_async_fetcher_; 00176 static SimpleStats* statistics_; 00177 00178 private: 00179 DISALLOW_COPY_AND_ASSIGN(FetcherTest); 00180 }; 00181 00182 } 00183 00184 #endif ///< NET_INSTAWEB_HTTP_PUBLIC_FETCHER_TEST_H_