proxygen
ZlibServerFilterTest Class Reference
Inheritance diagram for ZlibServerFilterTest:
testing::Test

Public Member Functions

void SetUp () override
 
void TearDown () override
 
- Public Member Functions inherited from testing::Test
virtual ~Test ()
 
virtual ~Test ()
 
virtual ~Test ()
 

Protected Member Functions

void exercise_compression (bool expectCompression, std::string url, std::string acceptedEncoding, std::string expectedEncoding, std::string originalRequestBody, std::string responseContentType, std::unique_ptr< folly::IOBuf > originalResponseBody, int32_t compressionLevel=4, uint32_t minimumCompressionSize=1)
 
std::unique_ptr< folly::IOBufcreateResponseChain (std::vector< std::string > const &bodyStrings)
 
- Protected Member Functions inherited from testing::Test
 Test ()
 
 Test ()
 
 Test ()
 

Protected Attributes

ZlibServerFilterfilter_ {nullptr}
 
MockRequestHandlerrequestHandler_
 
std::unique_ptr< MockResponseHandlerresponseHandler_
 
std::unique_ptr< ZlibStreamDecompressorzd_
 
ResponseHandlerdownstream_ {nullptr}
 

Additional Inherited Members

- Public Types inherited from testing::Test
typedef internal::SetUpTestCaseFunc SetUpTestCaseFunc
 
typedef internal::TearDownTestCaseFunc TearDownTestCaseFunc
 
typedef internal::SetUpTestCaseFunc SetUpTestCaseFunc
 
typedef internal::TearDownTestCaseFunc TearDownTestCaseFunc
 
typedef internal::SetUpTestCaseFunc SetUpTestCaseFunc
 
typedef internal::TearDownTestCaseFunc TearDownTestCaseFunc
 
- Static Public Member Functions inherited from testing::Test
static void SetUpTestCase ()
 
static void TearDownTestCase ()
 
static bool HasFatalFailure ()
 
static bool HasNonfatalFailure ()
 
static bool HasFailure ()
 
static void RecordProperty (const std::string &key, const std::string &value)
 
static void RecordProperty (const std::string &key, int value)
 
static void SetUpTestCase ()
 
static void TearDownTestCase ()
 
static bool HasFatalFailure ()
 
static bool HasNonfatalFailure ()
 
static bool HasFailure ()
 
static void RecordProperty (const std::string &key, const std::string &value)
 
static void RecordProperty (const std::string &key, int value)
 
static void SetUpTestCase ()
 
static void TearDownTestCase ()
 
static bool HasFatalFailure ()
 
static bool HasNonfatalFailure ()
 
static bool HasFailure ()
 
static void RecordProperty (const std::string &key, const std::string &value)
 
static void RecordProperty (const std::string &key, int value)
 

Detailed Description

Definition at line 35 of file ZlibServerFilterTest.cpp.

Member Function Documentation

std::unique_ptr<folly::IOBuf> ZlibServerFilterTest::createResponseChain ( std::vector< std::string > const &  bodyStrings)
inlineprotected

Definition at line 193 of file ZlibServerFilterTest.cpp.

References folly::IOBuf::copyBuffer(), folly::gen::move, folly::IOBuf::prependChain(), and s.

194  {
195 
196  std::unique_ptr<folly::IOBuf> responseBodyChain;
197 
198  for (auto& s : bodyStrings) {
199  auto nextBody = folly::IOBuf::copyBuffer(s.c_str());
200  if (responseBodyChain) {
201  responseBodyChain->prependChain(std::move(nextBody));
202  } else {
203  responseBodyChain = std::move(nextBody);
204  }
205  }
206 
207  return responseBodyChain;
208  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void prependChain(std::unique_ptr< IOBuf > &&iobuf)
Definition: IOBuf.cpp:509
static set< string > s
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
void ZlibServerFilterTest::exercise_compression ( bool  expectCompression,
std::string  url,
std::string  acceptedEncoding,
std::string  expectedEncoding,
std::string  originalRequestBody,
std::string  responseContentType,
std::unique_ptr< folly::IOBuf originalResponseBody,
int32_t  compressionLevel = 4,
uint32_t  minimumCompressionSize = 1 
)
inlineprotected

Definition at line 58 of file ZlibServerFilterTest.cpp.

References testing::_, ASSERT_FALSE, proxygen::ResponseBuilder::body(), proxygen::HTTPMessage::checkForHeaderToken(), folly::IOBuf::clone(), folly::IOBuf::cloneOne(), folly::IOBuf::copyBuffer(), folly::IOBuf::countChainElements(), proxygen::ZlibStreamDecompressor::decompress(), testing::DoAll(), EXPECT_CALL, EXPECT_FALSE, EXPECT_THAT, EXPECT_TRUE, folly::pushmi::operators::filter, proxygen::HTTPMessage::getHeaders(), proxygen::HTTPMessage::getIsChunked(), proxygen::ZlibStreamDecompressor::getStatus(), proxygen::ZlibStreamDecompressor::hasError(), proxygen::ResponseBuilder::header(), proxygen::HTTP_HEADER_ACCEPT_ENCODING, proxygen::HTTP_HEADER_CONTENT_ENCODING, proxygen::HTTP_HEADER_CONTENT_TYPE, testing::Invoke(), folly::IOBuf::isChained(), folly::gen::move, folly::IOBuf::next(), folly::IOBuf::prependChain(), testing::Return(), proxygen::ResponseBuilder::send(), folly::netops::send(), proxygen::ResponseBuilder::sendWithEOM(), proxygen::HTTPHeaders::set(), proxygen::HTTPMessage::setURL(), and proxygen::ResponseBuilder::status().

66  {
67 
68  // If there is only one IOBuf, then it's not chunked.
69  bool isResponseChunked = originalResponseBody->isChained();
70  size_t chunkCount = originalResponseBody->countChainElements();
71 
72  // Chunked and compressed responses will have an extra block
73  if (isResponseChunked && expectCompression) {
74  chunkCount += 1;
75  }
76 
77  // Request Handler Expectations
78  EXPECT_CALL(*requestHandler_, onBody(_)).Times(1);
79  EXPECT_CALL(*requestHandler_, onEOM()).Times(1);
80 
81  // Need to capture whatever the filter is for ResponseBuilder later
82  EXPECT_CALL(*requestHandler_, setResponseHandler(_))
83  .WillOnce(DoAll(SaveArg<0>(&downstream_), Return()));
84 
85  // Response Handler Expectations
86  // Headers are only sent once
87  EXPECT_CALL(*responseHandler_, sendHeaders(_)).WillOnce(DoAll(
88  Invoke([&](HTTPMessage& msg) {
89  auto& headers = msg.getHeaders();
90  if (expectCompression) {
92  HTTP_HEADER_CONTENT_ENCODING, expectedEncoding.c_str(), false));
93  }
94 
95  if (msg.getIsChunked()) {
96  EXPECT_FALSE(headers.exists("Content-Length"));
97  } else {
98  //Content-Length is not set on chunked messages
99  EXPECT_TRUE(headers.exists("Content-Length"));
100  }
101  }),
102  Return()));
103 
104  if (isResponseChunked) {
105  // The final chunk has 0 body
106  EXPECT_CALL(*responseHandler_, sendChunkHeader(_)).Times(chunkCount);
107  EXPECT_CALL(*responseHandler_, sendChunkTerminator()).Times(chunkCount);
108  } else {
109  EXPECT_CALL(*responseHandler_, sendChunkHeader(_)).Times(0);
110  EXPECT_CALL(*responseHandler_, sendChunkTerminator()).Times(0);
111  }
112 
113  // Accumulate the body, decompressing it if it's compressed
114  std::unique_ptr<folly::IOBuf> responseBody;
115  EXPECT_CALL(*responseHandler_, sendBody(_))
116  .Times(chunkCount)
117  .WillRepeatedly(DoAll(
118  Invoke([&](std::shared_ptr<folly::IOBuf> body) {
119 
120  std::unique_ptr<folly::IOBuf> processedBody;
121 
122  if (expectCompression) {
123  processedBody = zd_->decompress(body.get());
124  ASSERT_FALSE(zd_->hasError())
125  << "Failed to decompress body. r=" << zd_->getStatus();
126  } else {
127  processedBody = folly::IOBuf::copyBuffer(
128  body->data(), body->length(), 0, 0);
129  }
130 
131  if (responseBody) {
132  responseBody->prependChain(std::move(processedBody));
133  } else {
134  responseBody = std::move(processedBody);
135  }
136  }),
137  Return()));
138 
139  EXPECT_CALL(*responseHandler_, sendEOM()).Times(1);
140 
141  /* Simulate Request/Response */
142 
143  HTTPMessage msg;
144  msg.setURL(url);
145  msg.getHeaders().set(HTTP_HEADER_ACCEPT_ENCODING, acceptedEncoding);
146 
147  std::set<std::string> compressibleTypes = {"text/html"};
148  auto filterFactory = std::make_unique<ZlibServerFilterFactory>(
149  compressionLevel, minimumCompressionSize, compressibleTypes);
150 
151  auto filter = filterFactory->onRequest(requestHandler_, &msg);
152  filter->setResponseHandler(responseHandler_.get());
153 
154  // Send fake request
155  filter->onBody(folly::IOBuf::copyBuffer(originalRequestBody));
156  filter->onEOM();
157 
158  // Send a fake Response
159  if (isResponseChunked) {
160 
162  .status(200, "OK")
163  .header(HTTP_HEADER_CONTENT_TYPE, responseContentType)
164  .send();
165 
166  folly::IOBuf* crtBuf;
167  crtBuf = originalResponseBody.get();
168 
169  do {
171  crtBuf = crtBuf->next();
172  } while (crtBuf != originalResponseBody.get());
173 
175 
176  } else {
177 
178  // Send unchunked response
180  .status(200, "OK")
181  .header(HTTP_HEADER_CONTENT_TYPE, responseContentType)
182  .body(originalResponseBody->clone())
183  .sendWithEOM();
184  }
185 
186  filter->requestComplete();
187 
188  EXPECT_THAT(responseBody, IOBufEquals(originalRequestBody));
189  }
ResponseBuilder & status(uint16_t code, const std::string &message)
PUSHMI_INLINE_VAR constexpr detail::filter_fn filter
Definition: filter.h:75
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
bool isChained() const
Definition: IOBuf.h:760
std::unique_ptr< IOBuf > clone() const
Definition: IOBuf.cpp:527
size_t countChainElements() const
Definition: IOBuf.cpp:493
ResponseBuilder & body(std::unique_ptr< folly::IOBuf > bodyIn)
ResponseHandler * downstream_
void set(folly::StringPiece name, const std::string &value)
Definition: HTTPHeaders.h:119
std::unique_ptr< ZlibStreamDecompressor > zd_
bool checkForHeaderToken(const HTTPHeaderCode headerCode, char const *token, bool caseSensitive) const
ParseURL setURL(T &&url)
Definition: HTTPMessage.h:183
bool getIsChunked() const
Definition: HTTPMessage.h:80
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
ResponseBuilder & header(const std::string &headerIn, const T &value)
MockRequestHandler * requestHandler_
ssize_t send(NetworkSocket s, const void *buf, size_t len, int flags)
Definition: NetOps.cpp:319
std::unique_ptr< IOBuf > cloneOne() const
Definition: IOBuf.cpp:531
IOBuf * next()
Definition: IOBuf.h:600
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
std::unique_ptr< MockResponseHandler > responseHandler_
void prependChain(std::unique_ptr< IOBuf > &&iobuf)
Definition: IOBuf.cpp:509
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_THAT(value, matcher)
internal::DoBothAction< Action1, Action2 > DoAll(Action1 a1, Action2 a2)
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
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)
void ZlibServerFilterTest::SetUp ( )
inlineoverridevirtual

Reimplemented from testing::Test.

Definition at line 37 of file ZlibServerFilterTest.cpp.

References proxygen::GZIP.

37  {
38  // requesthandler is the server, responsehandler is the client
40  responseHandler_ = std::make_unique<MockResponseHandler>(requestHandler_);
41  zd_ = std::make_unique<ZlibStreamDecompressor>(ZlibCompressionType::GZIP);
42  }
std::unique_ptr< ZlibStreamDecompressor > zd_
MockRequestHandler * requestHandler_
std::unique_ptr< MockResponseHandler > responseHandler_
void ZlibServerFilterTest::TearDown ( )
inlineoverridevirtual

Reimplemented from testing::Test.

Definition at line 44 of file ZlibServerFilterTest.cpp.

44  {
45  Mock::VerifyAndClear(requestHandler_);
46  Mock::VerifyAndClear(responseHandler_.get());
47 
48  delete requestHandler_;
49  }
MockRequestHandler * requestHandler_
std::unique_ptr< MockResponseHandler > responseHandler_

Member Data Documentation

ResponseHandler* ZlibServerFilterTest::downstream_ {nullptr}
protected

Definition at line 56 of file ZlibServerFilterTest.cpp.

ZlibServerFilter* ZlibServerFilterTest::filter_ {nullptr}
protected

Definition at line 52 of file ZlibServerFilterTest.cpp.

MockRequestHandler* ZlibServerFilterTest::requestHandler_
protected

Definition at line 53 of file ZlibServerFilterTest.cpp.

std::unique_ptr<MockResponseHandler> ZlibServerFilterTest::responseHandler_
protected

Definition at line 54 of file ZlibServerFilterTest.cpp.

std::unique_ptr<ZlibStreamDecompressor> ZlibServerFilterTest::zd_
protected

Definition at line 55 of file ZlibServerFilterTest.cpp.


The documentation for this class was generated from the following file: