proxygen
HTTPMessageTest.cpp File Reference
#include <fcntl.h>
#include <folly/portability/GTest.h>
#include <proxygen/lib/http/HTTPMessage.h>
#include <proxygen/lib/utils/TestUtils.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

Go to the source code of this file.

Functions

 TEST (HTTPMessage, TestParseCookiesSimple)
 
 TEST (HTTPMessage, TestParseCookiesSpaces)
 
 TEST (HTTPMessage, TestParseCookiesSingleCookie)
 
 TEST (HTTPMessage, TestParseCookiesMultipleCookies)
 
 TEST (HTTPMessage, TestParseQueryParamsSimple)
 
 TEST (HTTPMessage, TestParseQueryParamsComplex)
 
 TEST (HTTPMessage, TestHeaderPreservation)
 
 TEST (HTTPMessage, TestHeaderRemove)
 
 TEST (HTTPMessage, TestSetHeader)
 
 TEST (HTTPMessage, TestCombine)
 
 TEST (HTTPMessage, TestProxification)
 
 TEST (HTTPMessage, TestKeepaliveCheck)
 
 TEST (HTTPMessage, TestHeaderStripPerHop)
 
 TEST (HTTPMessage, TestEmptyName)
 
 TEST (HTTPMessage, TestMethod)
 
void testPathAndQuery (const string &url, const string &expectedPath, const string &expectedQuery)
 
 TEST (GetPathAndQuery, ParseURL)
 
 TEST (HTTPHeaders, AddStringPiece)
 
 TEST (HTTPHeaders, InitializerList)
 
 TEST (HTTPHeaders, InitializerListStringPiece)
 
void testRemoveQueryParam (const string &url, const string &queryParam, const string &expectedUrl, const string &expectedQuery)
 
 TEST (HTTPMessage, RemoveQueryParamTests)
 
void testSetQueryParam (const string &url, const string &queryParam, const string &paramValue, const string &expectedUrl, const string &expectedQuery)
 
 TEST (HTTPMessage, SetQueryParamTests)
 
 TEST (HTTPMessage, TestCheckForHeaderToken)
 
 TEST (HttpMessage, TestProtocolStringHTTPVersion)
 
 TEST (HttpMessage, TestProtocolStringAdvancedProtocol)
 

Function Documentation

TEST ( HTTPMessage  ,
TestParseCookiesSimple   
)

Definition at line 22 of file HTTPMessageTest.cpp.

References proxygen::HTTPHeaders::add(), EXPECT_EQ, proxygen::HTTPMessage::getCookie(), and proxygen::HTTPMessage::getHeaders().

22  {
23  HTTPMessage msg;
24 
25  msg.getHeaders().add("Cookie", "id=1256679245; data=0:1234567");
26  EXPECT_EQ(msg.getCookie("id"), "1256679245");
27  EXPECT_EQ(msg.getCookie("data"), "0:1234567");
28  EXPECT_EQ(msg.getCookie("mising"), "");
29 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const folly::StringPiece getCookie(const std::string &name) const
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
TEST ( HTTPMessage  ,
TestParseCookiesSpaces   
)

Definition at line 31 of file HTTPMessageTest.cpp.

References proxygen::HTTPHeaders::add(), EXPECT_EQ, proxygen::HTTPMessage::getCookie(), and proxygen::HTTPMessage::getHeaders().

31  {
32  HTTPMessage msg;
33 
34  msg.getHeaders().add("Cookie", " id=1256679245 ; data=0:1234567 ;");
35  EXPECT_EQ(msg.getCookie("id"), "1256679245");
36  EXPECT_EQ(msg.getCookie("data"), "0:1234567");
37 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const folly::StringPiece getCookie(const std::string &name) const
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
TEST ( HTTPMessage  ,
TestParseCookiesSingleCookie   
)

Definition at line 39 of file HTTPMessageTest.cpp.

References proxygen::HTTPHeaders::add(), EXPECT_EQ, proxygen::HTTPMessage::getCookie(), and proxygen::HTTPMessage::getHeaders().

39  {
40  HTTPMessage msg;
41 
42  msg.getHeaders().add("Cookie", " user_id=1256679245 ");
43  EXPECT_EQ(msg.getCookie("user_id"), "1256679245");
44 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const folly::StringPiece getCookie(const std::string &name) const
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
TEST ( HTTPMessage  ,
TestParseCookiesMultipleCookies   
)

Definition at line 46 of file HTTPMessageTest.cpp.

References proxygen::HTTPHeaders::add(), EXPECT_EQ, proxygen::HTTPMessage::getCookie(), and proxygen::HTTPMessage::getHeaders().

46  {
47  HTTPMessage msg;
48 
49  msg.getHeaders().add(
50  "Cookie", "id=1256679245; data=0:1234567; same=Always; Name");
51  msg.getHeaders().add(
52  "Cookie", "id2=1256679245; data2=0:1234567; same=Always; ");
53  EXPECT_EQ(msg.getCookie("id"), "1256679245");
54  EXPECT_EQ(msg.getCookie("id2"), "1256679245");
55  EXPECT_EQ(msg.getCookie("data"), "0:1234567");
56  EXPECT_EQ(msg.getCookie("data2"), "0:1234567");
57  EXPECT_EQ(msg.getCookie("same"), "Always");
58  EXPECT_EQ(msg.getCookie("Name"), "");
59 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const folly::StringPiece getCookie(const std::string &name) const
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
TEST ( HTTPMessage  ,
TestParseQueryParamsSimple   
)

Definition at line 61 of file HTTPMessageTest.cpp.

References EXPECT_ANY_THROW, EXPECT_EQ, proxygen::HTTPMessage::getDecodedQueryParam(), proxygen::HTTPMessage::getIntQueryParam(), proxygen::HTTPMessage::getQueryParam(), proxygen::HTTPMessage::hasQueryParam(), and proxygen::HTTPMessage::setURL().

61  {
62  HTTPMessage msg;
63  string url = "/test?seq=123456&userid=1256679245&dup=1&dup=2&helloWorld"
64  "&second=was+it+clear+%28already%29%3F";
65 
66  msg.setURL(url);
67  EXPECT_EQ(msg.getQueryParam("seq"), "123456");
68  EXPECT_EQ(msg.getQueryParam("userid"), "1256679245");
69  EXPECT_EQ(msg.getQueryParam("dup"), "2");
70  EXPECT_EQ(msg.getQueryParam("helloWorld"), "");
71  EXPECT_EQ(msg.getIntQueryParam("dup", 5), 2);
72  EXPECT_EQ(msg.getIntQueryParam("abc", 5), 5);
73  EXPECT_EQ(msg.getDecodedQueryParam("second"), "was it clear (already)?");
74  EXPECT_EQ(msg.getDecodedQueryParam("seq"), "123456");
75  EXPECT_EQ(msg.hasQueryParam("seq"), true);
76  EXPECT_EQ(msg.hasQueryParam("seq1"), false);
77  EXPECT_EQ(msg.getIntQueryParam("dup"), 2);
79  EXPECT_ANY_THROW(msg.getIntQueryParam("second"));
80 }
#define EXPECT_ANY_THROW(statement)
Definition: gtest.h:1847
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const std::string & getQueryParam(const std::string &name) const
ParseURL setURL(T &&url)
Definition: HTTPMessage.h:183
int getIntQueryParam(const std::string &name) const
std::string getDecodedQueryParam(const std::string &name) const
bool hasQueryParam(const std::string &name) const
TEST ( HTTPMessage  ,
TestParseQueryParamsComplex   
)

Definition at line 82 of file HTTPMessageTest.cpp.

References folly::empty(), EXPECT_EQ, folly::gen::first, proxygen::HTTPMessage::getQueryParam(), i, proxygen::HTTPMessage::setURL(), string, folly::gen::sub(), and val.

82  {
83  HTTPMessage msg;
84  std::vector<std::vector<std::string>> input = {
85  {"", "", ""},
86  {"key_and_equal_but_no_value", "=", ""},
87  {"only_key", "", ""},
88  {"key_and_value", "=", "value"},
89  {"key_and_value_2", "=", "value2"},
90  {"key_and_value_3", "=", "value3"}
91  };
92 
93  for (int i = 0; i < (1 << input.size()); ++i) {
94  std::vector<std::vector<std::string>> sub;
95  for (size_t j = 0; j < input.size(); ++j) {
96  if ((i & (1 << j))) {
97  sub.push_back(input[j]);
98  }
99  }
100 
101  sort(sub.begin(), sub.end());
102  do {
103  bool first = true;
104  std::string url = "/test?";
105  for (const auto& val: sub) {
106  if (first) {
107  first = false;
108  } else {
109  url += "&";
110  }
111 
112  url += val[0] + val[1] + val[2];
113  }
114 
115  msg.setURL(url);
116  for (const auto& val: sub) {
117  if (val[0].empty()) {
118  continue;
119  }
120 
121  EXPECT_EQ(val[2], msg.getQueryParam(val[0]));
122  }
123 
124  } while (next_permutation(sub.begin(), sub.end()));
125  }
126 }
Sub sub(Sink sink)
Definition: Parallel.h:104
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
double val
Definition: String.cpp:273
const std::string & getQueryParam(const std::string &name) const
constexpr auto empty(C const &c) -> decltype(c.empty())
Definition: Access.h:55
ParseURL setURL(T &&url)
Definition: HTTPMessage.h:183
const char * string
Definition: Conv.cpp:212
constexpr detail::First first
Definition: Base-inl.h:2553
TEST ( HTTPMessage  ,
TestHeaderPreservation   
)

Definition at line 128 of file HTTPMessageTest.cpp.

References proxygen::HTTPHeaders::add(), EXPECT_EQ, proxygen::HTTPMessage::getHeaders(), proxygen::HTTPHeaders::getNumberOfValues(), and proxygen::HTTPHeaders::size().

128  {
129  HTTPMessage msg;
130  HTTPHeaders& hdrs = msg.getHeaders();
131 
132  hdrs.add("Jojo", "1");
133  hdrs.add("Binky", "2");
134  hdrs.add("jOJo", "3");
135  hdrs.add("bINKy", "4");
136 
137  EXPECT_EQ(hdrs.size(), 4);
138  EXPECT_EQ(hdrs.getNumberOfValues("jojo"), 2);
139  EXPECT_EQ(hdrs.getNumberOfValues("binky"), 2);
140 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
size_t getNumberOfValues(HTTPHeaderCode code) const
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
size_t size() const
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
TEST ( HTTPMessage  ,
TestHeaderRemove   
)

Definition at line 142 of file HTTPMessageTest.cpp.

References proxygen::HTTPHeaders::add(), EXPECT_EQ, proxygen::HTTPMessage::getHeaders(), proxygen::HTTPHeaders::getNumberOfValues(), proxygen::HTTPHeaders::remove(), and proxygen::HTTPHeaders::size().

142  {
143  HTTPMessage msg;
144  HTTPHeaders& hdrs = msg.getHeaders();
145 
146  hdrs.add("Jojo", "1");
147  hdrs.add("Binky", "2");
148  hdrs.add("jOJo", "3");
149  hdrs.add("bINKy", "4");
150  hdrs.remove("jojo");
151 
152  EXPECT_EQ(hdrs.size(), 2);
153  EXPECT_EQ(hdrs.getNumberOfValues("binky"), 2);
154 }
bool remove(folly::StringPiece name)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
size_t getNumberOfValues(HTTPHeaderCode code) const
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
size_t size() const
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
TEST ( HTTPMessage  ,
TestSetHeader   
)

Definition at line 156 of file HTTPMessageTest.cpp.

References proxygen::HTTPHeaders::add(), EXPECT_EQ, proxygen::HTTPMessage::getHeaders(), proxygen::HTTPHeaders::getNumberOfValues(), proxygen::HTTPHeaders::set(), and proxygen::HTTPHeaders::size().

156  {
157  HTTPMessage msg;
158  HTTPHeaders& hdrs = msg.getHeaders();
159 
160  hdrs.set("Jojo", "1");
161  EXPECT_EQ(hdrs.size(), 1);
162  EXPECT_EQ(hdrs.getNumberOfValues("Jojo"), 1);
163 
164  hdrs.add("jojo", "2");
165  hdrs.add("jojo", "3");
166  hdrs.add("bar", "4");
167  EXPECT_EQ(hdrs.size(), 4);
168  EXPECT_EQ(hdrs.getNumberOfValues("Jojo"), 3);
169 
170  hdrs.set("joJO", "5");
171  EXPECT_EQ(hdrs.size(), 2);
172  EXPECT_EQ(hdrs.getNumberOfValues("Jojo"), 1);
173 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
size_t getNumberOfValues(HTTPHeaderCode code) const
void set(folly::StringPiece name, const std::string &value)
Definition: HTTPHeaders.h:119
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
size_t size() const
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
TEST ( HTTPMessage  ,
TestCombine   
)

Definition at line 175 of file HTTPMessageTest.cpp.

References proxygen::HTTPHeaders::add(), proxygen::HTTPHeaders::combine(), EXPECT_EQ, and proxygen::HTTPMessage::getHeaders().

175  {
176  HTTPMessage msg;
177  HTTPHeaders& headers = msg.getHeaders();
178 
179  EXPECT_EQ(headers.combine("Combine"), "");
180 
181  headers.add("Combine", "first value");
182  EXPECT_EQ(headers.combine("Combine"), "first value");
183 
184  headers.add("Combine", "second value");
185  EXPECT_EQ(headers.combine("Combine"), "first value, second value");
186 
187  headers.add("Combine", "third value");
188  EXPECT_EQ(headers.combine("Combine"),
189  "first value, second value, third value");
190  VLOG(4) << msg;
191 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string combine(const T &header, const std::string &separator=COMBINE_SEPARATOR) const
Definition: HTTPHeaders.h:382
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
TEST ( HTTPMessage  ,
TestProxification   
)

Definition at line 193 of file HTTPMessageTest.cpp.

References proxygen::HTTPMessage::ensureHostHeader(), EXPECT_EQ, EXPECT_FALSE, proxygen::HTTPMessage::getHeaders(), proxygen::HTTPHeaders::getSingleOrEmpty(), proxygen::HTTP_HEADER_HOST, proxygen::HTTPMessage::setDstAddress(), proxygen::HTTPMessage::setLocalIp(), proxygen::HTTPMessage::setWantsKeepalive(), and proxygen::HTTPMessage::wantsKeepalive().

193  {
194  HTTPMessage msg;
195 
196  folly::SocketAddress dstAddr("192.168.1.1", 1887);
197  folly::SocketAddress clientAddr("74.125.127.9", 1987);
198  msg.setDstAddress(dstAddr);
199  msg.setLocalIp("10.0.0.1");
200  msg.ensureHostHeader();
201  msg.setWantsKeepalive(false);
202 
203  HTTPHeaders& hdrs = msg.getHeaders();
204  EXPECT_EQ("192.168.1.1", hdrs.getSingleOrEmpty(HTTP_HEADER_HOST));
206 }
void setWantsKeepalive(bool wantsKeepaliveVal)
Definition: HTTPMessage.h:343
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void setDstAddress(const folly::SocketAddress &addr, std::string addressStr=empty_string, std::string portStr=empty_string)
Definition: HTTPMessage.h:119
void setLocalIp(T &&ip)
Definition: HTTPMessage.h:148
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
const std::string & getSingleOrEmpty(const T &nameOrCode) const
Definition: HTTPHeaders.h:420
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
bool wantsKeepalive() const
Definition: HTTPMessage.h:346
TEST ( HTTPMessage  ,
TestKeepaliveCheck   
)

Definition at line 208 of file HTTPMessageTest.cpp.

References proxygen::HTTPHeaders::add(), proxygen::HTTPMessage::computeKeepalive(), EXPECT_FALSE, EXPECT_TRUE, proxygen::HTTPMessage::getHeaders(), proxygen::HTTP_HEADER_CONNECTION, proxygen::HTTPMessage::setHTTPVersion(), and proxygen::HTTPMessage::stripPerHopHeaders().

208  {
209  {
210  HTTPMessage msg;
211  msg.setHTTPVersion(1, 0);
213  }
214 
215  {
216  HTTPMessage msg;
217  msg.setHTTPVersion(1, 1);
219  }
220 
221  {
222  HTTPMessage msg;
223  msg.setHTTPVersion(1, 1);
224  msg.getHeaders().add(HTTP_HEADER_CONNECTION, "close");
226  }
227 
228  {
229  HTTPMessage msg;
230  msg.setHTTPVersion(1, 1);
231  msg.getHeaders().add(HTTP_HEADER_CONNECTION, "ClOsE");
233  }
234 
235  {
236  HTTPMessage msg;
237  msg.setHTTPVersion(1, 1);
238  msg.getHeaders().add(HTTP_HEADER_CONNECTION, "foo,bar");
240  }
241 
242  {
243  HTTPMessage msg;
244  msg.setHTTPVersion(1, 1);
245  msg.getHeaders().add(HTTP_HEADER_CONNECTION, "foo,bar");
246  msg.getHeaders().add(HTTP_HEADER_CONNECTION, "abc,CLOSE,def");
247  msg.getHeaders().add(HTTP_HEADER_CONNECTION, "xyz");
249  }
250 
251  {
252  HTTPMessage msg;
253  msg.setHTTPVersion(1, 1);
254  msg.getHeaders().add(HTTP_HEADER_CONNECTION, "foo,bar");
255  msg.getHeaders().add(HTTP_HEADER_CONNECTION, "abc , CLOSE , def");
256  msg.getHeaders().add(HTTP_HEADER_CONNECTION, "xyz");
258  }
259 
260  {
261  HTTPMessage msg;
262  msg.setHTTPVersion(1, 1);
263  msg.getHeaders().add(HTTP_HEADER_CONNECTION, " close ");
265  }
266 
267  {
268  HTTPMessage msg;
269  msg.setHTTPVersion(1, 1);
270  msg.getHeaders().add(HTTP_HEADER_CONNECTION, ", close ");
272  }
273 
274  {
275  HTTPMessage msg;
276  msg.setHTTPVersion(1, 1);
277  msg.getHeaders().add(HTTP_HEADER_CONNECTION, " close , ");
279  }
280 
281  {
282  HTTPMessage msg;
283  msg.setHTTPVersion(1, 0);
284  msg.getHeaders().add(HTTP_HEADER_CONNECTION, "Keep-Alive");
286  }
287 
288  {
289  HTTPMessage msg;
290  msg.setHTTPVersion(1, 0);
291  msg.getHeaders().add(HTTP_HEADER_CONNECTION, "keep-alive");
293  }
294 
295  {
296  HTTPMessage msg;
297  msg.setHTTPVersion(1, 0);
298  msg.getHeaders().add(HTTP_HEADER_CONNECTION, "keep-alive");
299  msg.getHeaders().add(HTTP_HEADER_CONNECTION, "close");
301  }
302 
303  {
304  HTTPMessage msg;
305  msg.setHTTPVersion(1, 0);
306  msg.getHeaders().add(HTTP_HEADER_CONNECTION, "keep-alive");
307  msg.stripPerHopHeaders();
309  }
310 }
bool computeKeepalive() const
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
void setHTTPVersion(uint8_t major, uint8_t minor)
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( HTTPMessage  ,
TestHeaderStripPerHop   
)

Definition at line 312 of file HTTPMessageTest.cpp.

References proxygen::HTTPHeaders::add(), EXPECT_EQ, proxygen::HTTPMessage::getHeaders(), proxygen::HTTPMessage::getStrippedPerHopHeaders(), proxygen::HTTP_HEADER_CONNECTION, proxygen::HTTPHeaders::size(), and proxygen::HTTPMessage::stripPerHopHeaders().

312  {
313  HTTPMessage msg;
314 
315  msg.getHeaders().add(HTTP_HEADER_CONNECTION, "a, b, c");
317  msg.getHeaders().add(HTTP_HEADER_CONNECTION, ",,,,");
318  msg.getHeaders().add(HTTP_HEADER_CONNECTION, " , , , ,");
319  msg.getHeaders().add(HTTP_HEADER_CONNECTION, ", e");
320  msg.getHeaders().add(HTTP_HEADER_CONNECTION, " f ,\tg\t, \r\n\th ");
321  msg.getHeaders().add("Keep-Alive", "true");
322 
323  msg.getHeaders().add("a", "1");
324  msg.getHeaders().add("b", "2");
325  msg.getHeaders().add("c", "3");
326  msg.getHeaders().add("d", "4");
327  msg.getHeaders().add("e", "5");
328  msg.getHeaders().add("f", "6");
329  msg.getHeaders().add("g", "7");
330  msg.getHeaders().add("h", "8");
331 
332  EXPECT_EQ(msg.getHeaders().size(), 15);
333  msg.stripPerHopHeaders();
334  EXPECT_EQ(msg.getHeaders().size(), 0);
336 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const HTTPHeaders & getStrippedPerHopHeaders() const
Definition: HTTPMessage.h:530
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
size_t size() const
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
TEST ( HTTPMessage  ,
TestEmptyName   
)

Definition at line 338 of file HTTPMessageTest.cpp.

References EXPECT_DEATH_NO_CORE, proxygen::HTTPMessage::getHeaders(), and proxygen::HTTPHeaders::set().

338  {
339  HTTPMessage msg;
340  EXPECT_DEATH_NO_CORE(msg.getHeaders().set("", "empty name?!"), ".*");
341 }
#define EXPECT_DEATH_NO_CORE(token, regex)
Definition: TestUtils.h:19
void set(folly::StringPiece name, const std::string &value)
Definition: HTTPHeaders.h:119
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
TEST ( HTTPMessage  ,
TestMethod   
)

Definition at line 343 of file HTTPMessageTest.cpp.

References proxygen::CONNECT, EXPECT_EQ, proxygen::GET, proxygen::HTTPMessage::getMethod(), proxygen::HTTPMessage::getMethodString(), folly::none, and proxygen::HTTPMessage::setMethod().

343  {
344  HTTPMessage msg;
345 
346  msg.setMethod(HTTPMethod::GET);
347  EXPECT_EQ("GET", msg.getMethodString());
348  EXPECT_EQ(HTTPMethod::GET == msg.getMethod(), true);
349 
350  msg.setMethod("FOO");
351  EXPECT_EQ("FOO", msg.getMethodString());
353 
354  msg.setMethod(HTTPMethod::CONNECT);
355  EXPECT_EQ("CONNECT", msg.getMethodString());
356  EXPECT_EQ(HTTPMethod::CONNECT == msg.getMethod(), true);
357 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void setMethod(HTTPMethod method)
const std::string & getMethodString() const
folly::Optional< HTTPMethod > getMethod() const
constexpr None none
Definition: Optional.h:87
TEST ( GetPathAndQuery  ,
ParseURL   
)

Definition at line 370 of file HTTPMessageTest.cpp.

References testPathAndQuery().

370  {
371  testPathAndQuery("http://localhost:80/foo?bar#qqq", "/foo", "bar");
372  testPathAndQuery("localhost:80/foo?bar#qqq", "/foo", "bar");
373  testPathAndQuery("localhost", "", "");
374  testPathAndQuery("/f/o/o?bar#qqq", "/f/o/o", "bar");
375  testPathAndQuery("#?hello", "", "");
376 }
void testPathAndQuery(const string &url, const string &expectedPath, const string &expectedQuery)
TEST ( HTTPHeaders  ,
AddStringPiece   
)

Definition at line 378 of file HTTPMessageTest.cpp.

References proxygen::HTTPHeaders::add(), EXPECT_EQ, proxygen::HTTPHeaders::getSingleOrEmpty(), name, and folly::Range< Iter >::split_step().

378  {
379  const char foo[] = "name:value";
380  HTTPHeaders headers;
381 
382  folly::StringPiece str(foo);
383  folly::StringPiece name = str.split_step(':');
384  headers.add(name, str);
385  EXPECT_EQ("value", headers.getSingleOrEmpty("name"));
386 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const char * name
Definition: http_parser.c:437
const std::string & getSingleOrEmpty(const T &nameOrCode) const
Definition: HTTPHeaders.h:420
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
TEST ( HTTPHeaders  ,
InitializerList   
)

Definition at line 388 of file HTTPMessageTest.cpp.

References proxygen::HTTPHeaders::add(), proxygen::HTTPHeaders::combine(), EXPECT_EQ, proxygen::HTTPHeaders::getSingleOrEmpty(), proxygen::HTTP_HEADER_CONNECTION, and proxygen::HTTP_HEADER_SERVER.

388  {
389  HTTPHeaders hdrs;
390 
391  hdrs.add({{"name", "value"}});
392  hdrs.add({{HTTP_HEADER_CONNECTION, "close"}});
393  hdrs.add({{"a", "b"},
394  {HTTP_HEADER_CONNECTION, "foo"},
395  {HTTP_HEADER_SERVER, "x"}});
396 
397  EXPECT_EQ("value", hdrs.getSingleOrEmpty("name"));
398  EXPECT_EQ("close, foo", hdrs.combine(HTTP_HEADER_CONNECTION));
400 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string combine(const T &header, const std::string &separator=COMBINE_SEPARATOR) const
Definition: HTTPHeaders.h:382
const std::string & getSingleOrEmpty(const T &nameOrCode) const
Definition: HTTPHeaders.h:420
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
TEST ( HTTPHeaders  ,
InitializerListStringPiece   
)

Definition at line 402 of file HTTPMessageTest.cpp.

References proxygen::HTTPHeaders::add(), EXPECT_EQ, proxygen::HTTPHeaders::getSingleOrEmpty(), proxygen::HTTP_HEADER_CONNECTION, name, and folly::Range< Iter >::split_step().

402  {
403  HTTPHeaders hdrs;
404 
405  const char* foo = "name:value";
406  folly::StringPiece str(foo);
407  folly::StringPiece name = str.split_step(':');
408  hdrs.add({{name, str}, {HTTP_HEADER_CONNECTION, str}});
409 
410  EXPECT_EQ("value", hdrs.getSingleOrEmpty("name"));
412 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const char * name
Definition: http_parser.c:437
const std::string & getSingleOrEmpty(const T &nameOrCode) const
Definition: HTTPHeaders.h:420
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
TEST ( HTTPMessage  ,
RemoveQueryParamTests   
)

Definition at line 427 of file HTTPMessageTest.cpp.

References testRemoveQueryParam().

427  {
428  // Query param present
429  testRemoveQueryParam("http://localhost:80/foo?param1=a&param2=b#qqq",
430  "param2",
431  "http://localhost:80/foo?param1=a#qqq",
432  "param1=a");
433  // Query param not present
434  testRemoveQueryParam("http://localhost/foo?param1=a&param2=b#qqq",
435  "param3",
436  "http://localhost/foo?param1=a&param2=b#qqq",
437  "param1=a&param2=b");
438  // No scheme
439  testRemoveQueryParam("localhost:80/foo?param1=a&param2=b#qqq",
440  "param2",
441  "localhost:80/foo?param1=a#qqq",
442  "param1=a");
443  // Just hostname as URL and empty query param
444  testRemoveQueryParam("localhost", "param2", "localhost", "");
445  testRemoveQueryParam("localhost", "", "localhost", "");
446  // Just path as URL
447  testRemoveQueryParam("/f/o/o?bar#qqq", "bar", "/f/o/o#qqq", "");
448 }
void testRemoveQueryParam(const string &url, const string &queryParam, const string &expectedUrl, const string &expectedQuery)
TEST ( HTTPMessage  ,
SetQueryParamTests   
)

Definition at line 464 of file HTTPMessageTest.cpp.

References testSetQueryParam().

464  {
465  // Overwrite existing parameter
466  testSetQueryParam("http://localhost:80/foo?param1=a&param2=b#qqq",
467  "param2",
468  "true",
469  "http://localhost:80/foo?param1=a&param2=true#qqq",
470  "param1=a&param2=true");
471  // Add a query parameter
472  testSetQueryParam("http://localhost/foo?param1=a&param2=b#qqq",
473  "param3",
474  "true",
475  "http://localhost/foo?param1=a&param2=b&param3=true#qqq",
476  "param1=a&param2=b&param3=true");
477  // Add a query parameter, should be alphabetical order
478  testSetQueryParam("localhost:80/foo?param1=a&param3=c#qqq",
479  "param2",
480  "b",
481  "localhost:80/foo?param1=a&param2=b&param3=c#qqq",
482  "param1=a&param2=b&param3=c");
483  // Add a query parameter when no query parameters exist
484  testSetQueryParam("localhost:80/foo#qqq",
485  "param2",
486  "b",
487  "localhost:80/foo?param2=b#qqq",
488  "param2=b");
489 }
void testSetQueryParam(const string &url, const string &queryParam, const string &paramValue, const string &expectedUrl, const string &expectedQuery)
TEST ( HTTPMessage  ,
TestCheckForHeaderToken   
)

Definition at line 491 of file HTTPMessageTest.cpp.

References proxygen::HTTPHeaders::add(), proxygen::HTTPMessage::checkForHeaderToken(), EXPECT_FALSE, EXPECT_TRUE, proxygen::HTTPMessage::getHeaders(), and proxygen::HTTP_HEADER_CONNECTION.

491  {
492  HTTPMessage msg;
493  HTTPHeaders& headers = msg.getHeaders();
494 
495  headers.add(HTTP_HEADER_CONNECTION, "HTTP2-Settings");
497  false));
499  true));
500 }
bool checkForHeaderToken(const HTTPHeaderCode headerCode, char const *token, bool caseSensitive) const
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( HttpMessage  ,
TestProtocolStringHTTPVersion   
)

Definition at line 502 of file HTTPMessageTest.cpp.

References EXPECT_EQ, proxygen::HTTPMessage::getProtocolString(), and proxygen::HTTPMessage::setHTTPVersion().

502  {
503  HTTPMessage msg;
504  msg.setHTTPVersion(1, 1);
505 
506  EXPECT_EQ(msg.getProtocolString(), "1.1");
507 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void setHTTPVersion(uint8_t major, uint8_t minor)
const std::string & getProtocolString() const
Definition: HTTPMessage.h:558
TEST ( HttpMessage  ,
TestProtocolStringAdvancedProtocol   
)

Definition at line 509 of file HTTPMessageTest.cpp.

References EXPECT_EQ, proxygen::HTTPMessage::getProtocolString(), proxygen::HTTPMessage::setAdvancedProtocolString(), and string.

509  {
510  HTTPMessage msg;
511  std::string advancedProtocol = "h2";
512  msg.setAdvancedProtocolString(advancedProtocol);
513  EXPECT_EQ(msg.getProtocolString(), advancedProtocol);
514 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void setAdvancedProtocolString(const std::string &protocol)
Definition: HTTPMessage.h:542
const char * string
Definition: Conv.cpp:212
const std::string & getProtocolString() const
Definition: HTTPMessage.h:558
void testPathAndQuery ( const string url,
const string expectedPath,
const string expectedQuery 
)

Definition at line 359 of file HTTPMessageTest.cpp.

References EXPECT_EQ, proxygen::HTTPMessage::getPath(), proxygen::HTTPMessage::getQueryString(), proxygen::HTTPMessage::getURL(), and proxygen::HTTPMessage::setURL().

Referenced by TEST().

361  {
362  HTTPMessage msg;
363  msg.setURL(url);
364 
365  EXPECT_EQ(msg.getURL(), url);
366  EXPECT_EQ(msg.getPath(), expectedPath);
367  EXPECT_EQ(msg.getQueryString(), expectedQuery);
368 }
const std::string & getPath() const
Definition: HTTPMessage.h:215
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const std::string & getQueryString() const
Definition: HTTPMessage.h:222
ParseURL setURL(T &&url)
Definition: HTTPMessage.h:183
const std::string & getURL() const
Definition: HTTPMessage.h:205
void testRemoveQueryParam ( const string url,
const string queryParam,
const string expectedUrl,
const string expectedQuery 
)

Definition at line 414 of file HTTPMessageTest.cpp.

References EXPECT_EQ, proxygen::HTTPMessage::getQueryString(), proxygen::HTTPMessage::getURL(), proxygen::HTTPMessage::removeQueryParam(), and proxygen::HTTPMessage::setURL().

Referenced by TEST().

417  {
418  HTTPMessage msg;
419  msg.setURL(url);
420  bool expectedChange = (url != expectedUrl);
421  EXPECT_EQ(msg.removeQueryParam(queryParam), expectedChange);
422 
423  EXPECT_EQ(msg.getURL(), expectedUrl);
424  EXPECT_EQ(msg.getQueryString(), expectedQuery);
425 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const std::string & getQueryString() const
Definition: HTTPMessage.h:222
ParseURL setURL(T &&url)
Definition: HTTPMessage.h:183
bool removeQueryParam(const std::string &name)
const std::string & getURL() const
Definition: HTTPMessage.h:205
void testSetQueryParam ( const string url,
const string queryParam,
const string paramValue,
const string expectedUrl,
const string expectedQuery 
)

Definition at line 450 of file HTTPMessageTest.cpp.

References EXPECT_EQ, proxygen::HTTPMessage::getQueryString(), proxygen::HTTPMessage::getURL(), proxygen::HTTPMessage::setQueryParam(), and proxygen::HTTPMessage::setURL().

Referenced by TEST().

454  {
455  HTTPMessage msg;
456  msg.setURL(url);
457  bool expectedChange = (url != expectedUrl);
458  EXPECT_EQ(msg.setQueryParam(queryParam, paramValue), expectedChange);
459 
460  EXPECT_EQ(msg.getURL(), expectedUrl);
461  EXPECT_EQ(msg.getQueryString(), expectedQuery);
462 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const std::string & getQueryString() const
Definition: HTTPMessage.h:222
ParseURL setURL(T &&url)
Definition: HTTPMessage.h:183
bool setQueryParam(const std::string &name, const std::string &value)
const std::string & getURL() const
Definition: HTTPMessage.h:205