proxygen
TestUtils.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  */
13 
14 #include <boost/optional/optional_io.hpp>
15 #include <folly/Random.h>
16 #include <folly/io/Cursor.h>
17 
18 using namespace folly::io;
19 using namespace folly;
20 using namespace std;
21 using namespace testing;
22 
23 namespace proxygen {
24 
26  {SettingsId::INITIAL_WINDOW_SIZE, 65536}
27 };
28 
29 std::unique_ptr<HTTPMessage> getPriorityMessage(uint8_t priority) {
30  auto ret = std::make_unique<HTTPMessage>();
31  ret->setAdvancedProtocolString(spdy::kVersionStrv2);
32  ret->setPriority(priority);
33  return ret;
34 }
35 
36 std::unique_ptr<folly::IOBuf> makeBuf(uint32_t size) {
37  auto out = folly::IOBuf::create(size);
38  out->append(size);
39  // fill with random junk
40  RWPrivateCursor cursor(out.get());
41  while (cursor.length() >= 8) {
43  }
44  while (cursor.length()) {
45  cursor.write<uint8_t>((uint8_t)folly::Random::rand32());
46  }
47  return out;
48 }
49 
50 std::unique_ptr<testing::NiceMock<MockHTTPCodec>>
52  auto codec = std::make_unique<testing::NiceMock<MockHTTPCodec>>();
53  EXPECT_CALL(*codec, supportsParallelRequests())
54  .WillRepeatedly(testing::Return(true));
55  EXPECT_CALL(*codec, getProtocol())
56  .WillRepeatedly(testing::Return(CodecProtocol::SPDY_3_1));
57  EXPECT_CALL(*codec, isReusable())
58  .WillRepeatedly(testing::Return(true));
59  EXPECT_CALL(*codec, getTransportDirection())
60  .WillRepeatedly(testing::Return(dir));
61  EXPECT_CALL(*codec, getIngressSettings())
62  .WillRepeatedly(testing::Return(&kDefaultIngressSettings));
63  return codec;
64 }
65 
66 std::unique_ptr<testing::NiceMock<MockHTTPCodec>>
68  return makeMockParallelCodec(TransportDirection::DOWNSTREAM);
69 }
70 
71 std::unique_ptr<testing::NiceMock<MockHTTPCodec>>
73  return makeMockParallelCodec(TransportDirection::UPSTREAM);
74 }
75 
77  HTTPMessage req;
78  req.setMethod("GET");
79  req.setURL(url);
80  req.setHTTPVersion(1, 1);
81  req.getHeaders().set(HTTP_HEADER_HOST, "www.foo.com");
82  return req;
83 }
84 
86  HTTPMessage req;
87  req.setMethod("GET");
88  req.setURL(url);
89  req.setHTTPVersion(1, 1);
90  req.getHeaders().set(HTTP_HEADER_HOST, "www.foo.com");
91  req.getHeaders().add(HTTP_HEADER_USER_AGENT, "coolio");
92  req.getHeaders().add(
93  "x-huge-header",
95  return req;
96 }
97 
98 std::unique_ptr<HTTPMessage> makeGetRequest() {
99  return std::make_unique<HTTPMessage>(getGetRequest());
100 }
101 
103  HTTPMessage req;
104  req.setMethod("POST");
105  req.setURL<string>("/");
106  req.setHTTPVersion(1, 1);
107  req.getHeaders().set(HTTP_HEADER_HOST, "www.foo.com");
109  folly::to<string>(contentLength));
110  return req;
111 }
112 
114  HTTPMessage req;
115  req.setMethod("POST");
116  req.setURL<string>("/");
117  req.setHTTPVersion(1, 1);
118  req.setIsChunked(true);
119  req.getHeaders().set(HTTP_HEADER_HOST, "www.foo.com");
120  req.getHeaders().set(HTTP_HEADER_TRANSFER_ENCODING, "chunked");
121  return req;
122 }
123 
124 std::unique_ptr<HTTPMessage> makePostRequest(uint32_t contentLength) {
125  return std::make_unique<HTTPMessage>(getPostRequest(contentLength));
126 }
127 
129  HTTPMessage req;
130  req.setMethod("PUB");
131  req.setURL(url);
132  req.setHTTPVersion(1, 1);
133  req.getHeaders().set(HTTP_HEADER_HOST, "www.foo.com");
134  return req;
135 }
136 
138  HTTPMessage resp;
139  resp.setStatusCode(code);
140  if (bodyLen > 0) {
142  folly::to<string>(bodyLen));
143  }
144  return resp;
145 }
146 
147 std::unique_ptr<HTTPMessage> makeResponse(uint16_t statusCode) {
148  auto resp = std::make_unique<HTTPMessage>();
149  resp->setStatusCode(statusCode);
150  resp->setHTTPVersion(1, 1);
151  return resp;
152 }
153 
154 std::tuple<std::unique_ptr<HTTPMessage>, std::unique_ptr<folly::IOBuf> >
156  auto resp = makeResponse(statusCode);
157  resp->getHeaders().set(HTTP_HEADER_CONTENT_LENGTH, folly::to<string>(len));
158  return std::make_pair(std::move(resp), makeBuf(len));
159 }
160 
162  HTTPMethod method, uint32_t bodyLen) {
163  HTTPMessage req = getGetRequest();
164  req.setMethod(method);
165  req.getHeaders().set(HTTP_HEADER_UPGRADE, upgradeHeader);
166  if (bodyLen > 0) {
168  folly::to<std::string>(bodyLen));
169  }
170  return req;
171 }
172 
174  // For each generate* function, write some data to the chain
175  EXPECT_CALL(codec, generateHeader(_, _, _, _, _))
176  .WillRepeatedly(Invoke(
178  HTTPCodec::StreamID /*stream*/,
179  const HTTPMessage& /*msg*/,
180  bool /*eom*/,
181  HTTPHeaderSize* /*size*/) { writeBuf.append(makeBuf(10)); }));
182 
183  EXPECT_CALL(codec, generatePushPromise(_, _, _, _, _, _))
184  .WillRepeatedly(Invoke(
185  [](folly::IOBufQueue& writeBuf,
186  HTTPCodec::StreamID /*stream*/,
187  const HTTPMessage& /*msg*/,
188  HTTPCodec::StreamID /*assocStream*/,
189  bool /*eom*/,
190  HTTPHeaderSize* /*size*/) { writeBuf.append(makeBuf(10)); }));
191 
192  EXPECT_CALL(codec, generateBody(_, _, _, _, _))
193  .WillRepeatedly(Invoke([](folly::IOBufQueue& writeBuf,
194  HTTPCodec::StreamID /*stream*/,
195  std::shared_ptr<folly::IOBuf> chain,
196  folly::Optional<uint8_t> /*padding*/,
197  bool /*eom*/) {
198  auto len = chain->computeChainDataLength();
199  writeBuf.append(chain->clone());
200  return len;
201  }));
202 
203  EXPECT_CALL(codec, generateChunkHeader(_, _, _))
204  .WillRepeatedly(Invoke([](folly::IOBufQueue& writeBuf,
205  HTTPCodec::StreamID /*stream*/,
206  size_t length) {
207  writeBuf.append(makeBuf(length));
208  return length;
209  }));
210 
211  EXPECT_CALL(codec, generateChunkTerminator(_, _))
212  .WillRepeatedly(Invoke(
213  [](folly::IOBufQueue& writeBuf, HTTPCodec::StreamID /*stream*/) {
214  writeBuf.append(makeBuf(4));
215  return 4;
216  }));
217 
218  EXPECT_CALL(codec, generateTrailers(_, _, _))
219  .WillRepeatedly(Invoke([](folly::IOBufQueue& writeBuf,
220  HTTPCodec::StreamID /*stream*/,
221  const HTTPHeaders& /*trailers*/) {
222  writeBuf.append(makeBuf(30));
223  return 30;
224  }));
225 
226  EXPECT_CALL(codec, generateEOM(_, _))
227  .WillRepeatedly(Invoke(
228  [](folly::IOBufQueue& writeBuf, HTTPCodec::StreamID /*stream*/) {
229  writeBuf.append(makeBuf(6));
230  return 6;
231  }));
232 
233  EXPECT_CALL(codec, generateRstStream(_, _, _))
234  .WillRepeatedly(Invoke([](folly::IOBufQueue& writeBuf,
235  HTTPCodec::StreamID /*stream*/,
236  ErrorCode /*code*/) {
237  writeBuf.append(makeBuf(6));
238  return 6;
239  }));
240 
241  EXPECT_CALL(codec, generateGoaway(_, _, _, _))
242  .WillRepeatedly(Invoke([](folly::IOBufQueue& writeBuf,
243  uint32_t /*lastStream*/,
244  ErrorCode,
245  std::shared_ptr<folly::IOBuf>) {
246  writeBuf.append(makeBuf(6));
247  return 6;
248  }));
249 
250  EXPECT_CALL(codec, generatePingRequest(_))
251  .WillRepeatedly(Invoke([] (folly::IOBufQueue& writeBuf) {
252  writeBuf.append(makeBuf(6));
253  return 6;
254  }));
255 
256  EXPECT_CALL(codec, generatePingReply(_, _))
257  .WillRepeatedly(Invoke([](folly::IOBufQueue& writeBuf, uint64_t /*id*/) {
258  writeBuf.append(makeBuf(6));
259  return 6;
260  }));
261 
262  EXPECT_CALL(codec, generateSettings(_))
263  .WillRepeatedly(Invoke([] (folly::IOBufQueue& writeBuf) {
264  writeBuf.append(makeBuf(6));
265  return 6;
266  }));
267 
268  EXPECT_CALL(codec, generateWindowUpdate(_, _, _))
269  .WillRepeatedly(Invoke([](folly::IOBufQueue& writeBuf,
270  HTTPCodec::StreamID /*stream*/,
271  uint32_t /*delta*/) {
272  writeBuf.append(makeBuf(6));
273  return 6;
274  }));
275 
276  EXPECT_CALL(codec, generateCertificateRequest(_, _, _))
277  .WillRepeatedly(Invoke([](folly::IOBufQueue& writeBuf,
278  uint16_t /*requestId*/,
279  std::shared_ptr<folly::IOBuf>) {
280  writeBuf.append(makeBuf(6));
281  return 6;
282  }));
283 
284  EXPECT_CALL(codec, generateCertificate(_, _, _))
285  .WillRepeatedly(Invoke([](folly::IOBufQueue& writeBuf,
286  uint16_t /*certId*/,
287  std::shared_ptr<folly::IOBuf>) {
288  writeBuf.append(makeBuf(6));
289  return 6;
290  }));
291 }
292 }
std::unique_ptr< testing::NiceMock< MockHTTPCodec > > makeDownstreamParallelCodec()
Definition: TestUtils.cpp:67
void append(std::unique_ptr< folly::IOBuf > &&buf, bool pack=false)
Definition: IOBufQueue.cpp:143
spdy::GoawayStatusCode statusCode
Definition: SPDYCodec.cpp:110
HTTPMessage getChunkedPostRequest()
Definition: TestUtils.cpp:113
std::tuple< std::unique_ptr< HTTPMessage >, std::unique_ptr< folly::IOBuf > > makeResponse(uint16_t statusCode, size_t len)
Definition: TestUtils.cpp:155
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
std::enable_if< std::is_arithmetic< T >::value >::type write(T value)
Definition: Cursor.h:737
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
HTTPMessage getBigGetRequest(const std::string &url)
Definition: TestUtils.cpp:85
CodecFactory codec
STL namespace.
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
std::unique_ptr< HTTPMessage > makePostRequest(uint32_t contentLength)
Definition: TestUtils.cpp:124
std::unique_ptr< folly::IOBuf > makeBuf(uint32_t size)
Definition: ZlibTests.cpp:26
void setIsChunked(bool chunked)
Definition: HTTPMessage.h:79
void set(folly::StringPiece name, const std::string &value)
Definition: HTTPHeaders.h:119
const uint32_t kMaxFramePayloadLengthMin
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
void writeBuf(const Buf &buf, folly::io::Appender &out)
ParseURL setURL(T &&url)
Definition: HTTPMessage.h:183
std::unique_ptr< HTTPMessage > getPriorityMessage(uint8_t priority)
Definition: TestUtils.cpp:29
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
std::unique_ptr< testing::NiceMock< MockHTTPCodec > > makeMockParallelCodec(TransportDirection dir)
Definition: TestUtils.cpp:51
HTTPMessage getResponse(uint32_t code, uint32_t bodyLen)
Definition: TestUtils.cpp:137
void fakeMockCodec(MockHTTPCodec &codec)
Definition: TestUtils.cpp:173
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
void setMethod(HTTPMethod method)
HTTPMessage getGetRequest(const std::string &url)
Definition: TestUtils.cpp:76
const char * string
Definition: Conv.cpp:212
HTTPMessage getPostRequest(uint32_t contentLength)
Definition: TestUtils.cpp:102
void setHTTPVersion(uint8_t major, uint8_t minor)
std::unique_ptr< HTTPMessage > makeGetRequest()
Definition: TestUtils.cpp:98
#define EXPECT_CALL(obj, call)
uint64_t StreamID
Definition: HTTPCodec.h:49
const internal::AnythingMatcher _
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
HTTPMessage getUpgradeRequest(const std::string &upgradeHeader, HTTPMethod method, uint32_t bodyLen)
Definition: TestUtils.cpp:161
static uint32_t rand32()
Definition: Random.h:213
static uint64_t rand64()
Definition: Random.h:263
const HTTPSettings kDefaultIngressSettings
internal::ReturnAction< R > Return(R value)
std::unique_ptr< testing::NiceMock< MockHTTPCodec > > makeUpstreamParallelCodec()
Definition: TestUtils.cpp:72
HTTPMessage getPubRequest(const std::string &url)
Definition: TestUtils.cpp:128
const std::string kVersionStrv2
void setStatusCode(uint16_t status)