proxygen
HTTP1xCodecTest.cpp File Reference

Go to the source code of this file.

Classes

class  HTTP1xCodecCallback
 
class  ConnectionHeaderTest
 

Functions

unique_ptr< folly::IOBufgetSimpleRequestData ()
 
 TEST (HTTP1xCodecTest, TestSimpleHeaders)
 
 TEST (HTTP1xCodecTest, Test09Req)
 
 TEST (HTTP1xCodecTest, Test09ReqVers)
 
 TEST (HTTP1xCodecTest, Test09Resp)
 
 TEST (HTTP1xCodecTest, TestBadHeaders)
 
 TEST (HTTP1xCodecTest, TestHeadRequestChunkedResponse)
 
 TEST (HTTP1xCodecTest, TestGetRequestChunkedResponse)
 
unique_ptr< folly::IOBufgetChunkedRequest1st ()
 
unique_ptr< folly::IOBufgetChunkedRequest2nd ()
 
 TEST (HTTP1xCodecTest, TestChunkedHeaders)
 
 TEST (HTTP1xCodecTest, TestChunkedUpstream)
 
 TEST (HTTP1xCodecTest, TestBadPost100)
 
 TEST (HTTP1xCodecTest, TestMultipleIdenticalContentLengthHeaders)
 
 TEST (HTTP1xCodecTest, TestMultipleDistinctContentLengthHeaders)
 
 TEST (HTTP1xCodecTest, TestCorrectTransferEncodingHeader)
 
 TEST (HTTP1xCodecTest, TestFoldedTransferEncodingHeader)
 
 TEST (HTTP1xCodecTest, TestBadTransferEncodingHeader)
 
 TEST (HTTP1xCodecTest, Test1xxConnectionHeader)
 
 TEST (HTTP1xCodecTest, TestChainedBody)
 
 TEST (HTTP1xCodecTest, TestIgnoreUpstreamUpgrade)
 
 TEST (HTTP1xCodecTest, WebsocketUpgrade)
 
 TEST (HTTP1xCodecTest, WebsocketUpgradeKeyError)
 
 TEST (HTTP1xCodecTest, WebsocketUpgradeHeaderSet)
 
 TEST (HTTP1xCodecTest, WebsocketConnectionHeader)
 
 TEST (HTTP1xCodecTest, TrailersAndEomAreNotGeneratedWhenNonChunked)
 
 TEST_P (ConnectionHeaderTest, TestConnectionHeaders)
 
 INSTANTIATE_TEST_CASE_P (HTTP1xCodec, ConnectionHeaderTest,::testing::Values(ConnectionHeaderTest::ParamType({"foo","bar","close","baz"},"foo, bar, baz, close"), ConnectionHeaderTest::ParamType({"foo","bar, close","baz"},"foo, bar, baz, close"), ConnectionHeaderTest::ParamType({" foo","bar, close "," baz "},"foo, bar, baz, close"), ConnectionHeaderTest::ParamType({"foo","bar, boo","baz"},"foo, bar, boo, baz, keep-alive"), ConnectionHeaderTest::ParamType({"foo","keep-alive, boo","close"},"foo, boo, close"), ConnectionHeaderTest::ParamType({"foo","upgrade, boo","baz"},"foo, upgrade, boo, baz, keep-alive")))
 

Function Documentation

unique_ptr<folly::IOBuf> getChunkedRequest1st ( )

Definition at line 214 of file HTTP1xCodecTest.cpp.

References folly::IOBuf::copyBuffer().

Referenced by TEST().

214  {
215  string req("GET /aha HTTP/1.1\n");
216  return folly::IOBuf::copyBuffer(req);
217 }
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
unique_ptr<folly::IOBuf> getChunkedRequest2nd ( )

Definition at line 219 of file HTTP1xCodecTest.cpp.

References folly::IOBuf::copyBuffer().

Referenced by TEST().

219  {
220  string req("Host: m.facebook.com\nAccept-Encoding: meflate\n\n");
221  return folly::IOBuf::copyBuffer(req);
222 }
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
unique_ptr<folly::IOBuf> getSimpleRequestData ( )

Definition at line 66 of file HTTP1xCodecTest.cpp.

References folly::IOBuf::copyBuffer().

Referenced by TEST().

66  {
67  string req("GET /yeah HTTP/1.1\nHost: www.facebook.com\n\n");
68  return folly::IOBuf::copyBuffer(req);
69 }
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
INSTANTIATE_TEST_CASE_P ( HTTP1xCodec  ,
ConnectionHeaderTest  ,
::testing::Values(ConnectionHeaderTest::ParamType({"foo","bar","close","baz"},"foo, bar, baz, close"), ConnectionHeaderTest::ParamType({"foo","bar, close","baz"},"foo, bar, baz, close"), ConnectionHeaderTest::ParamType({" foo","bar, close "," baz "},"foo, bar, baz, close"), ConnectionHeaderTest::ParamType({"foo","bar, boo","baz"},"foo, bar, boo, baz, keep-alive"), ConnectionHeaderTest::ParamType({"foo","keep-alive, boo","close"},"foo, boo, close"), ConnectionHeaderTest::ParamType({"foo","upgrade, boo","baz"},"foo, upgrade, boo, baz, keep-alive"))   
)
TEST ( HTTP1xCodecTest  ,
TestSimpleHeaders   
)

Definition at line 71 of file HTTP1xCodecTest.cpp.

References buffer(), codec, wangle::HTTPHeaderSize::compressed, proxygen::DOWNSTREAM, EXPECT_EQ, getSimpleRequestData(), HTTP1xCodecCallback::headersComplete, HTTP1xCodecCallback::headerSize, proxygen::HTTP1xCodec::onIngress(), proxygen::HTTP1xCodec::setCallback(), and wangle::HTTPHeaderSize::uncompressed.

71  {
72  HTTP1xCodec codec(TransportDirection::DOWNSTREAM);
73  HTTP1xCodecCallback callbacks;
74  codec.setCallback(&callbacks);
76  codec.onIngress(*buffer);
77  EXPECT_EQ(callbacks.headersComplete, 1);
78  EXPECT_EQ(buffer->length(), callbacks.headerSize.uncompressed);
79  EXPECT_EQ(callbacks.headerSize.compressed, 0);
80 }
std::vector< uint8_t > buffer(kBufferSize+16)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
CodecFactory codec
unique_ptr< folly::IOBuf > getSimpleRequestData()
HTTPHeaderSize headerSize
TEST ( HTTP1xCodecTest  ,
Test09Req   
)

Definition at line 82 of file HTTP1xCodecTest.cpp.

References buffer(), codec, wangle::HTTPHeaderSize::compressed, folly::IOBuf::copyBuffer(), proxygen::DOWNSTREAM, EXPECT_EQ, HTTP1xCodecCallback::headersComplete, HTTP1xCodecCallback::headerSize, HTTP1xCodecCallback::messageComplete, proxygen::HTTP1xCodec::onIngress(), proxygen::HTTP1xCodec::setCallback(), and wangle::HTTPHeaderSize::uncompressed.

82  {
83  HTTP1xCodec codec(TransportDirection::DOWNSTREAM);
84  HTTP1xCodecCallback callbacks;
85  codec.setCallback(&callbacks);
86  auto buffer = folly::IOBuf::copyBuffer(string("GET /yeah\r\n"));
87  codec.onIngress(*buffer);
88  EXPECT_EQ(callbacks.headersComplete, 1);
89  EXPECT_EQ(callbacks.messageComplete, 1);
90  EXPECT_EQ(buffer->length(), callbacks.headerSize.uncompressed);
91  EXPECT_EQ(callbacks.headerSize.compressed, 0);
92  buffer = folly::IOBuf::copyBuffer(string("\r\n"));
93  codec.onIngress(*buffer);
94  EXPECT_EQ(callbacks.headersComplete, 1);
95  EXPECT_EQ(callbacks.messageComplete, 1);
96 }
std::vector< uint8_t > buffer(kBufferSize+16)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
CodecFactory codec
HTTPHeaderSize headerSize
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 ( HTTP1xCodecTest  ,
Test09ReqVers   
)

Definition at line 98 of file HTTP1xCodecTest.cpp.

References buffer(), codec, wangle::HTTPHeaderSize::compressed, folly::IOBuf::copyBuffer(), proxygen::DOWNSTREAM, EXPECT_EQ, HTTP1xCodecCallback::headersComplete, HTTP1xCodecCallback::headerSize, HTTP1xCodecCallback::messageComplete, proxygen::HTTP1xCodec::onIngress(), proxygen::HTTP1xCodec::setCallback(), and wangle::HTTPHeaderSize::uncompressed.

98  {
99  HTTP1xCodec codec(TransportDirection::DOWNSTREAM);
100  HTTP1xCodecCallback callbacks;
101  codec.setCallback(&callbacks);
102  auto buffer = folly::IOBuf::copyBuffer(string("GET /yeah HTTP/0.9\r\n"));
103  codec.onIngress(*buffer);
104  EXPECT_EQ(callbacks.headersComplete, 1);
105  EXPECT_EQ(callbacks.messageComplete, 1);
106  EXPECT_EQ(buffer->length(), callbacks.headerSize.uncompressed);
107  EXPECT_EQ(callbacks.headerSize.compressed, 0);
108 }
std::vector< uint8_t > buffer(kBufferSize+16)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
CodecFactory codec
HTTPHeaderSize headerSize
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 ( HTTP1xCodecTest  ,
Test09Resp   
)

Definition at line 110 of file HTTP1xCodecTest.cpp.

References HTTP1xCodecCallback::bodyLen, buffer(), codec, folly::IOBuf::copyBuffer(), proxygen::HTTP1xCodec::createStream(), EXPECT_EQ, proxygen::HTTP1xCodec::generateHeader(), proxygen::GET, HTTP1xCodecCallback::headersComplete, HTTP1xCodecCallback::messageComplete, proxygen::HTTP1xCodec::onIngress(), proxygen::HTTP1xCodec::onIngressEOF(), proxygen::HTTP1xCodec::setCallback(), proxygen::HTTPMessage::setHTTPVersion(), proxygen::HTTPMessage::setMethod(), proxygen::HTTPMessage::setURL(), and proxygen::UPSTREAM.

110  {
111  HTTP1xCodec codec(TransportDirection::UPSTREAM);
112  HTTP1xCodecCallback callbacks;
113  HTTPMessage req;
114  auto id = codec.createStream();
115  req.setHTTPVersion(0, 9);
116  req.setMethod(HTTPMethod::GET);
117  req.setURL("/");
118  codec.setCallback(&callbacks);
119  folly::IOBufQueue buf;
120  codec.generateHeader(buf, id, req, true);
122  string("iamtheverymodelofamodernmajorgeneral"));
123  codec.onIngress(*buffer);
124  EXPECT_EQ(callbacks.headersComplete, 1);
125  EXPECT_EQ(callbacks.bodyLen, buffer->computeChainDataLength());
126  EXPECT_EQ(callbacks.messageComplete, 0);
127  codec.onIngressEOF();
128  EXPECT_EQ(callbacks.messageComplete, 1);
129 }
std::vector< uint8_t > buffer(kBufferSize+16)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
CodecFactory codec
ParseURL setURL(T &&url)
Definition: HTTPMessage.h:183
void setMethod(HTTPMethod method)
void setHTTPVersion(uint8_t major, uint8_t minor)
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 ( HTTP1xCodecTest  ,
TestBadHeaders   
)

Definition at line 131 of file HTTP1xCodecTest.cpp.

References testing::_, buffer(), codec, folly::IOBuf::copyBuffer(), proxygen::DOWNSTREAM, folly::pushmi::operators::error(), EXPECT_CALL, EXPECT_EQ, testing::Invoke(), proxygen::HTTP1xCodec::onIngress(), and proxygen::HTTP1xCodec::setCallback().

131  {
132  HTTP1xCodec codec(TransportDirection::DOWNSTREAM);
133  MockHTTPCodecCallback callbacks;
134  codec.setCallback(&callbacks);
136  string("GET /yeah HTTP/1.1\nUser-Agent: Mozilla/5.0 Version/4.0 "
137  "\x10i\xC7n tho\xA1iSafari/534.30]"));
138  EXPECT_CALL(callbacks, onMessageBegin(1, _));
139  EXPECT_CALL(callbacks, onError(1, _, _))
140  .WillOnce(Invoke([&] (HTTPCodec::StreamID,
141  std::shared_ptr<HTTPException> error,
142  bool) {
143  EXPECT_EQ(error->getHttpStatusCode(), 400);
144  }));
145  codec.onIngress(*buffer);
146 }
std::vector< uint8_t > buffer(kBufferSize+16)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
CodecFactory codec
requires And< SemiMovable< VN >... > &&SemiMovable< E > auto error(E e)
Definition: error.h:48
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
#define EXPECT_CALL(obj, call)
uint64_t StreamID
Definition: HTTPCodec.h:49
const internal::AnythingMatcher _
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 ( HTTP1xCodecTest  ,
TestHeadRequestChunkedResponse   
)

Definition at line 148 of file HTTP1xCodecTest.cpp.

References folly::IOBufQueue::cacheChainLength(), codec, folly::IOBuf::copyBuffer(), proxygen::HTTP1xCodec::createStream(), proxygen::DOWNSTREAM, EXPECT_EQ, EXPECT_TRUE, proxygen::HTTP1xCodec::generateHeader(), proxygen::HTTPMessage::getHeaders(), HTTP1xCodecCallback::headersComplete, proxygen::HTTP_HEADER_TRANSFER_ENCODING, folly::IOBufQueue::move(), proxygen::HTTP1xCodec::onIngress(), proxygen::HTTPHeaders::set(), proxygen::HTTP1xCodec::setCallback(), proxygen::HTTPMessage::setHTTPVersion(), proxygen::HTTPMessage::setIsChunked(), and proxygen::HTTPMessage::setStatusCode().

148  {
149  HTTP1xCodec codec(TransportDirection::DOWNSTREAM);
150  HTTP1xCodecCallback callbacks;
151  codec.setCallback(&callbacks);
152  auto txnID = codec.createStream();
153 
154  // Generate a HEAD request
155  auto reqBuf = folly::IOBuf::copyBuffer(
156  "HEAD /www.facebook.com HTTP/1.1\nHost: www.facebook.com\n\n");
157  codec.onIngress(*reqBuf);
158  EXPECT_EQ(callbacks.headersComplete, 1);
159 
160  // Generate chunked response with no body
161  HTTPMessage resp;
162  resp.setHTTPVersion(1, 1);
163  resp.setStatusCode(200);
164  resp.setIsChunked(true);
165  resp.getHeaders().set(HTTP_HEADER_TRANSFER_ENCODING, "chunked");
167  codec.generateHeader(respBuf, txnID, resp, true);
168  auto respStr = respBuf.move()->moveToFbString();
169  EXPECT_TRUE(respStr.find("0\r\n") == string::npos);
170 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
CodecFactory codec
void setIsChunked(bool chunked)
Definition: HTTPMessage.h:79
void set(folly::StringPiece name, const std::string &value)
Definition: HTTPHeaders.h:119
static Options cacheChainLength()
Definition: IOBufQueue.h:83
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
void setHTTPVersion(uint8_t major, uint8_t minor)
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 setStatusCode(uint16_t status)
TEST ( HTTP1xCodecTest  ,
TestGetRequestChunkedResponse   
)

Definition at line 172 of file HTTP1xCodecTest.cpp.

References ASSERT_EQ, folly::IOBufQueue::cacheChainLength(), folly::IOBufQueue::chainLength(), codec, folly::IOBuf::copyBuffer(), proxygen::HTTP1xCodec::createStream(), proxygen::DOWNSTREAM, EXPECT_EQ, proxygen::HTTP1xCodec::generateBody(), proxygen::HTTP1xCodec::generateHeader(), proxygen::HTTPMessage::getHeaders(), HTTP1xCodecCallback::headersComplete, proxygen::HTTP_HEADER_TRANSFER_ENCODING, folly::gen::move, proxygen::HTTPCodec::NoPadding, proxygen::HTTP1xCodec::onIngress(), proxygen::HTTPHeaders::set(), proxygen::HTTP1xCodec::setCallback(), proxygen::HTTPMessage::setHTTPVersion(), proxygen::HTTPMessage::setIsChunked(), proxygen::HTTPMessage::setStatusCode(), and folly::IOBufQueue::split().

172  {
173  HTTP1xCodec codec(TransportDirection::DOWNSTREAM);
174  HTTP1xCodecCallback callbacks;
175  codec.setCallback(&callbacks);
176  auto txnID = codec.createStream();
177 
178  // Generate a GET request
179  auto reqBuf = folly::IOBuf::copyBuffer(
180  "GET /www.facebook.com HTTP/1.1\nHost: www.facebook.com\n\n");
181  codec.onIngress(*reqBuf);
182  EXPECT_EQ(callbacks.headersComplete, 1);
183 
184  // Generate chunked response with body
185  HTTPMessage resp;
186  resp.setHTTPVersion(1, 1);
187  resp.setStatusCode(200);
188  resp.setIsChunked(true);
189  resp.getHeaders().set(HTTP_HEADER_TRANSFER_ENCODING, "chunked");
191  codec.generateHeader(respBuf, txnID, resp, false);
192 
193  auto headerFromBuf = respBuf.split(respBuf.chainLength());
194 
195  string resp1("Hello");
196  auto body1 = folly::IOBuf::copyBuffer(resp1);
197 
198  string resp2("");
199  auto body2 = folly::IOBuf::copyBuffer(resp2);
200 
201  codec.generateBody(respBuf, txnID, std::move(body1), HTTPCodec::NoPadding,
202  false);
203 
204  auto bodyFromBuf = respBuf.split(respBuf.chainLength());
205  ASSERT_EQ("5\r\nHello\r\n", bodyFromBuf->moveToFbString());
206 
207  codec.generateBody(respBuf, txnID, std::move(body2), HTTPCodec::NoPadding,
208  true);
209 
210  bodyFromBuf = respBuf.split(respBuf.chainLength());
211  ASSERT_EQ("0\r\n\r\n", bodyFromBuf->moveToFbString());
212 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
CodecFactory codec
void setIsChunked(bool chunked)
Definition: HTTPMessage.h:79
void set(folly::StringPiece name, const std::string &value)
Definition: HTTPHeaders.h:119
static Options cacheChainLength()
Definition: IOBufQueue.h:83
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
void setHTTPVersion(uint8_t major, uint8_t minor)
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 setStatusCode(uint16_t status)
TEST ( HTTP1xCodecTest  ,
TestChunkedHeaders   
)

Definition at line 224 of file HTTP1xCodecTest.cpp.

References codec, proxygen::DOWNSTREAM, EXPECT_EQ, getChunkedRequest1st(), getChunkedRequest2nd(), HTTP1xCodecCallback::headersComplete, HTTP1xCodecCallback::headerSize, i, proxygen::HTTP1xCodec::onIngress(), proxygen::HTTP1xCodec::setCallback(), and wangle::HTTPHeaderSize::uncompressed.

224  {
225  HTTP1xCodec codec(TransportDirection::DOWNSTREAM);
226  HTTP1xCodecCallback callbacks;
227  codec.setCallback(&callbacks);
228  // test a sequence of requests to make sure we're resetting the size counter
229  for (int i = 0; i < 3; i++) {
230  callbacks.headersComplete = 0;
231  auto buffer1 = getChunkedRequest1st();
232  codec.onIngress(*buffer1);
233  EXPECT_EQ(callbacks.headersComplete, 0);
234 
235  auto buffer2 = getChunkedRequest2nd();
236  codec.onIngress(*buffer2);
237  EXPECT_EQ(callbacks.headersComplete, 1);
239  buffer1->length() + buffer2->length());
240  }
241 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
CodecFactory codec
unique_ptr< folly::IOBuf > getChunkedRequest1st()
unique_ptr< folly::IOBuf > getChunkedRequest2nd()
HTTPHeaderSize headerSize
TEST ( HTTP1xCodecTest  ,
TestChunkedUpstream   
)

Definition at line 243 of file HTTP1xCodecTest.cpp.

References ASSERT_EQ, folly::IOBufQueue::cacheChainLength(), folly::IOBufQueue::chainLength(), codec, folly::IOBuf::copyBuffer(), proxygen::HTTP1xCodec::createStream(), proxygen::HTTP1xCodec::generateBody(), proxygen::HTTP1xCodec::generateHeader(), proxygen::HTTPMessage::getHeaders(), proxygen::HTTP_HEADER_HOST, proxygen::HTTP_HEADER_TRANSFER_ENCODING, folly::gen::move, proxygen::HTTPCodec::NoPadding, proxygen::HTTPHeaders::set(), proxygen::HTTPMessage::setHTTPVersion(), proxygen::HTTPMessage::setIsChunked(), proxygen::HTTPMessage::setURL(), folly::size(), folly::IOBufQueue::split(), and proxygen::UPSTREAM.

243  {
244  HTTP1xCodec codec(TransportDirection::UPSTREAM);
245 
246  auto txnID = codec.createStream();
247 
248  HTTPMessage msg;
249  msg.setHTTPVersion(1, 1);
250  msg.setURL("https://www.facebook.com/");
251  msg.getHeaders().set(HTTP_HEADER_HOST, "www.facebook.com");
252  msg.getHeaders().set(HTTP_HEADER_TRANSFER_ENCODING, "chunked");
253  msg.setIsChunked(true);
254 
256 
258  codec.generateHeader(buf, txnID, msg, false, &size);
259  auto headerFromBuf = buf.split(buf.chainLength());
260 
261  string req1("Hello");
262  auto body1 = folly::IOBuf::copyBuffer(req1);
263 
264  string req2("World");
265  auto body2 = folly::IOBuf::copyBuffer(req2);
266 
267  codec.generateBody(buf, txnID, std::move(body1), HTTPCodec::NoPadding,
268  false);
269 
270  auto bodyFromBuf = buf.split(buf.chainLength());
271  ASSERT_EQ("5\r\nHello\r\n", bodyFromBuf->moveToFbString());
272 
273  codec.generateBody(buf, txnID, std::move(body2), HTTPCodec::NoPadding,
274  true);
275  LOG(WARNING) << "len chain" << buf.chainLength();
276 
277  auto eomFromBuf = buf.split(buf.chainLength());
278  ASSERT_EQ("5\r\nWorld\r\n0\r\n\r\n", eomFromBuf->moveToFbString());
279 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
CodecFactory codec
void setIsChunked(bool chunked)
Definition: HTTPMessage.h:79
void set(folly::StringPiece name, const std::string &value)
Definition: HTTPHeaders.h:119
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
ParseURL setURL(T &&url)
Definition: HTTPMessage.h:183
static Options cacheChainLength()
Definition: IOBufQueue.h:83
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
void setHTTPVersion(uint8_t major, uint8_t minor)
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 ( HTTP1xCodecTest  ,
TestBadPost100   
)

Definition at line 281 of file HTTP1xCodecTest.cpp.

References testing::_, folly::IOBufQueue::cacheChainLength(), codec, folly::IOBuf::copyBuffer(), proxygen::DOWNSTREAM, EXPECT_CALL, proxygen::HTTP1xCodec::generateEOM(), proxygen::HTTP1xCodec::generateHeader(), testing::InvokeWithoutArgs(), proxygen::HTTP1xCodec::onIngress(), proxygen::HTTP1xCodec::setCallback(), proxygen::HTTP1xCodec::setParserPaused(), proxygen::HTTPMessage::setStatusCode(), proxygen::HTTPMessage::setStatusMessage(), and fizz::detail::writeBuf().

281  {
282  HTTP1xCodec codec(TransportDirection::DOWNSTREAM);
283  MockHTTPCodecCallback callbacks;
284  codec.setCallback(&callbacks);
286 
287  InSequence enforceOrder;
288  EXPECT_CALL(callbacks, onMessageBegin(1, _));
289  EXPECT_CALL(callbacks, onHeadersComplete(1, _))
290  .WillOnce(InvokeWithoutArgs([&] {
291  HTTPMessage cont;
292  cont.setStatusCode(100);
293  cont.setStatusMessage("Continue");
294  codec.generateHeader(writeBuf, 1, cont);
295  }));
296 
297  EXPECT_CALL(callbacks, onBody(1, _, _));
298  EXPECT_CALL(callbacks, onMessageComplete(1, _));
299  EXPECT_CALL(callbacks, onMessageBegin(2, _))
300  .WillOnce(InvokeWithoutArgs([&] {
301  // simulate HTTPSession's aversion to pipelining
302  codec.setParserPaused(true);
303 
304  // Trigger the response to the POST
305  HTTPMessage resp;
306  resp.setStatusCode(200);
307  resp.setStatusMessage("OK");
308  codec.generateHeader(writeBuf, 1, resp);
309  codec.generateEOM(writeBuf, 1);
310  codec.setParserPaused(false);
311  }));
312  EXPECT_CALL(callbacks, onError(2, _, _))
313  .WillOnce(InvokeWithoutArgs([&] {
314  HTTPMessage resp;
315  resp.setStatusCode(400);
316  resp.setStatusMessage("Bad");
317  codec.generateHeader(writeBuf, 2, resp);
318  codec.generateEOM(writeBuf, 2);
319  }));
320  // Generate a POST request with a bad content-length
321  auto reqBuf = folly::IOBuf::copyBuffer(
322  "POST /www.facebook.com HTTP/1.1\r\nHost: www.facebook.com\r\n"
323  "Expect: 100-Continue\r\nContent-Length: 5\r\n\r\nabcdefghij");
324  codec.onIngress(*reqBuf);
325 }
void setStatusMessage(T &&msg)
Definition: HTTPMessage.h:242
CodecFactory codec
PolymorphicAction< internal::InvokeWithoutArgsAction< FunctionImpl > > InvokeWithoutArgs(FunctionImpl function_impl)
void writeBuf(const Buf &buf, folly::io::Appender &out)
static Options cacheChainLength()
Definition: IOBufQueue.h:83
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
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 setStatusCode(uint16_t status)
TEST ( HTTP1xCodecTest  ,
TestMultipleIdenticalContentLengthHeaders   
)

Definition at line 327 of file HTTP1xCodecTest.cpp.

References folly::IOBufQueue::cacheChainLength(), codec, folly::IOBuf::copyBuffer(), proxygen::DOWNSTREAM, EXPECT_EQ, proxygen::FakeHTTPCodecCallback::headersComplete, proxygen::FakeHTTPCodecCallback::messageBegin, proxygen::HTTP1xCodec::onIngress(), proxygen::HTTP1xCodec::setCallback(), proxygen::FakeHTTPCodecCallback::streamErrors, and fizz::detail::writeBuf().

327  {
328  HTTP1xCodec codec(TransportDirection::DOWNSTREAM);
329  FakeHTTPCodecCallback callbacks;
330  codec.setCallback(&callbacks);
332 
333  // Generate a POST request with two identical Content-Length headers
334  auto reqBuf = folly::IOBuf::copyBuffer(
335  "POST /www.facebook.com HTTP/1.1\r\nHost: www.facebook.com\r\n"
336  "Content-Length: 5\r\nContent-Length: 5\r\n\r\n");
337  codec.onIngress(*reqBuf);
338 
339  // Check that the request is accepted
340  EXPECT_EQ(callbacks.streamErrors, 0);
341  EXPECT_EQ(callbacks.messageBegin, 1);
342  EXPECT_EQ(callbacks.headersComplete, 1);
343 
344 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
CodecFactory codec
void writeBuf(const Buf &buf, folly::io::Appender &out)
static Options cacheChainLength()
Definition: IOBufQueue.h:83
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 ( HTTP1xCodecTest  ,
TestMultipleDistinctContentLengthHeaders   
)

Definition at line 346 of file HTTP1xCodecTest.cpp.

References folly::IOBufQueue::cacheChainLength(), codec, folly::IOBuf::copyBuffer(), proxygen::DOWNSTREAM, EXPECT_EQ, proxygen::FakeHTTPCodecCallback::headersComplete, proxygen::FakeHTTPCodecCallback::lastParseError, proxygen::FakeHTTPCodecCallback::messageBegin, proxygen::HTTP1xCodec::onIngress(), proxygen::HTTP1xCodec::setCallback(), proxygen::FakeHTTPCodecCallback::streamErrors, and fizz::detail::writeBuf().

346  {
347  HTTP1xCodec codec(TransportDirection::DOWNSTREAM);
348  FakeHTTPCodecCallback callbacks;
349  codec.setCallback(&callbacks);
351 
352  // Generate a POST request with two distinct Content-Length headers
353  auto reqBuf = folly::IOBuf::copyBuffer(
354  "POST /www.facebook.com HTTP/1.1\r\nHost: www.facebook.com\r\n"
355  "Content-Length: 5\r\nContent-Length: 6\r\n\r\n");
356  codec.onIngress(*reqBuf);
357 
358  // Check that the request fails before the codec finishes parsing the headers
359  EXPECT_EQ(callbacks.streamErrors, 1);
360  EXPECT_EQ(callbacks.messageBegin, 1);
361  EXPECT_EQ(callbacks.headersComplete, 0);
362  EXPECT_EQ(callbacks.lastParseError->getHttpStatusCode(), 400);
363 }
std::unique_ptr< HTTPException > lastParseError
Definition: TestUtils.h:405
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
CodecFactory codec
void writeBuf(const Buf &buf, folly::io::Appender &out)
static Options cacheChainLength()
Definition: IOBufQueue.h:83
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 ( HTTP1xCodecTest  ,
TestCorrectTransferEncodingHeader   
)

Definition at line 365 of file HTTP1xCodecTest.cpp.

References folly::IOBufQueue::cacheChainLength(), folly::IOBuf::copyBuffer(), proxygen::DOWNSTREAM, EXPECT_EQ, proxygen::FakeHTTPCodecCallback::headersComplete, proxygen::FakeHTTPCodecCallback::messageBegin, proxygen::HTTP1xCodec::onIngress(), proxygen::HTTP1xCodec::setCallback(), proxygen::FakeHTTPCodecCallback::streamErrors, and fizz::detail::writeBuf().

365  {
366  HTTP1xCodec downstream(TransportDirection::DOWNSTREAM);
367  FakeHTTPCodecCallback callbacks;
368  downstream.setCallback(&callbacks);
370 
371  // Generate a POST request with folded
372  auto reqBuf = folly::IOBuf::copyBuffer(
373  "POST /www.facebook.com HTTP/1.1\r\nHost: www.facebook.com\r\n"
374  "Transfer-Encoding: chunked\r\n\r\n");
375  downstream.onIngress(*reqBuf);
376 
377  // Check that the request fails before the codec finishes parsing the headers
378  EXPECT_EQ(callbacks.streamErrors, 0);
379  EXPECT_EQ(callbacks.messageBegin, 1);
380  EXPECT_EQ(callbacks.headersComplete, 1);
381 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void writeBuf(const Buf &buf, folly::io::Appender &out)
static Options cacheChainLength()
Definition: IOBufQueue.h:83
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 ( HTTP1xCodecTest  ,
TestFoldedTransferEncodingHeader   
)

Definition at line 383 of file HTTP1xCodecTest.cpp.

References folly::IOBufQueue::cacheChainLength(), folly::IOBuf::copyBuffer(), proxygen::DOWNSTREAM, EXPECT_EQ, proxygen::FakeHTTPCodecCallback::headersComplete, proxygen::FakeHTTPCodecCallback::lastParseError, proxygen::FakeHTTPCodecCallback::messageBegin, proxygen::HTTP1xCodec::onIngress(), proxygen::HTTP1xCodec::setCallback(), proxygen::FakeHTTPCodecCallback::streamErrors, and fizz::detail::writeBuf().

383  {
384  HTTP1xCodec downstream(TransportDirection::DOWNSTREAM);
385  FakeHTTPCodecCallback callbacks;
386  downstream.setCallback(&callbacks);
388 
389  // Generate a POST request with folded
390  auto reqBuf = folly::IOBuf::copyBuffer(
391  "POST /www.facebook.com HTTP/1.1\r\nHost: www.facebook.com\r\n"
392  "Transfer-Encoding: \r\n chunked\r\nContent-Length: 8\r\n\r\n");
393  downstream.onIngress(*reqBuf);
394 
395  // Check that the request fails before the codec finishes parsing the headers
396  EXPECT_EQ(callbacks.streamErrors, 1);
397  EXPECT_EQ(callbacks.messageBegin, 1);
398  EXPECT_EQ(callbacks.headersComplete, 0);
399  EXPECT_EQ(callbacks.lastParseError->getHttpStatusCode(), 400);
400 }
std::unique_ptr< HTTPException > lastParseError
Definition: TestUtils.h:405
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void writeBuf(const Buf &buf, folly::io::Appender &out)
static Options cacheChainLength()
Definition: IOBufQueue.h:83
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 ( HTTP1xCodecTest  ,
TestBadTransferEncodingHeader   
)

Definition at line 402 of file HTTP1xCodecTest.cpp.

References folly::IOBufQueue::cacheChainLength(), folly::IOBuf::copyBuffer(), proxygen::DOWNSTREAM, EXPECT_EQ, proxygen::FakeHTTPCodecCallback::headersComplete, proxygen::FakeHTTPCodecCallback::lastParseError, proxygen::FakeHTTPCodecCallback::messageBegin, proxygen::HTTP1xCodec::onIngress(), proxygen::HTTP1xCodec::setCallback(), proxygen::FakeHTTPCodecCallback::streamErrors, and fizz::detail::writeBuf().

402  {
403  HTTP1xCodec downstream(TransportDirection::DOWNSTREAM);
404  FakeHTTPCodecCallback callbacks;
405  downstream.setCallback(&callbacks);
407 
408  auto reqBuf = folly::IOBuf::copyBuffer(
409  "POST /www.facebook.com HTTP/1.1\r\nHost: www.facebook.com\r\n"
410  "Transfer-Encoding: chunked, zorg\r\n\r\n");
411  downstream.onIngress(*reqBuf);
412 
413  // Check that the request fails before the codec finishes parsing the headers
414  EXPECT_EQ(callbacks.streamErrors, 1);
415  EXPECT_EQ(callbacks.messageBegin, 1);
416  EXPECT_EQ(callbacks.headersComplete, 0);
417  EXPECT_EQ(callbacks.lastParseError->getHttpStatusCode(), 400);
418 }
std::unique_ptr< HTTPException > lastParseError
Definition: TestUtils.h:405
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void writeBuf(const Buf &buf, folly::io::Appender &out)
static Options cacheChainLength()
Definition: IOBufQueue.h:83
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 ( HTTP1xCodecTest  ,
Test1xxConnectionHeader   
)

Definition at line 420 of file HTTP1xCodecTest.cpp.

References proxygen::HTTPHeaders::add(), folly::IOBufQueue::cacheChainLength(), proxygen::HTTP1xCodec::createStream(), proxygen::DOWNSTREAM, EXPECT_EQ, folly::IOBufQueue::front(), proxygen::HTTP1xCodec::generateHeader(), proxygen::HTTPMessage::getHeaders(), HTTP1xCodecCallback::headersComplete, proxygen::HTTP_HEADER_CONNECTION, proxygen::HTTP_HEADER_CONTENT_LENGTH, folly::IOBufQueue::move(), HTTP1xCodecCallback::msg_, proxygen::HTTP1xCodec::onIngress(), proxygen::HTTPHeaders::remove(), proxygen::HTTP1xCodec::setCallback(), proxygen::HTTPMessage::setHTTPVersion(), proxygen::HTTPMessage::setStatusCode(), streamID, proxygen::UPSTREAM, and fizz::detail::writeBuf().

420  {
421  HTTP1xCodec upstream(TransportDirection::UPSTREAM);
422  HTTP1xCodec downstream(TransportDirection::DOWNSTREAM);
423  HTTP1xCodecCallback callbacks;
424  upstream.setCallback(&callbacks);
425  HTTPMessage resp;
426  resp.setStatusCode(100);
427  resp.setHTTPVersion(1, 1);
428  resp.getHeaders().add(HTTP_HEADER_CONNECTION, "Upgrade");
430  auto streamID = downstream.createStream();
431  downstream.generateHeader(writeBuf, streamID, resp);
432  upstream.onIngress(*writeBuf.front());
433  EXPECT_EQ(callbacks.headersComplete, 1);
434  EXPECT_EQ(
435  callbacks.msg_->getHeaders().getSingleOrEmpty(HTTP_HEADER_CONNECTION),
436  "Upgrade");
437  resp.setStatusCode(200);
440  writeBuf.move();
441  downstream.generateHeader(writeBuf, streamID, resp);
442  upstream.onIngress(*writeBuf.front());
443  EXPECT_EQ(callbacks.headersComplete, 2);
444  EXPECT_EQ(
445  callbacks.msg_->getHeaders().getSingleOrEmpty(HTTP_HEADER_CONNECTION),
446  "keep-alive");
447 }
bool remove(folly::StringPiece name)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void writeBuf(const Buf &buf, folly::io::Appender &out)
static Options cacheChainLength()
Definition: IOBufQueue.h:83
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
void setHTTPVersion(uint8_t major, uint8_t minor)
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
uint32_t streamID
Definition: SPDYCodec.cpp:131
std::unique_ptr< HTTPMessage > msg_
void setStatusCode(uint16_t status)
TEST ( HTTP1xCodecTest  ,
TestChainedBody   
)

Definition at line 449 of file HTTP1xCodecTest.cpp.

References testing::_, folly::IOBufQueue::append(), codec, folly::IOBuf::copyBuffer(), proxygen::DOWNSTREAM, folly::IOBufQueue::empty(), EXPECT_TRUE, folly::IOBufQueue::front(), testing::Invoke(), ON_CALL, proxygen::HTTP1xCodec::onIngress(), proxygen::HTTP1xCodec::setCallback(), folly::IOBufQueue::trimStart(), and uint16_t.

449  {
450  HTTP1xCodec codec(TransportDirection::DOWNSTREAM);
451  MockHTTPCodecCallback callbacks;
452  codec.setCallback(&callbacks);
453 
454  folly::IOBufQueue bodyQueue;
455  ON_CALL(callbacks, onBody(1, _, _))
456  .WillByDefault(Invoke(
457  [&bodyQueue](HTTPCodec::StreamID, std::shared_ptr<folly::IOBuf> buf,
458  uint16_t) { bodyQueue.append(buf->clone()); }));
459 
460  folly::IOBufQueue reqQueue;
462  "POST /test.php HTTP/1.1\r\nHost: www.test.com\r\n"
463  "Content-Length: 10\r\n\r\nabcde"));
464  reqQueue.append(folly::IOBuf::copyBuffer("fghij"));
465 
466  while (!reqQueue.empty()) {
467  auto processed = codec.onIngress(*reqQueue.front());
468  if (processed == 0) {
469  break;
470  }
471  reqQueue.trimStart(processed);
472  }
473 
474  EXPECT_TRUE(folly::IOBufEqualTo()(*bodyQueue.front(),
475  *folly::IOBuf::copyBuffer("abcdefghij")));
476 }
const folly::IOBuf * front() const
Definition: IOBufQueue.h:476
void append(std::unique_ptr< folly::IOBuf > &&buf, bool pack=false)
Definition: IOBufQueue.cpp:143
bool empty() const
Definition: IOBufQueue.h:503
CodecFactory codec
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define ON_CALL(obj, call)
void trimStart(size_t amount)
Definition: IOBufQueue.cpp:255
uint64_t StreamID
Definition: HTTPCodec.h:49
const internal::AnythingMatcher _
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 ( HTTP1xCodecTest  ,
TestIgnoreUpstreamUpgrade   
)

Definition at line 478 of file HTTP1xCodecTest.cpp.

References proxygen::FakeHTTPCodecCallback::bodyLength, folly::IOBufQueue::cacheChainLength(), codec, folly::IOBuf::copyBuffer(), EXPECT_EQ, proxygen::FakeHTTPCodecCallback::headersComplete, proxygen::FakeHTTPCodecCallback::messageBegin, proxygen::HTTP1xCodec::onIngress(), proxygen::HTTP1xCodec::setCallback(), proxygen::FakeHTTPCodecCallback::streamErrors, proxygen::UPSTREAM, and fizz::detail::writeBuf().

478  {
479  HTTP1xCodec codec(TransportDirection::UPSTREAM);
480  FakeHTTPCodecCallback callbacks;
481  codec.setCallback(&callbacks);
483 
484  auto reqBuf = folly::IOBuf::copyBuffer(
485  "HTTP/1.1 200 OK\r\n"
486  "Connection: close\r\n"
487  "Upgrade: h2,h2c\r\n"
488  "\r\n"
489  "<!DOCTYPE html>");
490  codec.onIngress(*reqBuf);
491 
492  EXPECT_EQ(callbacks.streamErrors, 0);
493  EXPECT_EQ(callbacks.messageBegin, 1);
494  EXPECT_EQ(callbacks.headersComplete, 1);
495  EXPECT_EQ(callbacks.bodyLength, 15);
496 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
CodecFactory codec
void writeBuf(const Buf &buf, folly::io::Appender &out)
static Options cacheChainLength()
Definition: IOBufQueue.h:83
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 ( HTTP1xCodecTest  ,
WebsocketUpgrade   
)

Definition at line 498 of file HTTP1xCodecTest.cpp.

References folly::IOBufQueue::clear(), proxygen::HTTP1xCodec::createStream(), proxygen::DOWNSTREAM, proxygen::empty_string, EXPECT_EQ, EXPECT_NE, EXPECT_TRUE, folly::IOBufQueue::front(), proxygen::HTTP1xCodec::generateHeader(), HTTP1xCodecCallback::headersComplete, proxygen::HTTP_HEADER_SEC_WEBSOCKET_ACCEPT, proxygen::HTTP_HEADER_SEC_WEBSOCKET_KEY, HTTP1xCodecCallback::msg_, proxygen::HTTP1xCodec::onIngress(), proxygen::HTTP1xCodec::setCallback(), proxygen::HTTPMessage::setEgressWebsocketUpgrade(), proxygen::HTTPMessage::setHTTPVersion(), proxygen::HTTPMessage::setStatusCode(), proxygen::HTTPMessage::setURL(), streamID, and proxygen::UPSTREAM.

498  {
499  HTTP1xCodec upstreamCodec(TransportDirection::UPSTREAM);
500  HTTP1xCodec downstreamCodec(TransportDirection::DOWNSTREAM);
501  HTTP1xCodecCallback downStreamCallbacks;
502  HTTP1xCodecCallback upstreamCallbacks;
503  downstreamCodec.setCallback(&downStreamCallbacks);
504  upstreamCodec.setCallback(&upstreamCallbacks);
505 
506  HTTPMessage req;
507  req.setHTTPVersion(1, 1);
508  req.setURL("/websocket");
510  folly::IOBufQueue buf;
511  auto streamID = upstreamCodec.createStream();
512  upstreamCodec.generateHeader(buf, streamID, req);
513 
514  downstreamCodec.onIngress(*buf.front());
515  EXPECT_EQ(downStreamCallbacks.headersComplete, 1);
516  EXPECT_TRUE(downStreamCallbacks.msg_->isIngressWebsocketUpgrade());
517  auto& headers = downStreamCallbacks.msg_->getHeaders();
518  auto ws_key_header = headers.getSingleOrEmpty(HTTP_HEADER_SEC_WEBSOCKET_KEY);
519  EXPECT_NE(ws_key_header, empty_string);
520 
521  HTTPMessage resp;
522  resp.setHTTPVersion(1, 1);
523  resp.setStatusCode(101);
525  buf.clear();
526  downstreamCodec.generateHeader(buf, streamID, resp);
527  upstreamCodec.onIngress(*buf.front());
528  EXPECT_EQ(upstreamCallbacks.headersComplete, 1);
529  headers = upstreamCallbacks.msg_->getHeaders();
530  auto ws_accept_header =
531  headers.getSingleOrEmpty(HTTP_HEADER_SEC_WEBSOCKET_ACCEPT);
532  EXPECT_NE(ws_accept_header, empty_string);
533 }
const folly::IOBuf * front() const
Definition: IOBufQueue.h:476
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
ParseURL setURL(T &&url)
Definition: HTTPMessage.h:183
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
const std::string empty_string
Definition: HTTPHeaders.cpp:23
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
void setHTTPVersion(uint8_t major, uint8_t minor)
void setEgressWebsocketUpgrade()
Definition: HTTPMessage.h:66
uint32_t streamID
Definition: SPDYCodec.cpp:131
std::unique_ptr< HTTPMessage > msg_
void setStatusCode(uint16_t status)
TEST ( HTTP1xCodecTest  ,
WebsocketUpgradeKeyError   
)

Definition at line 535 of file HTTP1xCodecTest.cpp.

References codec, folly::IOBuf::copyBuffer(), proxygen::HTTP1xCodec::createStream(), HTTP1xCodecCallback::errors, EXPECT_EQ, proxygen::HTTP1xCodec::generateHeader(), HTTP1xCodecCallback::headersComplete, proxygen::HTTP1xCodec::onIngress(), proxygen::HTTP1xCodec::setCallback(), proxygen::HTTPMessage::setEgressWebsocketUpgrade(), proxygen::HTTPMessage::setHTTPVersion(), proxygen::HTTPMessage::setURL(), streamID, and proxygen::UPSTREAM.

535  {
536  HTTP1xCodec codec(TransportDirection::UPSTREAM);
537  HTTP1xCodecCallback callbacks;
538  codec.setCallback(&callbacks);
539 
540  HTTPMessage req;
541  req.setHTTPVersion(1, 1);
542  req.setURL("/websocket");
544  folly::IOBufQueue buf;
545  auto streamID = codec.createStream();
546  codec.generateHeader(buf, streamID, req);
547 
548  auto resp = folly::IOBuf::copyBuffer(
549  "HTTP/1.1 200 OK\r\n"
550  "Connection: upgrade\r\n"
551  "Upgrade: websocket\r\n"
552  "\r\n");
553  codec.onIngress(*resp);
554  EXPECT_EQ(callbacks.headersComplete, 0);
555  EXPECT_EQ(callbacks.errors, 1);
556 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
CodecFactory codec
ParseURL setURL(T &&url)
Definition: HTTPMessage.h:183
void setHTTPVersion(uint8_t major, uint8_t minor)
void setEgressWebsocketUpgrade()
Definition: HTTPMessage.h:66
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
uint32_t streamID
Definition: SPDYCodec.cpp:131
TEST ( HTTP1xCodecTest  ,
WebsocketUpgradeHeaderSet   
)

Definition at line 558 of file HTTP1xCodecTest.cpp.

References proxygen::HTTPHeaders::add(), proxygen::HTTP1xCodec::createStream(), proxygen::DOWNSTREAM, proxygen::empty_string, EXPECT_EQ, folly::IOBufQueue::front(), proxygen::HTTP1xCodec::generateHeader(), proxygen::GET, proxygen::HTTPMessage::getHeaders(), proxygen::HTTP_HEADER_SEC_WEBSOCKET_KEY, proxygen::HTTP_HEADER_UPGRADE, HTTP1xCodecCallback::msg_, proxygen::HTTP1xCodec::onIngress(), proxygen::HTTP1xCodec::setCallback(), proxygen::HTTPMessage::setEgressWebsocketUpgrade(), proxygen::HTTPMessage::setMethod(), proxygen::HTTPMessage::setURL(), and proxygen::UPSTREAM.

558  {
559  HTTP1xCodec upstreamCodec(TransportDirection::UPSTREAM);
560  HTTPMessage req;
561  req.setMethod(HTTPMethod::GET);
562  req.setURL("/websocket");
564  req.getHeaders().add(proxygen::HTTP_HEADER_UPGRADE, "Websocket");
565 
566  folly::IOBufQueue buf;
567  upstreamCodec.generateHeader(buf, upstreamCodec.createStream(), req);
568 
569  HTTP1xCodec downstreamCodec(TransportDirection::DOWNSTREAM);
570  HTTP1xCodecCallback callbacks;
571  downstreamCodec.setCallback(&callbacks);
572  downstreamCodec.onIngress(*buf.front());
573  auto headers = callbacks.msg_->getHeaders();
574  EXPECT_EQ(headers.getSingleOrEmpty(HTTP_HEADER_SEC_WEBSOCKET_KEY),
575  empty_string);
576 }
const folly::IOBuf * front() const
Definition: IOBufQueue.h:476
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
ParseURL setURL(T &&url)
Definition: HTTPMessage.h:183
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
void setMethod(HTTPMethod method)
const std::string empty_string
Definition: HTTPHeaders.cpp:23
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
void setEgressWebsocketUpgrade()
Definition: HTTPMessage.h:66
std::unique_ptr< HTTPMessage > msg_
TEST ( HTTP1xCodecTest  ,
WebsocketConnectionHeader   
)

Definition at line 578 of file HTTP1xCodecTest.cpp.

References proxygen::HTTPHeaders::add(), proxygen::HTTP1xCodec::createStream(), proxygen::Base64::decode(), proxygen::DOWNSTREAM, proxygen::empty_string, EXPECT_EQ, EXPECT_NE, EXPECT_NO_THROW, folly::IOBufQueue::front(), proxygen::HTTP1xCodec::generateHeader(), proxygen::GET, proxygen::HTTPMessage::getHeaders(), proxygen::HTTP_HEADER_CONNECTION, proxygen::HTTP_HEADER_SEC_WEBSOCKET_ACCEPT, proxygen::HTTP_HEADER_SEC_WEBSOCKET_KEY, HTTP1xCodecCallback::msg_, proxygen::HTTP1xCodec::onIngress(), proxygen::HTTP1xCodec::setCallback(), proxygen::HTTPMessage::setEgressWebsocketUpgrade(), proxygen::HTTPMessage::setMethod(), proxygen::HTTPMessage::setURL(), and proxygen::UPSTREAM.

578  {
579  HTTP1xCodec upstreamCodec(TransportDirection::UPSTREAM);
580  HTTPMessage req;
581  req.setMethod(HTTPMethod::GET);
582  req.setURL("/websocket");
584  req.getHeaders().add(proxygen::HTTP_HEADER_CONNECTION, "upgrade, keep-alive");
586  "key should change");
588  "this should not be found");
589 
590  folly::IOBufQueue buf;
591  upstreamCodec.generateHeader(buf, upstreamCodec.createStream(), req);
592  HTTP1xCodec downstreamCodec(TransportDirection::DOWNSTREAM);
593  HTTP1xCodecCallback callbacks;
594  downstreamCodec.setCallback(&callbacks);
595  downstreamCodec.onIngress(*buf.front());
596  auto headers = callbacks.msg_->getHeaders();
597  auto ws_sec_key = headers.getSingleOrEmpty(HTTP_HEADER_SEC_WEBSOCKET_KEY);
598  EXPECT_NE(ws_sec_key, empty_string);
599  EXPECT_NE(ws_sec_key, "key should change");
600 
601  // We know the key is length 16
602  // https://tools.ietf.org/html/rfc6455#section-4.2.1.5
603  // for base64 % 3 leaves 1 byte so we expect padding of '=='
604  // hence this offset of 2 explicitly
605  EXPECT_NO_THROW(Base64::decode(ws_sec_key, 2));
606  EXPECT_EQ(16, Base64::decode(ws_sec_key, 2).length());
607 
608  EXPECT_EQ(headers.getSingleOrEmpty(HTTP_HEADER_SEC_WEBSOCKET_ACCEPT),
609  empty_string);
610  EXPECT_EQ(headers.getSingleOrEmpty(HTTP_HEADER_CONNECTION),
611  "upgrade, keep-alive");
612 }
const folly::IOBuf * front() const
Definition: IOBufQueue.h:476
#define EXPECT_NO_THROW(statement)
Definition: gtest.h:1845
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TokenBindingMessage decode(folly::io::Cursor &cursor)
Definition: Types.cpp:132
ParseURL setURL(T &&url)
Definition: HTTPMessage.h:183
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
void setMethod(HTTPMethod method)
const std::string empty_string
Definition: HTTPHeaders.cpp:23
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
void setEgressWebsocketUpgrade()
Definition: HTTPMessage.h:66
std::unique_ptr< HTTPMessage > msg_
TEST ( HTTP1xCodecTest  ,
TrailersAndEomAreNotGeneratedWhenNonChunked   
)

Definition at line 614 of file HTTP1xCodecTest.cpp.

References proxygen::HTTPHeaders::add(), codec, proxygen::HTTP1xCodec::createStream(), EXPECT_EQ, proxygen::HTTP1xCodec::generateEOM(), proxygen::HTTP1xCodec::generateHeader(), proxygen::HTTP1xCodec::generateTrailers(), proxygen::HTTPMessage::getHeaders(), proxygen::HTTP_HEADER_HOST, proxygen::HTTPHeaders::set(), proxygen::HTTPMessage::setHTTPVersion(), proxygen::HTTPMessage::setIsChunked(), proxygen::HTTPMessage::setURL(), and proxygen::UPSTREAM.

614  {
615  // Verify that generateTrailes and generateEom result in 0 bytes
616  // generated when message is not chunked.
617  // HTTP2 codec handles all BODY as regular non-chunked body, thus
618  // HTTPTransation SM transitions allow trailers after regular body. Which
619  // is not allowed in HTTP1.
620  HTTP1xCodec codec(TransportDirection::UPSTREAM);
621 
622  auto txnID = codec.createStream();
623 
624  HTTPMessage msg;
625  msg.setHTTPVersion(1, 1);
626  msg.setURL("https://www.facebook.com/");
627  msg.getHeaders().set(HTTP_HEADER_HOST, "www.facebook.com");
628  msg.setIsChunked(false);
629 
630  folly::IOBufQueue buf;
631  codec.generateHeader(buf, txnID, msg);
632 
633  HTTPHeaders trailers;
634  trailers.add("X-Test-Trailer", "test");
635  EXPECT_EQ(0, codec.generateTrailers(buf, txnID, trailers));
636  EXPECT_EQ(0, codec.generateEOM(buf, txnID));
637 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
CodecFactory codec
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
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
void setHTTPVersion(uint8_t major, uint8_t minor)
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
TEST_P ( ConnectionHeaderTest  ,
TestConnectionHeaders   
)

Definition at line 645 of file HTTP1xCodecTest.cpp.

References proxygen::HTTPHeaders::add(), folly::IOBufQueue::cacheChainLength(), proxygen::HTTP1xCodec::createStream(), proxygen::DOWNSTREAM, EXPECT_EQ, folly::IOBufQueue::front(), proxygen::HTTP1xCodec::generateHeader(), proxygen::GET, proxygen::HTTPMessage::getHeaders(), HTTP1xCodecCallback::headersComplete, proxygen::HTTP_HEADER_CONNECTION, proxygen::INSTANTIATE_TEST_CASE_P(), HTTP1xCodecCallback::msg_, proxygen::HTTP1xCodec::onIngress(), proxygen::HTTP1xCodec::setCallback(), proxygen::HTTPMessage::setMethod(), proxygen::HTTPMessage::setURL(), proxygen::UPSTREAM, val, and fizz::detail::writeBuf().

645  {
646  HTTP1xCodec upstream(TransportDirection::UPSTREAM);
647  HTTP1xCodec downstream(TransportDirection::DOWNSTREAM);
648  HTTP1xCodecCallback callbacks;
649  downstream.setCallback(&callbacks);
650  HTTPMessage req;
651  req.setMethod(HTTPMethod::GET);
652  req.setURL("/");
653  auto val = GetParam();
654  for (auto header: val.first) {
655  req.getHeaders().add(HTTP_HEADER_CONNECTION, header);
656  }
658  upstream.generateHeader(writeBuf, upstream.createStream(), req);
659  downstream.onIngress(*writeBuf.front());
660  EXPECT_EQ(callbacks.headersComplete, 1);
661  auto& headers = callbacks.msg_->getHeaders();
662  EXPECT_EQ(headers.getSingleOrEmpty(HTTP_HEADER_CONNECTION),
663  val.second);
664 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
double val
Definition: String.cpp:273
void writeBuf(const Buf &buf, folly::io::Appender &out)
ParseURL setURL(T &&url)
Definition: HTTPMessage.h:183
static Options cacheChainLength()
Definition: IOBufQueue.h:83
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
void setMethod(HTTPMethod method)
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
std::unique_ptr< HTTPMessage > msg_