Page Speed Optimization Libraries
1.7.30.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 00020 00021 #ifndef NET_INSTAWEB_HTTP_PUBLIC_FETCHER_TEST_H_ 00022 #define NET_INSTAWEB_HTTP_PUBLIC_FETCHER_TEST_H_ 00023 00024 #include "base/logging.h" 00025 #include "net/instaweb/http/public/async_fetch.h" 00026 #include "net/instaweb/http/public/counting_url_async_fetcher.h" 00027 #include "net/instaweb/http/public/mock_url_fetcher.h" 00028 #include "net/instaweb/http/public/request_context.h" 00029 #include "net/instaweb/http/public/wait_url_async_fetcher.h" 00030 #include "net/instaweb/util/public/basictypes.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/scoped_ptr.h" 00034 #include "net/instaweb/util/public/string.h" 00035 #include "net/instaweb/util/public/string_util.h" 00036 #include "net/instaweb/util/public/thread_system.h" 00037 00038 namespace net_instaweb { 00039 00040 class ResponseHeaders; 00041 class SimpleStats; 00042 class UrlAsyncFetcher; 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(); 00056 00057 static void SetUpTestCase(); 00058 static void TearDownTestCase(); 00059 00061 00065 WaitUrlAsyncFetcher* wait_fetcher() { 00066 return &wait_url_async_fetcher_; 00067 } 00068 00069 CountingUrlAsyncFetcher* counting_fetcher() { return &counting_fetcher_; } 00070 00073 class CheckCallback : public StringAsyncFetch { 00074 public: 00075 CheckCallback(const RequestContextPtr& ctx, bool expect_success, 00076 bool check_error_message, 00077 bool* callback_called) 00078 : StringAsyncFetch(ctx), 00079 expect_success_(expect_success), 00080 check_error_message_(check_error_message), 00081 callback_called_(callback_called) { 00082 } 00083 00084 virtual void HandleDone(bool success) { 00085 *callback_called_ = true; 00086 CHECK_EQ(expect_success_, success); 00087 ValidateMockFetcherResponse( 00088 success, check_error_message_, buffer(), *response_headers()); 00089 delete this; 00090 } 00091 00092 private: 00093 bool expect_success_; 00094 bool check_error_message_; 00095 bool* callback_called_; 00096 00097 DISALLOW_COPY_AND_ASSIGN(CheckCallback); 00098 }; 00099 00101 static void ValidateMockFetcherResponse( 00102 bool success, bool check_error_message, const GoogleString& content, 00103 const ResponseHeaders& response_headers); 00104 00108 int CountFetchesAsync(const StringPiece& url, bool expect_success, 00109 bool* callback_called); 00110 00113 int CountFetchesAsync(const StringPiece& url, UrlAsyncFetcher* fetcher, 00114 bool expect_success, bool check_error_message, 00115 bool* callback_called); 00116 00120 virtual UrlAsyncFetcher* async_fetcher() { 00121 LOG(FATAL) << "async_fetcher() must be overridden before use."; 00122 return NULL; 00123 }; 00124 00125 GoogleString TestFilename() { 00126 return (GTestSrcDir() + 00127 "/net/instaweb/http/testdata/google.http"); 00128 } 00129 00132 void ValidateOutput(const GoogleString& content, 00133 const ResponseHeaders& response_headers); 00134 00135 GoogleMessageHandler message_handler_; 00136 MockUrlFetcher mock_fetcher_; 00137 WaitUrlAsyncFetcher wait_url_async_fetcher_; 00138 CountingUrlAsyncFetcher counting_fetcher_; 00139 scoped_ptr<ThreadSystem> thread_system_; 00140 static SimpleStats* statistics_; 00141 00142 private: 00143 void Populate(const char* cache_control, 00144 ResponseHeaders* response_headers, 00145 GoogleString* content); 00146 00147 DISALLOW_COPY_AND_ASSIGN(FetcherTest); 00148 }; 00149 00150 } 00151 00152 #endif ///< NET_INSTAWEB_HTTP_PUBLIC_FETCHER_TEST_H_