Google APIs Client Library for C++
|
00001 /* 00002 * \copyright Copyright 2013 Google Inc. All Rights Reserved. 00003 * \license @{ 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 * 00017 * @} 00018 */ 00019 00020 /* 00021 * @defgroup TransportLayerTesting Transport Layer - Testing Support 00022 * 00023 * The Transport Layer Testing Support module contains classes and components 00024 * to facilitate testing, debugging, and diagnosing the transport layer. 00025 * In many cases these components can be used in place of normal production 00026 * components to test applications and libraries that use the transport layer. 00027 * 00028 * Components in this module are not typically deployed into production 00029 * applications as a matter of policy. There are not generally technical 00030 * reasons to prevent their inclusion. 00031 */ 00032 #ifndef APISERVING_CLIENTS_CPP_TRANSPORT_MOCK_HTTP_TRANSPORT_H_ 00033 #define APISERVING_CLIENTS_CPP_TRANSPORT_MOCK_HTTP_TRANSPORT_H_ 00034 00035 #include <string> 00036 using std::string; 00037 #include "googleapis/client/data/data_reader.h" 00038 #include "googleapis/client/data/data_writer.h" 00039 #include "googleapis/client/transport/http_authorization.h" 00040 #include "googleapis/client/transport/http_transport.h" 00041 #include "googleapis/client/transport/http_request.h" 00042 #include "googleapis/strings/stringpiece.h" 00043 #include <gmock/gmock.h> 00044 namespace googleapis { 00045 00046 namespace client { 00047 00048 /* 00049 * Mock transport error handler for testing with GMock. 00050 * @ingroup TransportLayerTesting 00051 */ 00052 class MockHttpTransportErrorHandler : public HttpTransportErrorHandler { 00053 public: 00054 MOCK_CONST_METHOD2(HandleTransportError, bool(int, HttpRequest*)); // NOLINT 00055 MOCK_CONST_METHOD2(HandleRedirect, bool(int, HttpRequest*)); // NOLINT 00056 MOCK_CONST_METHOD2(HandleHttpError, bool(int, HttpRequest*)); // NOLINT 00057 }; 00058 00059 /* 00060 * Mock authorization credential for testing with GMock. 00061 * @ingroup TransportLayerTesting 00062 */ 00063 class MockAuthorizationCredential : public AuthorizationCredential { 00064 public: 00065 MOCK_CONST_METHOD0(type, const StringPiece()); 00066 MOCK_METHOD1(AuthorizeRequest, util::Status(HttpRequest* request)); 00067 MOCK_METHOD0(Refresh, util::Status()); 00068 MOCK_METHOD1(Load, util::Status(DataReader* reader)); 00069 MOCK_CONST_METHOD0(MakeDataReader, DataReader*()); 00070 }; 00071 00072 /* 00073 * Mock http request for testing GMock. 00074 * @ingroup TransportLayerTesting 00075 */ 00076 class MockHttpRequest : public HttpRequest { 00077 public: 00078 typedef Callback1<HttpResponse*> DoExecuteCallback; 00079 00080 MockHttpRequest(HttpRequest::HttpMethod method, HttpTransport* transport) 00081 : HttpRequest(method, transport) {} 00082 MOCK_METHOD1(DoExecute, void(HttpResponse* response)); 00083 00084 void poke_http_code(int code) { 00085 response()->set_http_code(code); 00086 } 00087 00088 void poke_transport_status(util::Status status) { 00089 mutable_state()->set_transport_status(status); 00090 } 00091 00092 void poke_response_header( 00093 const StringPiece name, const StringPiece value) { 00094 response()->AddHeader(name, value); 00095 } 00096 00097 void poke_response_body(const string& body) { 00098 EXPECT_TRUE(response()->body_writer()->Write(body).ok()); 00099 } 00100 00101 /* 00102 * Testing convienence method for checking the content_reader() value. 00103 * 00104 * This method will rewind the reader if it had already been called so 00105 * is safe to grab multiple values. 00106 * 00107 * @return content_reader payload value as a string. 00108 */ 00109 const string content_as_string() { 00110 DataReader* reader = content_reader(); 00111 CHECK(reader); 00112 if (reader->offset()) { 00113 EXPECT_TRUE(reader->Reset()); 00114 } 00115 return reader->RemainderToString(); 00116 } 00117 00118 /* 00119 * Testing convienence method for checking the response body_reader() value. 00120 * 00121 * This method will rewind the reader if it had already been called so 00122 * is safe to grab multiple values. 00123 * 00124 * @return response_body payload value as a string. 00125 */ 00126 const string response_body_as_string() { 00127 CHECK(response() != NULL); 00128 DataReader* reader = response()->body_reader(); 00129 CHECK(reader != NULL); 00130 if (!reader->offset()) { 00131 EXPECT_TRUE(reader->Reset()); 00132 } 00133 return reader->RemainderToString(); 00134 } 00135 00136 /* 00137 * Testing convienence method added to check header values. 00138 * 00139 * Fails if the header was not present or value does not match. 00140 * 00141 * @param[in] name Header to check 00142 * @param[in] value Value to confirm 00143 */ 00144 void CheckHeader(const StringPiece& name, const string& value) { 00145 const string* have = FindHeaderValue(name); 00146 EXPECT_TRUE(have != NULL) << "Did not find header=" << name; 00147 if (have && !value.empty()) { 00148 EXPECT_EQ(value, *have) << "header=" << name; 00149 } 00150 } 00151 }; 00152 00153 /* 00154 * Mock http request for testing GMock. 00155 * @ingroup TransportLayerTesting 00156 */ 00157 class MockHttpTransport : public HttpTransport { 00158 public: 00159 MockHttpTransport() : HttpTransport(HttpTransportOptions()) { 00160 set_id("MockHttpTransport"); 00161 } 00162 explicit MockHttpTransport( 00163 const client::HttpTransportOptions& options) 00164 : HttpTransport(options) { 00165 } 00166 00167 MOCK_METHOD1( 00168 NewHttpRequest, HttpRequest*(const HttpRequest::HttpMethod& method)); 00169 }; 00170 00171 /* 00172 * Mock http transport factory for testing GMock. 00173 * @ingroup TransportLayerTesting 00174 */ 00175 class MockHttpTransportFactory : public HttpTransportFactory { 00176 public: 00177 MockHttpTransportFactory() { 00178 set_default_id("MockHttpTransport"); 00179 } 00180 explicit MockHttpTransportFactory(const HttpTransportLayerConfig* config) 00181 : HttpTransportFactory(config) { 00182 set_default_id("MockHttpTransport"); 00183 } 00184 MOCK_METHOD1(DoAlloc, HttpTransport*(const HttpTransportOptions& options)); 00185 }; 00186 00187 } // namespace client 00188 00189 } // namespace googleapis 00190 #endif // APISERVING_CLIENTS_CPP_TRANSPORT_MOCK_HTTP_TRANSPORT_H_