proxygen
EchoHandlerTest.cpp
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  */
15 
16 using namespace EchoService;
17 using namespace proxygen;
18 using namespace testing;
19 
20 class MockEchoStats : public EchoStats {
21  public:
22  MOCK_METHOD0(recordRequest, void());
23  MOCK_METHOD0(getRequestCount, uint64_t());
24 };
25 
27  public:
28  void SetUp() override {
29  handler = new EchoHandler(&stats);
30  responseHandler = std::make_unique<MockResponseHandler>(handler);
31  handler->setResponseHandler(responseHandler.get());
32  }
33 
34  void TearDown() override {
35  Mock::VerifyAndClear(&stats);
36  Mock::VerifyAndClear(responseHandler.get());
37 
38  // Since there is no easy way to verify that handler has deleted
39  // itself, its advised to run test binary under AddressSanitzer
40  // to verify that.
41  }
42 
43  protected:
44  EchoHandler* handler{nullptr};
46  std::unique_ptr<MockResponseHandler> responseHandler;
47 };
48 
49 TEST_F(EchoHandlerFixture, OnProperRequestSendsResponse) {
50  EXPECT_CALL(stats, recordRequest()).WillOnce(Return());
51  EXPECT_CALL(stats, getRequestCount()).WillOnce(Return(5));
52 
53  HTTPMessage response;
54  EXPECT_CALL(*responseHandler, sendHeaders(_)).WillOnce(
55  DoAll(SaveArg<0>(&response), Return()));
56  EXPECT_CALL(*responseHandler, sendEOM()).WillOnce(Return());
57 
58  // Since we know we dont touch request, its ok to pass `nullptr` here.
59  handler->onRequest(nullptr);
60  handler->onEOM();
61  handler->requestComplete();
62 
63  EXPECT_EQ("5", response.getHeaders().getSingleOrEmpty("Request-Number"));
64  EXPECT_EQ(200, response.getStatusCode());
65 }
66 
67 TEST_F(EchoHandlerFixture, ReplaysBodyProperly) {
68  EXPECT_CALL(stats, recordRequest()).WillOnce(Return());
69  EXPECT_CALL(stats, getRequestCount()).WillOnce(Return(5));
70 
71  HTTPMessage response;
72  folly::fbstring body;
73 
74  EXPECT_CALL(*responseHandler, sendHeaders(_)).WillOnce(
75  DoAll(SaveArg<0>(&response), Return()));
76 
77  EXPECT_CALL(*responseHandler, sendBody(_)).WillRepeatedly(
78  DoAll(
79  Invoke([&] (std::shared_ptr<folly::IOBuf> b) {
80  body += b->moveToFbString();
81  }),
82  Return()));
83 
84  EXPECT_CALL(*responseHandler, sendEOM()).WillOnce(Return());
85 
86  // Since we know we dont touch request, its ok to pass `nullptr` here.
87  handler->onRequest(nullptr);
88  handler->onBody(folly::IOBuf::copyBuffer("part1"));
89  handler->onBody(folly::IOBuf::copyBuffer("part2"));
90  handler->onEOM();
91  handler->requestComplete();
92 
93  EXPECT_EQ("5", response.getHeaders().getSingleOrEmpty("Request-Number"));
94  EXPECT_EQ(200, response.getStatusCode());
95  EXPECT_EQ("part1part2", body);
96 }
char b
uint16_t getStatusCode() const
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void TearDown() override
void handler(int, siginfo_t *, void *)
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
void SetUp() override
std::unique_ptr< MockResponseHandler > responseHandler
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
const std::string & getSingleOrEmpty(const T &nameOrCode) const
Definition: HTTPHeaders.h:420
internal::DoBothAction< Action1, Action2 > DoAll(Action1 a1, Action2 a2)
#define EXPECT_CALL(obj, call)
StrictMock< MockEchoStats > stats
const internal::AnythingMatcher _
TEST_F(EchoHandlerFixture, OnProperRequestSendsResponse)
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
internal::ReturnAction< R > Return(R value)
#define MOCK_METHOD0(m,...)