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