brpc_http_message_unittest.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. //
  18. // Date 2014/10/24 16:44:30
  19. #include <gtest/gtest.h>
  20. #include <google/protobuf/descriptor.h>
  21. #include "brpc/server.h"
  22. #include "brpc/details/http_message.h"
  23. #include "brpc/policy/http_rpc_protocol.h"
  24. #include "echo.pb.h"
  25. namespace brpc {
  26. namespace policy {
  27. Server::MethodProperty*
  28. FindMethodPropertyByURI(const std::string& uri_path, const Server* server,
  29. std::string* unknown_method_str);
  30. bool ParseHttpServerAddress(butil::EndPoint *point, const char *server_addr_and_port);
  31. }}
  32. namespace {
  33. using brpc::policy::FindMethodPropertyByURI;
  34. using brpc::policy::ParseHttpServerAddress;
  35. TEST(HttpMessageTest, http_method) {
  36. ASSERT_STREQ("DELETE", brpc::HttpMethod2Str(brpc::HTTP_METHOD_DELETE));
  37. ASSERT_STREQ("GET", brpc::HttpMethod2Str(brpc::HTTP_METHOD_GET));
  38. ASSERT_STREQ("POST", brpc::HttpMethod2Str(brpc::HTTP_METHOD_POST));
  39. ASSERT_STREQ("PUT", brpc::HttpMethod2Str(brpc::HTTP_METHOD_PUT));
  40. brpc::HttpMethod m;
  41. ASSERT_TRUE(brpc::Str2HttpMethod("DELETE", &m));
  42. ASSERT_EQ(brpc::HTTP_METHOD_DELETE, m);
  43. ASSERT_TRUE(brpc::Str2HttpMethod("GET", &m));
  44. ASSERT_EQ(brpc::HTTP_METHOD_GET, m);
  45. ASSERT_TRUE(brpc::Str2HttpMethod("POST", &m));
  46. ASSERT_EQ(brpc::HTTP_METHOD_POST, m);
  47. ASSERT_TRUE(brpc::Str2HttpMethod("PUT", &m));
  48. ASSERT_EQ(brpc::HTTP_METHOD_PUT, m);
  49. // case-insensitive
  50. ASSERT_TRUE(brpc::Str2HttpMethod("DeLeTe", &m));
  51. ASSERT_EQ(brpc::HTTP_METHOD_DELETE, m);
  52. ASSERT_TRUE(brpc::Str2HttpMethod("get", &m));
  53. ASSERT_EQ(brpc::HTTP_METHOD_GET, m);
  54. // non-existed
  55. ASSERT_FALSE(brpc::Str2HttpMethod("DEL", &m));
  56. ASSERT_FALSE(brpc::Str2HttpMethod("DELETE ", &m));
  57. ASSERT_FALSE(brpc::Str2HttpMethod("GOT", &m));
  58. }
  59. TEST(HttpMessageTest, eof) {
  60. GFLAGS_NS::SetCommandLineOption("verbose", "100");
  61. const char* http_request =
  62. "GET /CloudApiControl/HttpServer/telematics/v3/weather?location=%E6%B5%B7%E5%8D%97%E7%9C%81%E7%9B%B4%E8%BE%96%E5%8E%BF%E7%BA%A7%E8%A1%8C%E6%94%BF%E5%8D%95%E4%BD%8D&output=json&ak=0l3FSP6qA0WbOzGRaafbmczS HTTP/1.1\r\n"
  63. "X-Host: api.map.baidu.com\r\n"
  64. "X-Forwarded-Proto: http\r\n"
  65. "Host: api.map.baidu.com\r\n"
  66. "User-Agent: IME/Android/4.4.2/N80.QHD.LT.X10.V3/N80.QHD.LT.X10.V3.20150812.031915\r\n"
  67. "Accept: application/json\r\n"
  68. "Accept-Charset: UTF-8,*;q=0.5\r\n"
  69. "Accept-Encoding: deflate,sdch\r\n"
  70. "Accept-Language: zh-CN,en-US;q=0.8,zh;q=0.6\r\n"
  71. "Bfe-Atk: NORMAL_BROWSER\r\n"
  72. "Bfe_logid: 8767802212038413243\r\n"
  73. "Bfeip: 10.26.124.40\r\n"
  74. "CLIENTIP: 119.29.102.26\r\n"
  75. "CLIENTPORT: 59863\r\n"
  76. "Cache-Control: max-age=0\r\n"
  77. "Content-Type: application/json;charset=utf8\r\n"
  78. "X-Forwarded-For: 119.29.102.26\r\n"
  79. "X-Forwarded-Port: 59863\r\n"
  80. "X-Ime-Imei: 35629601890905\r\n"
  81. "X_BD_LOGID: 3959476981\r\n"
  82. "X_BD_LOGID64: 16815814797661447369\r\n"
  83. "X_BD_PRODUCT: map\r\n"
  84. "X_BD_SUBSYS: apimap\r\n";
  85. butil::IOBuf buf;
  86. buf.append(http_request);
  87. brpc::HttpMessage http_message;
  88. ASSERT_EQ((ssize_t)buf.size(), http_message.ParseFromIOBuf(buf));
  89. ASSERT_EQ(2, http_message.ParseFromArray("\r\n", 2));
  90. ASSERT_TRUE(http_message.Completed());
  91. }
  92. TEST(HttpMessageTest, request_sanity) {
  93. const char *http_request =
  94. "POST /path/file.html?sdfsdf=sdfs&sldf1=sdf HTTP/12.34\r\n"
  95. "From: someuser@jmarshall.com\r\n"
  96. "User-Agent: HTTPTool/1.0 \r\n" // intended ending spaces
  97. "Content-Type: json\r\n"
  98. "Content-Length: 19\r\n"
  99. "Log-ID: 456\r\n"
  100. "Host: myhost\r\n"
  101. "Correlation-ID: 123\r\n"
  102. "Authorization: test\r\n"
  103. "Accept: */*\r\n"
  104. "\r\n"
  105. "Message Body sdfsdf\r\n"
  106. ;
  107. brpc::HttpMessage http_message;
  108. ASSERT_EQ((ssize_t)strlen(http_request),
  109. http_message.ParseFromArray(http_request, strlen(http_request)));
  110. const brpc::HttpHeader& header = http_message.header();
  111. // Check all keys
  112. ASSERT_EQ("json", header.content_type());
  113. ASSERT_TRUE(header.GetHeader("HOST"));
  114. ASSERT_EQ("myhost", *header.GetHeader("host"));
  115. ASSERT_TRUE(header.GetHeader("CORRELATION-ID"));
  116. ASSERT_EQ("123", *header.GetHeader("CORRELATION-ID"));
  117. ASSERT_TRUE(header.GetHeader("User-Agent"));
  118. ASSERT_EQ("HTTPTool/1.0 ", *header.GetHeader("User-Agent"));
  119. ASSERT_TRUE(header.GetHeader("Host"));
  120. ASSERT_EQ("myhost", *header.GetHeader("Host"));
  121. ASSERT_TRUE(header.GetHeader("Accept"));
  122. ASSERT_EQ("*/*", *header.GetHeader("Accept"));
  123. ASSERT_EQ(1, header.major_version());
  124. ASSERT_EQ(34, header.minor_version());
  125. ASSERT_EQ(brpc::HTTP_METHOD_POST, header.method());
  126. ASSERT_EQ(brpc::HTTP_STATUS_OK, header.status_code());
  127. ASSERT_STREQ("OK", header.reason_phrase());
  128. ASSERT_TRUE(header.GetHeader("log-id"));
  129. ASSERT_EQ("456", *header.GetHeader("log-id"));
  130. ASSERT_TRUE(NULL != header.GetHeader("Authorization"));
  131. ASSERT_EQ("test", *header.GetHeader("Authorization"));
  132. }
  133. TEST(HttpMessageTest, response_sanity) {
  134. const char *http_response =
  135. "HTTP/12.34 410 GoneBlah\r\n"
  136. "From: someuser@jmarshall.com\r\n"
  137. "User-Agent: HTTPTool/1.0 \r\n" // intended ending spaces
  138. "Content-Type: json2\r\n"
  139. "Content-Length: 19\r\n"
  140. "Log-ID: 456\r\n"
  141. "Host: myhost\r\n"
  142. "Correlation-ID: 123\r\n"
  143. "Authorization: test\r\n"
  144. "Accept: */*\r\n"
  145. "\r\n"
  146. "Message Body sdfsdf\r\n"
  147. ;
  148. brpc::HttpMessage http_message;
  149. ASSERT_EQ((ssize_t)strlen(http_response),
  150. http_message.ParseFromArray(http_response, strlen(http_response)));
  151. // Check all keys
  152. const brpc::HttpHeader& header = http_message.header();
  153. ASSERT_EQ("json2", header.content_type());
  154. ASSERT_TRUE(header.GetHeader("HOST"));
  155. ASSERT_EQ("myhost", *header.GetHeader("host"));
  156. ASSERT_TRUE(header.GetHeader("CORRELATION-ID"));
  157. ASSERT_EQ("123", *header.GetHeader("CORRELATION-ID"));
  158. ASSERT_TRUE(header.GetHeader("User-Agent"));
  159. ASSERT_EQ("HTTPTool/1.0 ", *header.GetHeader("User-Agent"));
  160. ASSERT_TRUE(header.GetHeader("Host"));
  161. ASSERT_EQ("myhost", *header.GetHeader("Host"));
  162. ASSERT_TRUE(header.GetHeader("Accept"));
  163. ASSERT_EQ("*/*", *header.GetHeader("Accept"));
  164. ASSERT_EQ(1, header.major_version());
  165. ASSERT_EQ(34, header.minor_version());
  166. // method is undefined for response, in our case, it's set to 0.
  167. ASSERT_EQ(brpc::HTTP_METHOD_DELETE, header.method());
  168. ASSERT_EQ(brpc::HTTP_STATUS_GONE, header.status_code());
  169. ASSERT_STREQ(brpc::HttpReasonPhrase(header.status_code()), /*not GoneBlah*/
  170. header.reason_phrase());
  171. ASSERT_TRUE(header.GetHeader("log-id"));
  172. ASSERT_EQ("456", *header.GetHeader("log-id"));
  173. ASSERT_TRUE(header.GetHeader("Authorization"));
  174. ASSERT_EQ("test", *header.GetHeader("Authorization"));
  175. }
  176. TEST(HttpMessageTest, bad_format) {
  177. const char *http_request =
  178. "slkdjflksdf skldjf\r\n";
  179. brpc::HttpMessage http_message;
  180. ASSERT_EQ(-1, http_message.ParseFromArray(http_request, strlen(http_request)));
  181. }
  182. TEST(HttpMessageTest, incompleted_request_line) {
  183. const char *http_request = "GE" ;
  184. brpc::HttpMessage http_message;
  185. ASSERT_TRUE(http_message.ParseFromArray(http_request, strlen(http_request)) >= 0);
  186. ASSERT_FALSE(http_message.Completed());
  187. }
  188. TEST(HttpMessageTest, parse_from_iobuf) {
  189. const size_t content_length = 8192;
  190. char header[1024];
  191. snprintf(header, sizeof(header),
  192. "GET /service/method?key1=value1&key2=value2&key3=value3 HTTP/1.1\r\n"
  193. "Content-Type: text/plain\r\n"
  194. "Content-Length: %lu\r\n"
  195. "\r\n",
  196. content_length);
  197. std::string content;
  198. for (size_t i = 0; i < content_length; ++i) content.push_back('2');
  199. butil::IOBuf request;
  200. request.append(header);
  201. request.append(content);
  202. brpc::HttpMessage http_message;
  203. ASSERT_TRUE(http_message.ParseFromIOBuf(request) >= 0);
  204. ASSERT_TRUE(http_message.Completed());
  205. ASSERT_EQ(content, http_message.body().to_string());
  206. ASSERT_EQ("text/plain", http_message.header().content_type());
  207. }
  208. TEST(HttpMessageTest, find_method_property_by_uri) {
  209. brpc::Server server;
  210. ASSERT_EQ(0, server.AddService(new test::EchoService(),
  211. brpc::SERVER_OWNS_SERVICE));
  212. ASSERT_EQ(0, server.Start(9237, NULL));
  213. std::string unknown_method;
  214. brpc::Server::MethodProperty* mp = NULL;
  215. mp = FindMethodPropertyByURI("", &server, NULL);
  216. ASSERT_TRUE(mp);
  217. ASSERT_EQ("index", mp->method->service()->name());
  218. mp = FindMethodPropertyByURI("/", &server, NULL);
  219. ASSERT_TRUE(mp);
  220. ASSERT_EQ("index", mp->method->service()->name());
  221. mp = FindMethodPropertyByURI("//", &server, NULL);
  222. ASSERT_TRUE(mp);
  223. ASSERT_EQ("index", mp->method->service()->name());
  224. mp = FindMethodPropertyByURI("flags", &server, &unknown_method);
  225. ASSERT_TRUE(mp);
  226. ASSERT_EQ("flags", mp->method->service()->name());
  227. mp = FindMethodPropertyByURI("/flags/port", &server, &unknown_method);
  228. ASSERT_TRUE(mp);
  229. ASSERT_EQ("flags", mp->method->service()->name());
  230. ASSERT_EQ("port", unknown_method);
  231. mp = FindMethodPropertyByURI("/flags/foo/bar", &server, &unknown_method);
  232. ASSERT_TRUE(mp);
  233. ASSERT_EQ("flags", mp->method->service()->name());
  234. ASSERT_EQ("foo/bar", unknown_method);
  235. mp = FindMethodPropertyByURI("/brpc.flags/$*",
  236. &server, &unknown_method);
  237. ASSERT_TRUE(mp);
  238. ASSERT_EQ("flags", mp->method->service()->name());
  239. ASSERT_EQ("$*", unknown_method);
  240. mp = FindMethodPropertyByURI("EchoService/Echo", &server, &unknown_method);
  241. ASSERT_TRUE(mp);
  242. ASSERT_EQ("test.EchoService.Echo", mp->method->full_name());
  243. mp = FindMethodPropertyByURI("/EchoService/Echo",
  244. &server, &unknown_method);
  245. ASSERT_TRUE(mp);
  246. ASSERT_EQ("test.EchoService.Echo", mp->method->full_name());
  247. mp = FindMethodPropertyByURI("/test.EchoService/Echo",
  248. &server, &unknown_method);
  249. ASSERT_TRUE(mp);
  250. ASSERT_EQ("test.EchoService.Echo", mp->method->full_name());
  251. mp = FindMethodPropertyByURI("/test.EchoService/no_such_method",
  252. &server, &unknown_method);
  253. ASSERT_FALSE(mp);
  254. }
  255. TEST(HttpMessageTest, http_header) {
  256. brpc::HttpHeader header;
  257. header.set_version(10, 100);
  258. ASSERT_EQ(10, header.major_version());
  259. ASSERT_EQ(100, header.minor_version());
  260. ASSERT_TRUE(header.content_type().empty());
  261. header.set_content_type("text/plain");
  262. ASSERT_EQ("text/plain", header.content_type());
  263. ASSERT_FALSE(header.GetHeader("content-type"));
  264. header.set_content_type("application/json");
  265. ASSERT_EQ("application/json", header.content_type());
  266. ASSERT_FALSE(header.GetHeader("content-type"));
  267. ASSERT_FALSE(header.GetHeader("key1"));
  268. header.AppendHeader("key1", "value1");
  269. const std::string* value = header.GetHeader("key1");
  270. ASSERT_TRUE(value && *value == "value1");
  271. header.AppendHeader("key1", "value2");
  272. value = header.GetHeader("key1");
  273. ASSERT_TRUE(value && *value == "value1,value2");
  274. header.SetHeader("key1", "value3");
  275. value = header.GetHeader("key1");
  276. ASSERT_TRUE(value && *value == "value3");
  277. header.RemoveHeader("key1");
  278. ASSERT_FALSE(header.GetHeader("key1"));
  279. ASSERT_EQ(brpc::HTTP_METHOD_GET, header.method());
  280. header.set_method(brpc::HTTP_METHOD_POST);
  281. ASSERT_EQ(brpc::HTTP_METHOD_POST, header.method());
  282. ASSERT_EQ(brpc::HTTP_STATUS_OK, header.status_code());
  283. ASSERT_STREQ(brpc::HttpReasonPhrase(header.status_code()),
  284. header.reason_phrase());
  285. header.set_status_code(brpc::HTTP_STATUS_CONTINUE);
  286. ASSERT_EQ(brpc::HTTP_STATUS_CONTINUE, header.status_code());
  287. ASSERT_STREQ(brpc::HttpReasonPhrase(header.status_code()),
  288. header.reason_phrase());
  289. header.set_status_code(brpc::HTTP_STATUS_GONE);
  290. ASSERT_EQ(brpc::HTTP_STATUS_GONE, header.status_code());
  291. ASSERT_STREQ(brpc::HttpReasonPhrase(header.status_code()),
  292. header.reason_phrase());
  293. }
  294. TEST(HttpMessageTest, empty_url) {
  295. butil::EndPoint host;
  296. ASSERT_FALSE(ParseHttpServerAddress(&host, ""));
  297. }
  298. TEST(HttpMessageTest, serialize_http_request) {
  299. brpc::HttpHeader header;
  300. ASSERT_EQ(0u, header.HeaderCount());
  301. header.SetHeader("Foo", "Bar");
  302. ASSERT_EQ(1u, header.HeaderCount());
  303. header.set_method(brpc::HTTP_METHOD_POST);
  304. butil::EndPoint ep;
  305. ASSERT_EQ(0, butil::str2endpoint("127.0.0.1:1234", &ep));
  306. butil::IOBuf request;
  307. butil::IOBuf content;
  308. content.append("data");
  309. MakeRawHttpRequest(&request, &header, ep, &content);
  310. ASSERT_EQ("POST / HTTP/1.1\r\nContent-Length: 4\r\nHost: 127.0.0.1:1234\r\nFoo: Bar\r\nAccept: */*\r\nUser-Agent: brpc/1.0 curl/7.0\r\n\r\ndata", request);
  311. // user-set content-length is ignored.
  312. header.SetHeader("Content-Length", "100");
  313. MakeRawHttpRequest(&request, &header, ep, &content);
  314. ASSERT_EQ("POST / HTTP/1.1\r\nContent-Length: 4\r\nHost: 127.0.0.1:1234\r\nFoo: Bar\r\nAccept: */*\r\nUser-Agent: brpc/1.0 curl/7.0\r\n\r\ndata", request);
  315. // user-host overwrites passed-in remote_side
  316. header.SetHeader("Host", "MyHost: 4321");
  317. MakeRawHttpRequest(&request, &header, ep, &content);
  318. ASSERT_EQ("POST / HTTP/1.1\r\nContent-Length: 4\r\nFoo: Bar\r\nHost: MyHost: 4321\r\nAccept: */*\r\nUser-Agent: brpc/1.0 curl/7.0\r\n\r\ndata", request);
  319. // user-set accept
  320. header.SetHeader("accePT"/*intended uppercase*/, "blahblah");
  321. MakeRawHttpRequest(&request, &header, ep, &content);
  322. ASSERT_EQ("POST / HTTP/1.1\r\nContent-Length: 4\r\naccePT: blahblah\r\nFoo: Bar\r\nHost: MyHost: 4321\r\nUser-Agent: brpc/1.0 curl/7.0\r\n\r\ndata", request);
  323. // user-set UA
  324. header.SetHeader("user-AGENT", "myUA");
  325. MakeRawHttpRequest(&request, &header, ep, &content);
  326. ASSERT_EQ("POST / HTTP/1.1\r\nContent-Length: 4\r\naccePT: blahblah\r\nuser-AGENT: myUA\r\nFoo: Bar\r\nHost: MyHost: 4321\r\n\r\ndata", request);
  327. // user-set Authorization
  328. header.SetHeader("authorization", "myAuthString");
  329. MakeRawHttpRequest(&request, &header, ep, &content);
  330. ASSERT_EQ("POST / HTTP/1.1\r\nContent-Length: 4\r\naccePT: blahblah\r\nuser-AGENT: myUA\r\nauthorization: myAuthString\r\nFoo: Bar\r\nHost: MyHost: 4321\r\n\r\ndata", request);
  331. // GET does not serialize content
  332. header.set_method(brpc::HTTP_METHOD_GET);
  333. MakeRawHttpRequest(&request, &header, ep, &content);
  334. ASSERT_EQ("GET / HTTP/1.1\r\naccePT: blahblah\r\nuser-AGENT: myUA\r\nauthorization: myAuthString\r\nFoo: Bar\r\nHost: MyHost: 4321\r\n\r\n", request);
  335. }
  336. TEST(HttpMessageTest, serialize_http_response) {
  337. brpc::HttpHeader header;
  338. header.SetHeader("Foo", "Bar");
  339. header.set_method(brpc::HTTP_METHOD_POST);
  340. butil::IOBuf response;
  341. butil::IOBuf content;
  342. content.append("data");
  343. MakeRawHttpResponse(&response, &header, &content);
  344. ASSERT_EQ("HTTP/1.1 200 OK\r\nContent-Length: 4\r\nFoo: Bar\r\n\r\ndata", response);
  345. // content is cleared.
  346. CHECK(content.empty());
  347. // user-set content-length is ignored.
  348. content.append("data2");
  349. header.SetHeader("Content-Length", "100");
  350. MakeRawHttpResponse(&response, &header, &content);
  351. ASSERT_EQ("HTTP/1.1 200 OK\r\nContent-Length: 5\r\nFoo: Bar\r\n\r\ndata2", response);
  352. // null content
  353. MakeRawHttpResponse(&response, &header, NULL);
  354. ASSERT_EQ("HTTP/1.1 200 OK\r\nFoo: Bar\r\n\r\n", response);
  355. }
  356. } //namespace