proxygen
ZlibServerFilterTest.cpp File Reference

Go to the source code of this file.

Classes

class  ZlibServerFilterTest
 

Functions

 MATCHER_P (IOBufEquals, expected, folly::to< std::string >("IOBuf is ", negation?"not ":"","'", expected,"'"))
 
 TEST_F (ZlibServerFilterTest, NonchunkedCompression)
 
 TEST_F (ZlibServerFilterTest, ChunkedCompression)
 
 TEST_F (ZlibServerFilterTest, ParameterizedContenttype)
 
 TEST_F (ZlibServerFilterTest, MixedcaseContenttype)
 
 TEST_F (ZlibServerFilterTest, MultipleAcceptedEncodings)
 
 TEST_F (ZlibServerFilterTest, MultipleAcceptedEncodingsQvalues)
 
 TEST_F (ZlibServerFilterTest, NoCompressibleAcceptedEncodings)
 
 TEST_F (ZlibServerFilterTest, MissingAcceptedEncodings)
 
 TEST_F (ZlibServerFilterTest, UncompressibleContenttype)
 
 TEST_F (ZlibServerFilterTest, UncompressibleContenttypeParam)
 
 TEST_F (ZlibServerFilterTest, TooSmallToCompress)
 
 TEST_F (ZlibServerFilterTest, SmallChunksCompress)
 
 TEST_F (ZlibServerFilterTest, MinimumCompressSizeEqualToRequestSize)
 
 TEST_F (ZlibServerFilterTest, NoResponseBody)
 

Function Documentation

MATCHER_P ( IOBufEquals  ,
expected  ,
folly::to< std::string "IOBuf is ", negation?"not ":"","'", expected,"'" 
)

Definition at line 24 of file ZlibServerFilterTest.cpp.

References string.

27  : "", "'", expected, "'")) {
28  auto iob = arg->clone();
29  auto br = iob->coalesce();
30  std::string actual(br.begin(), br.end());
31  *result_listener << "'" << actual << "'";
32  return actual == expected;
33 }
const char * string
Definition: Conv.cpp:212
TEST_F ( ZlibServerFilterTest  ,
NonchunkedCompression   
)

Definition at line 212 of file ZlibServerFilterTest.cpp.

References ASSERT_NO_FATAL_FAILURE, folly::IOBuf::copyBuffer(), and string.

212  {
214  exercise_compression(true,
215  std::string("http://locahost/foo.compressme"),
216  std::string("gzip"),
217  std::string("gzip"),
218  std::string("Hello World"),
219  std::string("text/html"),
220  folly::IOBuf::copyBuffer("Hello World"));
221  });
222 }
const char * string
Definition: Conv.cpp:212
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
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
TEST_F ( ZlibServerFilterTest  ,
ChunkedCompression   
)

Definition at line 224 of file ZlibServerFilterTest.cpp.

References ASSERT_NO_FATAL_FAILURE, chunks, and string.

224  {
225  std::vector<std::string> chunks = {"Hello", " World"};
227  exercise_compression(true,
228  std::string("http://locahost/foo.compressme"),
229  std::string("gzip"),
230  std::string("gzip"),
231  std::string("Hello World"),
232  std::string("text/html"),
233  createResponseChain(chunks));
234  });
235 }
auto chunks
const char * string
Definition: Conv.cpp:212
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
TEST_F ( ZlibServerFilterTest  ,
ParameterizedContenttype   
)

Definition at line 237 of file ZlibServerFilterTest.cpp.

References ASSERT_NO_FATAL_FAILURE, folly::IOBuf::copyBuffer(), and string.

237  {
239  exercise_compression(true,
240  std::string("http://locahost/foo.compressme"),
241  std::string("gzip"),
242  std::string("gzip"),
243  std::string("Hello World"),
244  std::string("text/html; param1"),
245  folly::IOBuf::copyBuffer("Hello World"));
246  });
247 }
const char * string
Definition: Conv.cpp:212
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
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
TEST_F ( ZlibServerFilterTest  ,
MixedcaseContenttype   
)

Definition at line 249 of file ZlibServerFilterTest.cpp.

References ASSERT_NO_FATAL_FAILURE, folly::IOBuf::copyBuffer(), and string.

249  {
251  exercise_compression(true,
252  std::string("http://locahost/foo.compressme"),
253  std::string("gzip"),
254  std::string("gzip"),
255  std::string("Hello World"),
256  std::string("Text/Html; param1"),
257  folly::IOBuf::copyBuffer("Hello World"));
258  });
259 }
const char * string
Definition: Conv.cpp:212
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
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
TEST_F ( ZlibServerFilterTest  ,
MultipleAcceptedEncodings   
)

Definition at line 262 of file ZlibServerFilterTest.cpp.

References ASSERT_NO_FATAL_FAILURE, folly::IOBuf::copyBuffer(), and string.

262  {
264  exercise_compression(true,
265  std::string("http://locahost/foo.compressme"),
266  std::string("gzip, identity, deflate"),
267  std::string("gzip"),
268  std::string("Hello World"),
269  std::string("text/html"),
270  folly::IOBuf::copyBuffer("Hello World"));
271  });
272 }
const char * string
Definition: Conv.cpp:212
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
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
TEST_F ( ZlibServerFilterTest  ,
MultipleAcceptedEncodingsQvalues   
)

Definition at line 274 of file ZlibServerFilterTest.cpp.

References ASSERT_NO_FATAL_FAILURE, folly::IOBuf::copyBuffer(), and string.

274  {
276  exercise_compression(true,
277  std::string("http://locahost/foo.compressme"),
278  std::string("gzip; q=.7;, identity"),
279  std::string("gzip"),
280  std::string("Hello World"),
281  std::string("text/html"),
282  folly::IOBuf::copyBuffer("Hello World"));
283  });
284 }
const char * string
Definition: Conv.cpp:212
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
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
TEST_F ( ZlibServerFilterTest  ,
NoCompressibleAcceptedEncodings   
)

Definition at line 286 of file ZlibServerFilterTest.cpp.

References ASSERT_NO_FATAL_FAILURE, folly::IOBuf::copyBuffer(), and string.

286  {
288  exercise_compression(false,
289  std::string("http://locahost/foo.compressme"),
290  std::string("identity; q=.7;"),
291  std::string(""),
292  std::string("Hello World"),
293  std::string("text/html"),
294  folly::IOBuf::copyBuffer("Hello World"));
295  });
296 }
const char * string
Definition: Conv.cpp:212
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
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
TEST_F ( ZlibServerFilterTest  ,
MissingAcceptedEncodings   
)

Definition at line 298 of file ZlibServerFilterTest.cpp.

References ASSERT_NO_FATAL_FAILURE, folly::IOBuf::copyBuffer(), and string.

298  {
300  exercise_compression(false,
301  std::string("http://locahost/foo.compressme"),
302  std::string(""),
303  std::string(""),
304  std::string("Hello World"),
305  std::string("text/html"),
306  folly::IOBuf::copyBuffer("Hello World"));
307  });
308 }
const char * string
Definition: Conv.cpp:212
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
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
TEST_F ( ZlibServerFilterTest  ,
UncompressibleContenttype   
)

Definition at line 311 of file ZlibServerFilterTest.cpp.

References ASSERT_NO_FATAL_FAILURE, folly::IOBuf::copyBuffer(), and string.

311  {
313  exercise_compression(false,
314  std::string("http://locahost/foo.nocompress"),
315  std::string("gzip"),
316  std::string(""),
317  std::string("Hello World"),
318  std::string("image/jpeg"),
319  folly::IOBuf::copyBuffer("Hello World"));
320  });
321 }
const char * string
Definition: Conv.cpp:212
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
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
TEST_F ( ZlibServerFilterTest  ,
UncompressibleContenttypeParam   
)

Definition at line 323 of file ZlibServerFilterTest.cpp.

References ASSERT_NO_FATAL_FAILURE, folly::IOBuf::copyBuffer(), and string.

323  {
325  exercise_compression(false,
326  std::string("http://locahost/foo.nocompress"),
327  std::string("gzip"),
328  std::string(""),
329  std::string("Hello World"),
330  std::string("application/jpeg; param1"),
331  folly::IOBuf::copyBuffer("Hello World"));
332  });
333 }
const char * string
Definition: Conv.cpp:212
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
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
TEST_F ( ZlibServerFilterTest  ,
TooSmallToCompress   
)

Definition at line 336 of file ZlibServerFilterTest.cpp.

References ASSERT_NO_FATAL_FAILURE, folly::IOBuf::copyBuffer(), and string.

336  {
338  exercise_compression(false,
339  std::string("http://locahost/foo.smallfry"),
340  std::string("gzip"),
341  std::string(""),
342  std::string("Hello World"),
343  std::string("text/html"),
344  folly::IOBuf::copyBuffer("Hello World"),
345  4,
346  1000);
347  });
348 }
const char * string
Definition: Conv.cpp:212
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
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
TEST_F ( ZlibServerFilterTest  ,
SmallChunksCompress   
)

Definition at line 350 of file ZlibServerFilterTest.cpp.

References ASSERT_NO_FATAL_FAILURE, chunks, and string.

350  {
351  // Expect this to compress despite being small because can't tell the content
352  // length when we're chunked
353  std::vector<std::string> chunks = {"Hello", " World"};
355  exercise_compression(true,
356  std::string("http://locahost/foo.compressme"),
357  std::string("gzip"),
358  std::string("gzip"),
359  std::string("Hello World"),
360  std::string("text/html"),
361  createResponseChain(chunks),
362  4,
363  1000);
364  });
365 }
auto chunks
const char * string
Definition: Conv.cpp:212
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
TEST_F ( ZlibServerFilterTest  ,
MinimumCompressSizeEqualToRequestSize   
)

Definition at line 367 of file ZlibServerFilterTest.cpp.

References ASSERT_NO_FATAL_FAILURE, folly::IOBuf::copyBuffer(), and string.

367  {
368  auto requestBody = std::string("Hello World");
370  exercise_compression(true,
371  std::string("http://locahost/foo.compressme"),
372  std::string("gzip"),
373  std::string("gzip"),
374  requestBody,
375  std::string("text/html"),
376  folly::IOBuf::copyBuffer(requestBody),
377  4,
378  requestBody.length());
379  });
380 }
const char * string
Definition: Conv.cpp:212
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
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
TEST_F ( ZlibServerFilterTest  ,
NoResponseBody   
)

Definition at line 382 of file ZlibServerFilterTest.cpp.

References testing::_, ASSERT_NO_FATAL_FAILURE, proxygen::HTTPMessage::checkForHeaderToken(), testing::DoAll(), EXPECT_CALL, EXPECT_FALSE, EXPECT_TRUE, folly::pushmi::operators::filter, proxygen::HTTPMessage::getHeaders(), proxygen::HTTPMessage::getIsChunked(), proxygen::ResponseBuilder::header(), proxygen::HTTP_HEADER_ACCEPT_ENCODING, proxygen::HTTP_HEADER_CONTENT_ENCODING, proxygen::HTTP_HEADER_CONTENT_TYPE, int32_t, testing::Invoke(), testing::Return(), proxygen::ResponseBuilder::send(), proxygen::ResponseBuilder::sendWithEOM(), proxygen::HTTPHeaders::set(), proxygen::HTTPMessage::setURL(), proxygen::ResponseBuilder::status(), string, and uint32_t.

382  {
383  std::string acceptedEncoding = "gzip";
384  std::string expectedEncoding = "gzip";
385  std::string url = std::string("http://locahost/foo.compressme");
386  std::string responseContentType = std::string("text/html");
387  int32_t compressionLevel = 4;
388  uint32_t minimumCompressionSize = 0;
389 
391  // Request Handler Expectations
392  EXPECT_CALL(*requestHandler_, onEOM()).Times(1);
393 
394  // Need to capture whatever the filter is for ResponseBuilder later
395  EXPECT_CALL(*requestHandler_, setResponseHandler(_))
396  .WillOnce(DoAll(SaveArg<0>(&downstream_), Return()));
397 
398  // Response Handler Expectations
399  // Headers are only sent once
400  EXPECT_CALL(*responseHandler_, sendHeaders(_)).WillOnce(DoAll(
401  Invoke([&](HTTPMessage& msg) {
402  auto& headers = msg.getHeaders();
404  HTTP_HEADER_CONTENT_ENCODING, expectedEncoding.c_str(), false));
405  if (msg.getIsChunked()) {
406  EXPECT_FALSE(headers.exists("Content-Length"));
407  } else {
408  //Content-Length is not set on chunked messages
409  EXPECT_TRUE(headers.exists("Content-Length"));
410  }
411  }),
412  Return()));
413 
414  EXPECT_CALL(*responseHandler_, sendEOM()).Times(1);
415 
416  /* Simulate Request/Response where no body message received */
417  HTTPMessage msg;
418  msg.setURL(url);
419  msg.getHeaders().set(HTTP_HEADER_ACCEPT_ENCODING, acceptedEncoding);
420 
421  std::set<std::string> compressibleTypes = {"text/html"};
422  auto filterFactory = std::make_unique<ZlibServerFilterFactory>(
423  compressionLevel, minimumCompressionSize, compressibleTypes);
424 
425  auto filter = filterFactory->onRequest(requestHandler_, &msg);
426  filter->setResponseHandler(responseHandler_.get());
427 
428  // Send fake request
429  filter->onEOM();
430 
431  ResponseBuilder(downstream_)
432  .status(200, "OK")
433  .header(HTTP_HEADER_CONTENT_TYPE, responseContentType)
434  .send();
435 
436  ResponseBuilder(downstream_).sendWithEOM();
437 
438  filter->requestComplete();
439  });
440 }
ResponseBuilder & status(uint16_t code, const std::string &message)
PUSHMI_INLINE_VAR constexpr detail::filter_fn filter
Definition: filter.h:75
void set(folly::StringPiece name, const std::string &value)
Definition: HTTPHeaders.h:119
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)
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
const char * string
Definition: Conv.cpp:212
internal::DoBothAction< Action1, Action2 > DoAll(Action1 a1, Action2 a2)
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
internal::ReturnAction< R > Return(R value)