proxygen
HTTPSessionMocks.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
10 #pragma once
11 
19 
20 #define GMOCK_NOEXCEPT_METHOD0(m, F) GMOCK_METHOD0_(, noexcept, , m, F)
21 #define GMOCK_NOEXCEPT_METHOD1(m, F) GMOCK_METHOD1_(, noexcept, , m, F)
22 
23 namespace proxygen {
24 
26  public:
28  }
30  : txn_(txn), msg_(msg) {
31  }
32 
33  void terminate() {
34  txn_->sendAbort();
35  }
36 
37  void sendRequest() {
39  }
40 
42  // this copies but it's test code meh
43  txn_->sendHeaders(req);
44  txn_->sendEOM();
45  }
46 
47  using HeaderMap = std::map<std::string, std::string>;
48  void sendHeaders(uint32_t code,
49  uint32_t content_length,
50  bool keepalive = true,
51  HeaderMap headers = HeaderMap()) {
52  HTTPMessage reply;
53  reply.setStatusCode(code);
54  reply.setHTTPVersion(1, 1);
55  reply.setWantsKeepalive(keepalive);
57  folly::to<std::string>(content_length));
58  for (auto& nv : headers) {
59  reply.getHeaders().add(nv.first, nv.second);
60  }
61  txn_->sendHeaders(reply);
62  }
63 
64  void sendReply() {
65  sendReplyCode(200);
66  }
67 
68  void sendReplyCode(uint32_t code) {
69  sendHeaders(code, 0, true);
70  txn_->sendEOM();
71  }
72 
73  void sendBody(uint32_t content_length) {
74  while (content_length > 0) {
75  uint32_t toSend = std::min(content_length, uint32_t(4096));
76  char buf[toSend];
77  memset(buf, 'a', toSend);
78  txn_->sendBody(folly::IOBuf::copyBuffer(buf, toSend));
79  content_length -= toSend;
80  }
81  }
82 
83  void sendBodyWithLastByteTracking(uint32_t content_length) {
85  sendBody(content_length);
86  }
87 
89  uint32_t content_length,
90  bool keepalive = true,
91  bool sendEOM = true,
92  bool hasTrailers = false) {
93  sendHeaders(code, content_length, keepalive);
94  sendBody(content_length);
95  if (hasTrailers) {
96  HTTPHeaders trailers;
97  trailers.add("X-Trailer1", "Foo");
98  txn_->sendTrailers(trailers);
99  }
100  if (sendEOM) {
101  txn_->sendEOM();
102  }
103  }
104 
105  void sendEOM() {
106  txn_->sendEOM();
107  }
108 
110  uint32_t content_length,
111  uint32_t chunkSize,
112  bool hasTrailers,
113  bool sendEOM = true) {
114  HTTPMessage reply;
115  reply.setStatusCode(code);
116  reply.setHTTPVersion(1, 1);
117  reply.setIsChunked(true);
118  txn_->sendHeaders(reply);
119  while (content_length > 0) {
120  uint32_t toSend = std::min(content_length, chunkSize);
121  char buf[toSend];
122  memset(buf, 'a', toSend);
123  txn_->sendChunkHeader(toSend);
124  txn_->sendBody(folly::IOBuf::copyBuffer(buf, toSend));
126  content_length -= toSend;
127  }
128  if (hasTrailers) {
129  HTTPHeaders trailers;
130  trailers.add("X-Trailer1", "Foo");
131  txn_->sendTrailers(trailers);
132  }
133  if (sendEOM) {
134  txn_->sendEOM();
135  }
136  }
137 
139  std::shared_ptr<HTTPMessage> msg_;
140 };
141 
143  : public HTTPHandlerBase
144  , public HTTPTransaction::Handler {
145  public:
147  }
149  HTTPMessage* msg,
150  const folly::SocketAddress&)
151  : HTTPHandlerBase(&txn, msg) {
152  }
153 
154  GMOCK_NOEXCEPT_METHOD1(setTransaction, void(HTTPTransaction* txn));
155 
156  GMOCK_NOEXCEPT_METHOD0(detachTransaction, void());
157 
158  void onHeadersComplete(std::unique_ptr<HTTPMessage> msg) noexcept override {
159  onHeadersComplete(std::shared_ptr<HTTPMessage>(msg.release()));
160  }
161 
162  GMOCK_NOEXCEPT_METHOD1(onHeadersComplete,
163  void(std::shared_ptr<HTTPMessage> msg));
164 
165  void onBody(std::unique_ptr<folly::IOBuf> chain) noexcept override {
166  onBody(std::shared_ptr<folly::IOBuf>(chain.release()));
167  }
168  GMOCK_NOEXCEPT_METHOD1(onBody, void(std::shared_ptr<folly::IOBuf> chain));
169 
170  GMOCK_NOEXCEPT_METHOD1(onChunkHeader, void(size_t length));
171 
172  GMOCK_NOEXCEPT_METHOD0(onChunkComplete, void());
173 
174  void onTrailers(std::unique_ptr<HTTPHeaders> trailers) noexcept override {
175  onTrailers(std::shared_ptr<HTTPHeaders>(trailers.release()));
176  }
177 
178  GMOCK_NOEXCEPT_METHOD1(onTrailers,
179  void(std::shared_ptr<HTTPHeaders> trailers));
180 
181  GMOCK_NOEXCEPT_METHOD0(onEOM, void());
182 
183  GMOCK_NOEXCEPT_METHOD1(onUpgrade, void(UpgradeProtocol protocol));
184 
185  GMOCK_NOEXCEPT_METHOD1(onError, void(const HTTPException& error));
186 
187  GMOCK_NOEXCEPT_METHOD1(onGoaway, void(ErrorCode));
188 
189  GMOCK_NOEXCEPT_METHOD0(onEgressPaused, void());
190 
191  GMOCK_NOEXCEPT_METHOD0(onEgressResumed, void());
192 
193  GMOCK_NOEXCEPT_METHOD1(onPushedTransaction, void(HTTPTransaction*));
194 
195  GMOCK_NOEXCEPT_METHOD1(onExTransaction, void(HTTPTransaction*));
196 
197  void expectTransaction(std::function<void(HTTPTransaction* txn)> callback) {
198  EXPECT_CALL(*this, setTransaction(testing::_))
199  .WillOnce(testing::Invoke(callback))
200  .RetiresOnSaturation();
201  }
202 
203  void expectTransaction(HTTPTransaction** pTxn = nullptr) {
204  EXPECT_CALL(*this, setTransaction(testing::_))
205  .WillOnce(testing::SaveArg<0>(pTxn ? pTxn : &txn_));
206  }
207 
208  void expectHeaders(std::function<void()> callback = std::function<void()>()) {
209  if (callback) {
210  EXPECT_CALL(*this, onHeadersComplete(testing::_))
211  .WillOnce(testing::InvokeWithoutArgs(callback))
212  .RetiresOnSaturation();
213  } else {
214  EXPECT_CALL(*this, onHeadersComplete(testing::_));
215  }
216  }
217 
218  void expectHeaders(std::function<void(std::shared_ptr<HTTPMessage>)> cb) {
219  EXPECT_CALL(*this, onHeadersComplete(testing::_))
220  .WillOnce(testing::Invoke(cb))
221  .RetiresOnSaturation();
222  }
223 
225  std::function<void(std::shared_ptr<HTTPHeaders> trailers)> cb) {
226  EXPECT_CALL(*this, onTrailers(testing::_))
227  .WillOnce(testing::Invoke(cb))
228  .RetiresOnSaturation();
229  }
230 
232  std::function<void()> callback = std::function<void()>()) {
233  if (callback) {
234  EXPECT_CALL(*this, onChunkHeader(testing::_))
235  .WillOnce(testing::InvokeWithoutArgs(callback));
236  } else {
237  EXPECT_CALL(*this, onChunkHeader(testing::_));
238  }
239  }
240 
241  void expectBody(std::function<void()> callback = std::function<void()>()) {
242  if (callback) {
243  EXPECT_CALL(*this, onBody(testing::_))
244  .WillOnce(testing::InvokeWithoutArgs(callback));
245  } else {
246  EXPECT_CALL(*this, onBody(testing::_));
247  }
248  }
249 
250  void expectBody(std::function<void(std::shared_ptr<folly::IOBuf>)> callback) {
251  EXPECT_CALL(*this, onBody(testing::_)).WillOnce(testing::Invoke(callback));
252  }
253 
255  std::function<void()> callback = std::function<void()>()) {
256  if (callback) {
257  EXPECT_CALL(*this, onChunkComplete())
258  .WillOnce(testing::InvokeWithoutArgs(callback));
259  } else {
260  EXPECT_CALL(*this, onChunkComplete());
261  }
262  }
263 
264  void expectEOM(std::function<void()> callback = std::function<void()>()) {
265  if (callback) {
266  EXPECT_CALL(*this, onEOM()).WillOnce(testing::Invoke(callback));
267  } else {
268  EXPECT_CALL(*this, onEOM());
269  }
270  }
271 
273  std::function<void()> callback = std::function<void()>()) {
274  if (callback) {
275  EXPECT_CALL(*this, onEgressPaused()).WillOnce(testing::Invoke(callback));
276  } else {
277  EXPECT_CALL(*this, onEgressPaused());
278  }
279  }
280 
282  std::function<void()> callback = std::function<void()>()) {
283  if (callback) {
284  EXPECT_CALL(*this, onEgressResumed()).WillOnce(testing::Invoke(callback));
285  } else {
286  EXPECT_CALL(*this, onEgressResumed());
287  }
288  }
289 
290  void expectError(std::function<void(const HTTPException& ex)> callback =
291  std::function<void(const HTTPException& ex)>()) {
292  if (callback) {
293  EXPECT_CALL(*this, onError(testing::_))
294  .WillOnce(testing::Invoke(callback));
295  } else {
296  EXPECT_CALL(*this, onError(testing::_));
297  }
298  }
299 
300  void expectGoaway(std::function<void(ErrorCode)> callback =
301  std::function<void(ErrorCode)>()) {
302  if (callback) {
303  EXPECT_CALL(*this, onGoaway(testing::_))
304  .WillOnce(testing::Invoke(callback));
305  } else {
306  EXPECT_CALL(*this, onGoaway(testing::_));
307  }
308  }
309 
311  std::function<void()> callback = std::function<void()>()) {
312  if (callback) {
313  EXPECT_CALL(*this, detachTransaction())
314  .WillOnce(testing::Invoke(callback));
315  } else {
316  EXPECT_CALL(*this, detachTransaction());
317  }
318  }
319 };
320 
322  : public HTTPHandlerBase
324  public:
326  }
328  HTTPMessage* msg,
329  const folly::SocketAddress&)
330  : HTTPHandlerBase(&txn, msg) {
331  }
332 
333  GMOCK_NOEXCEPT_METHOD1(setTransaction, void(HTTPTransaction* txn));
334 
335  GMOCK_NOEXCEPT_METHOD0(detachTransaction, void());
336 
337  GMOCK_NOEXCEPT_METHOD1(onError, void(const HTTPException& error));
338 
339  GMOCK_NOEXCEPT_METHOD1(onGoaway, void(ErrorCode));
340 
341  GMOCK_NOEXCEPT_METHOD0(onEgressPaused, void());
342 
343  GMOCK_NOEXCEPT_METHOD0(onEgressResumed, void());
344 
345  void sendPushHeaders(const std::string& path,
346  const std::string& host,
347  uint32_t content_length,
348  http2::PriorityUpdate pri) {
349  HTTPMessage push;
350  push.setURL(path);
351  push.getHeaders().set(HTTP_HEADER_HOST, host);
353  folly::to<std::string>(content_length));
354  push.setHTTP2Priority(
356  txn_->sendHeaders(push);
357  }
358 };
359 
361  public:
362  MOCK_METHOD2(getRequestHandler,
364 
365  MOCK_METHOD3(getParseErrorHandler,
367  const HTTPException&,
368  const folly::SocketAddress&));
369 
370  MOCK_METHOD2(getTransactionTimeoutHandler,
372  const folly::SocketAddress&));
373 
374  MOCK_METHOD1(attachSession, void(HTTPSessionBase*));
375  MOCK_METHOD1(detachSession, void(const HTTPSessionBase*));
376  MOCK_METHOD1(onSessionCodecChange, void(HTTPSessionBase*));
377  MOCK_CONST_METHOD0(getGracefulShutdownTimeout, std::chrono::milliseconds());
378 
379  MOCK_CONST_METHOD0(getHeaderIndexingStrategy,
380  const HeaderIndexingStrategy*());
381 };
382 
384  public:
385  MOCK_METHOD1(attachSession, void(HTTPSessionBase*));
386  MOCK_METHOD1(detachSession, void(const HTTPSessionBase*));
387  MOCK_METHOD1(onSessionCodecChange, void(HTTPSessionBase*));
388 
389  MOCK_CONST_METHOD0(getHeaderIndexingStrategy,
390  const HeaderIndexingStrategy*());
391 };
392 
393 ACTION_P(ExpectString, expected) {
394  std::string bodystr((const char*)arg0->data(), arg0->length());
395  EXPECT_EQ(bodystr, expected);
396 }
397 
398 ACTION_P(ExpectBodyLen, expectedLen) {
399  EXPECT_EQ(arg1->computeChainDataLength(), expectedLen);
400 }
401 
403  public:
404  MOCK_METHOD1(onCreate, void(const HTTPSessionBase&));
405  MOCK_METHOD2(onIngressError, void(const HTTPSessionBase&, ProxygenError));
406  MOCK_METHOD0(onIngressEOF, void());
407  MOCK_METHOD2(onRead, void(const HTTPSessionBase&, size_t));
408  MOCK_METHOD2(onWrite, void(const HTTPSessionBase&, size_t));
409  MOCK_METHOD1(onRequestBegin, void(const HTTPSessionBase&));
410  MOCK_METHOD2(onRequestEnd, void(const HTTPSessionBase&, uint32_t));
411  MOCK_METHOD1(onActivateConnection, void(const HTTPSessionBase&));
412  MOCK_METHOD1(onDeactivateConnection, void(const HTTPSessionBase&));
413  MOCK_METHOD1(onDestroy, void(const HTTPSessionBase&));
414  MOCK_METHOD2(onIngressMessage,
415  void(const HTTPSessionBase&, const HTTPMessage&));
416  MOCK_METHOD1(onIngressLimitExceeded, void(const HTTPSessionBase&));
417  MOCK_METHOD1(onIngressPaused, void(const HTTPSessionBase&));
418  MOCK_METHOD1(onTransactionDetached, void(const HTTPSessionBase&));
419  MOCK_METHOD1(onPingReplySent, void(int64_t));
420  MOCK_METHOD0(onPingReplyReceived, void());
421  MOCK_METHOD1(onSettingsOutgoingStreamsFull, void(const HTTPSessionBase&));
422  MOCK_METHOD1(onSettingsOutgoingStreamsNotFull, void(const HTTPSessionBase&));
423  MOCK_METHOD1(onFlowControlWindowClosed, void(const HTTPSessionBase&));
424  MOCK_METHOD1(onEgressBuffered, void(const HTTPSessionBase&));
425  MOCK_METHOD1(onEgressBufferCleared, void(const HTTPSessionBase&));
426  MOCK_METHOD2(onSettings, void(const HTTPSessionBase&, const SettingsList&));
427  MOCK_METHOD1(onSettingsAck, void(const HTTPSessionBase&));
428 };
429 
431  public:
435  void recordSessionReused() noexcept override{};
436  // virtual void recordSessionIdleTime(std::chrono::seconds) noexcept {};
438  void recordSessionStalled() noexcept override{};
439 
440  void recordTTLBAExceedLimit() noexcept override{};
442  void recordTTLBANotFound() noexcept override{};
443  void recordTTLBAReceived() noexcept override{};
444  void recordTTLBATimeout() noexcept override{};
445  void recordTTLBAEomPassed() noexcept override{};
446  void recordTTLBATracked() noexcept override{};
447 };
448 
450  public:
452  }
453  GMOCK_NOEXCEPT_METHOD0(recordTransactionOpened, void());
454  GMOCK_NOEXCEPT_METHOD0(recordTransactionClosed, void());
455  GMOCK_NOEXCEPT_METHOD1(recordTransactionsServed, void(uint64_t));
456  GMOCK_NOEXCEPT_METHOD0(recordSessionReused, void());
457  GMOCK_NOEXCEPT_METHOD1(recordSessionIdleTime, void(std::chrono::seconds));
458  GMOCK_NOEXCEPT_METHOD0(recordTransactionStalled, void());
459  GMOCK_NOEXCEPT_METHOD0(recordSessionStalled, void());
460 };
461 
462 } // namespace proxygen
void recordTTLBATimeout() noexceptoverride
std::shared_ptr< HTTPMessage > msg_
void setWantsKeepalive(bool wantsKeepaliveVal)
Definition: HTTPMessage.h:343
void sendReplyCode(uint32_t code)
virtual void sendTrailers(const HTTPHeaders &trailers)
void setLastByteFlushedTrackingEnabled(bool enabled)
void onHeadersComplete(std::unique_ptr< HTTPMessage > msg) noexceptoverride
MockHTTPPushHandler(HTTPTransaction &txn, HTTPMessage *msg, const folly::SocketAddress &)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void recordTTLBAEomPassed() noexceptoverride
void sendBodyWithLastByteTracking(uint32_t content_length)
void sendChunkedReplyWithBody(uint32_t code, uint32_t content_length, uint32_t chunkSize, bool hasTrailers, bool sendEOM=true)
#define GMOCK_NOEXCEPT_METHOD0(m, F)
void expectEgressPaused(std::function< void()> callback=std::function< void()>())
void sendReplyWithBody(uint32_t code, uint32_t content_length, bool keepalive=true, bool sendEOM=true, bool hasTrailers=false)
void recordTTLBATracked() noexceptoverride
void recordTTLBAExceedLimit() noexceptoverride
void expectError(std::function< void(const HTTPException &ex)> callback=std::function< void(const HTTPException &ex)>())
requires E e noexcept(noexcept(s.error(std::move(e))))
void recordTTLBANotFound() noexceptoverride
requires And< SemiMovable< VN >... > &&SemiMovable< E > auto error(E e)
Definition: error.h:48
tuple make_tuple()
Definition: gtest-tuple.h:675
PolymorphicAction< internal::InvokeWithoutArgsAction< FunctionImpl > > InvokeWithoutArgs(FunctionImpl function_impl)
#define GMOCK_NOEXCEPT_METHOD1(m, F)
std::map< std::string, std::string > HeaderMap
void expectChunkComplete(std::function< void()> callback=std::function< void()>())
#define MOCK_METHOD3(m,...)
void setIsChunked(bool chunked)
Definition: HTTPMessage.h:79
void set(folly::StringPiece name, const std::string &value)
Definition: HTTPHeaders.h:119
ParseURL setURL(T &&url)
Definition: HTTPMessage.h:183
LogLevel min
Definition: LogLevel.cpp:30
ACTION_P(ExpectString, expected)
void setHTTP2Priority(HTTPPriority h2Pri)
Definition: HTTPMessage.h:599
void recordTransactionsServed(uint64_t) noexceptoverride
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
void expectTransaction(HTTPTransaction **pTxn=nullptr)
void sendRequest(HTTPMessage req)
void sendHeaders(uint32_t code, uint32_t content_length, bool keepalive=true, HeaderMap headers=HeaderMap())
void expectEOM(std::function< void()> callback=std::function< void()>())
void expectEgressResumed(std::function< void()> callback=std::function< void()>())
void recordTransactionOpened() noexceptoverride
virtual void sendBody(std::unique_ptr< folly::IOBuf > body)
MockHTTPHandler(HTTPTransaction &txn, HTTPMessage *msg, const folly::SocketAddress &)
virtual void sendHeaders(const HTTPMessage &headers)
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
void expectDetachTransaction(std::function< void()> callback=std::function< void()>())
void sendBody(uint32_t content_length)
void expectTransaction(std::function< void(HTTPTransaction *txn)> callback)
void expectHeaders(std::function< void()> callback=std::function< void()>())
void expectTrailers(std::function< void(std::shared_ptr< HTTPHeaders > trailers)> cb)
void recordSessionStalled() noexceptoverride
std::vector< HTTPSetting > SettingsList
Definition: HTTPSettings.h:81
void recordTransactionClosed() noexceptoverride
void expectGoaway(std::function< void(ErrorCode)> callback=std::function< void(ErrorCode)>())
void expectChunkHeader(std::function< void()> callback=std::function< void()>())
#define MOCK_METHOD1(m,...)
void sendPushHeaders(const std::string &path, const std::string &host, uint32_t content_length, http2::PriorityUpdate pri)
HTTPMessage getGetRequest(const std::string &url)
Definition: TestUtils.cpp:76
void recordTransactionStalled() noexceptoverride
const char * string
Definition: Conv.cpp:212
void expectBody(std::function< void(std::shared_ptr< folly::IOBuf >)> callback)
#define MOCK_CONST_METHOD0(m,...)
virtual void sendChunkTerminator()
void setHTTPVersion(uint8_t major, uint8_t minor)
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
void recordTTLBAIOBSplitByEom() noexceptoverride
void recordTTLBAReceived() noexceptoverride
void onTrailers(std::unique_ptr< HTTPHeaders > trailers) noexceptoverride
HTTPHandlerBase(HTTPTransaction *txn, HTTPMessage *msg)
void onBody(std::unique_ptr< folly::IOBuf > chain) noexceptoverride
void recordSessionReused() noexceptoverride
static std::unique_ptr< IOBuf > copyBuffer(const void *buf, std::size_t size, std::size_t headroom=0, std::size_t minTailroom=0)
Definition: IOBuf.h:1587
#define MOCK_METHOD2(m,...)
void expectBody(std::function< void()> callback=std::function< void()>())
virtual void sendChunkHeader(size_t length)
void expectHeaders(std::function< void(std::shared_ptr< HTTPMessage >)> cb)
#define MOCK_METHOD0(m,...)
void setStatusCode(uint16_t status)