http_ut.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include <library/cpp/testing/unittest/tests_data.h>
  3. #include <library/cpp/actors/core/executor_pool_basic.h>
  4. #include <library/cpp/actors/core/scheduler_basic.h>
  5. #include <library/cpp/actors/testlib/test_runtime.h>
  6. #include <util/system/tempfile.h>
  7. #include "http.h"
  8. #include "http_proxy.h"
  9. enum EService : NActors::NLog::EComponent {
  10. MIN,
  11. Logger,
  12. MVP,
  13. MAX
  14. };
  15. namespace {
  16. template <typename HttpType>
  17. void EatWholeString(TIntrusivePtr<HttpType>& request, const TString& data) {
  18. request->EnsureEnoughSpaceAvailable(data.size());
  19. auto size = std::min(request->Avail(), data.size());
  20. memcpy(request->Pos(), data.data(), size);
  21. request->Advance(size);
  22. }
  23. template <typename HttpType>
  24. void EatPartialString(TIntrusivePtr<HttpType>& request, const TString& data) {
  25. for (char c : data) {
  26. request->EnsureEnoughSpaceAvailable(1);
  27. memcpy(request->Pos(), &c, 1);
  28. request->Advance(1);
  29. }
  30. }
  31. }
  32. Y_UNIT_TEST_SUITE(HttpProxy) {
  33. Y_UNIT_TEST(BasicParsing) {
  34. NHttp::THttpIncomingRequestPtr request = new NHttp::THttpIncomingRequest();
  35. EatWholeString(request, "GET /test HTTP/1.1\r\nHost: test\r\nSome-Header: 32344\r\n\r\n");
  36. UNIT_ASSERT_EQUAL(request->Stage, NHttp::THttpIncomingRequest::EParseStage::Done);
  37. UNIT_ASSERT_EQUAL(request->Method, "GET");
  38. UNIT_ASSERT_EQUAL(request->URL, "/test");
  39. UNIT_ASSERT_EQUAL(request->Protocol, "HTTP");
  40. UNIT_ASSERT_EQUAL(request->Version, "1.1");
  41. UNIT_ASSERT_EQUAL(request->Host, "test");
  42. UNIT_ASSERT_EQUAL(request->Headers, "Host: test\r\nSome-Header: 32344\r\n\r\n");
  43. }
  44. Y_UNIT_TEST(GetWithSpecifiedContentType) {
  45. NHttp::THttpIncomingRequestPtr request = new NHttp::THttpIncomingRequest();
  46. EatWholeString(request, "GET /test HTTP/1.1\r\nHost: test\r\nContent-Type: application/json\r\nSome-Header: 32344\r\n\r\n");
  47. UNIT_ASSERT_EQUAL(request->Stage, NHttp::THttpIncomingRequest::EParseStage::Done);
  48. UNIT_ASSERT_EQUAL(request->Method, "GET");
  49. UNIT_ASSERT_EQUAL(request->URL, "/test");
  50. UNIT_ASSERT_EQUAL(request->Protocol, "HTTP");
  51. UNIT_ASSERT_EQUAL(request->Version, "1.1");
  52. UNIT_ASSERT_EQUAL(request->Host, "test");
  53. UNIT_ASSERT_EQUAL(request->Headers, "Host: test\r\nContent-Type: application/json\r\nSome-Header: 32344\r\n\r\n");
  54. }
  55. Y_UNIT_TEST(BasicParsingChunkedBodyRequest) {
  56. NHttp::THttpIncomingRequestPtr request = new NHttp::THttpIncomingRequest();
  57. EatWholeString(request, "POST /Url HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n4\r\nthis\r\n4\r\n is \r\n5\r\ntest.\r\n0\r\n\r\n");
  58. UNIT_ASSERT_EQUAL(request->Stage, NHttp::THttpIncomingRequest::EParseStage::Done);
  59. UNIT_ASSERT_EQUAL(request->Method, "POST");
  60. UNIT_ASSERT_EQUAL(request->URL, "/Url");
  61. UNIT_ASSERT_EQUAL(request->Connection, "close");
  62. UNIT_ASSERT_EQUAL(request->Protocol, "HTTP");
  63. UNIT_ASSERT_EQUAL(request->Version, "1.1");
  64. UNIT_ASSERT_EQUAL(request->TransferEncoding, "chunked");
  65. UNIT_ASSERT_EQUAL(request->Body, "this is test.");
  66. }
  67. Y_UNIT_TEST(BasicPost) {
  68. NHttp::THttpIncomingRequestPtr request = new NHttp::THttpIncomingRequest();
  69. EatWholeString(request, "POST /Url HTTP/1.1\r\nConnection: close\r\nContent-Length: 13\r\n\r\nthis is test.");
  70. UNIT_ASSERT_EQUAL(request->Stage, NHttp::THttpIncomingRequest::EParseStage::Done);
  71. UNIT_ASSERT_EQUAL(request->Method, "POST");
  72. UNIT_ASSERT_EQUAL(request->URL, "/Url");
  73. UNIT_ASSERT_EQUAL(request->Connection, "close");
  74. UNIT_ASSERT_EQUAL(request->Protocol, "HTTP");
  75. UNIT_ASSERT_EQUAL(request->Version, "1.1");
  76. UNIT_ASSERT_EQUAL(request->TransferEncoding, "");
  77. UNIT_ASSERT_EQUAL(request->Body, "this is test.");
  78. }
  79. Y_UNIT_TEST(BasicParsingChunkedBodyResponse) {
  80. NHttp::THttpOutgoingRequestPtr request = nullptr; //new NHttp::THttpOutgoingRequest();
  81. NHttp::THttpIncomingResponsePtr response = new NHttp::THttpIncomingResponse(request);
  82. EatWholeString(response, "HTTP/1.1 200 OK\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n4\r\nthis\r\n4\r\n is \r\n5\r\ntest.\r\n0\r\n\r\n");
  83. UNIT_ASSERT_EQUAL(response->Stage, NHttp::THttpIncomingResponse::EParseStage::Done);
  84. UNIT_ASSERT_EQUAL(response->Status, "200");
  85. UNIT_ASSERT_EQUAL(response->Connection, "close");
  86. UNIT_ASSERT_EQUAL(response->Protocol, "HTTP");
  87. UNIT_ASSERT_EQUAL(response->Version, "1.1");
  88. UNIT_ASSERT_EQUAL(response->TransferEncoding, "chunked");
  89. UNIT_ASSERT_EQUAL(response->Body, "this is test.");
  90. }
  91. Y_UNIT_TEST(InvalidParsingChunkedBody) {
  92. NHttp::THttpOutgoingRequestPtr request = nullptr; //new NHttp::THttpOutgoingRequest();
  93. NHttp::THttpIncomingResponsePtr response = new NHttp::THttpIncomingResponse(request);
  94. EatWholeString(response, "HTTP/1.1 200 OK\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nthis\r\n4\r\n is \r\n5\r\ntest.\r\n0\r\n\r\n");
  95. UNIT_ASSERT(response->IsError());
  96. }
  97. Y_UNIT_TEST(AdvancedParsingChunkedBody) {
  98. NHttp::THttpOutgoingRequestPtr request = nullptr; //new NHttp::THttpOutgoingRequest();
  99. NHttp::THttpIncomingResponsePtr response = new NHttp::THttpIncomingResponse(request);
  100. EatWholeString(response, "HTTP/1.1 200 OK\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n6\r\nthis\r\n\r\n4\r\n is \r\n5\r\ntest.\r\n0\r\n\r\n");
  101. UNIT_ASSERT_EQUAL(response->Stage, NHttp::THttpIncomingResponse::EParseStage::Done);
  102. UNIT_ASSERT_EQUAL(response->Status, "200");
  103. UNIT_ASSERT_EQUAL(response->Connection, "close");
  104. UNIT_ASSERT_EQUAL(response->Protocol, "HTTP");
  105. UNIT_ASSERT_EQUAL(response->Version, "1.1");
  106. UNIT_ASSERT_EQUAL(response->TransferEncoding, "chunked");
  107. UNIT_ASSERT_EQUAL(response->Body, "this\r\n is test.");
  108. }
  109. Y_UNIT_TEST(CreateCompressedResponse) {
  110. NHttp::THttpIncomingRequestPtr request = new NHttp::THttpIncomingRequest();
  111. EatWholeString(request, "GET /Url HTTP/1.1\r\nConnection: close\r\nAccept-Encoding: gzip, deflate\r\n\r\n");
  112. NHttp::THttpOutgoingResponsePtr response = new NHttp::THttpOutgoingResponse(request, "HTTP", "1.1", "200", "OK");
  113. TString compressedBody = "something very long to compress with deflate algorithm. something very long to compress with deflate algorithm.";
  114. response->EnableCompression();
  115. size_t size1 = response->Size();
  116. response->SetBody(compressedBody);
  117. size_t size2 = response->Size();
  118. size_t compressedBodySize = size2 - size1;
  119. UNIT_ASSERT_VALUES_EQUAL("deflate", response->ContentEncoding);
  120. UNIT_ASSERT(compressedBodySize < compressedBody.size());
  121. NHttp::THttpOutgoingResponsePtr response2 = response->Duplicate(request);
  122. UNIT_ASSERT_VALUES_EQUAL(response->Body, response2->Body);
  123. UNIT_ASSERT_VALUES_EQUAL(response->ContentLength, response2->ContentLength);
  124. UNIT_ASSERT_VALUES_EQUAL(response->Size(), response2->Size());
  125. }
  126. Y_UNIT_TEST(BasicPartialParsing) {
  127. NHttp::THttpIncomingRequestPtr request = new NHttp::THttpIncomingRequest();
  128. EatPartialString(request, "GET /test HTTP/1.1\r\nHost: test\r\nSome-Header: 32344\r\n\r\n");
  129. UNIT_ASSERT_EQUAL(request->Stage, NHttp::THttpIncomingRequest::EParseStage::Done);
  130. UNIT_ASSERT_EQUAL(request->Method, "GET");
  131. UNIT_ASSERT_EQUAL(request->URL, "/test");
  132. UNIT_ASSERT_EQUAL(request->Protocol, "HTTP");
  133. UNIT_ASSERT_EQUAL(request->Version, "1.1");
  134. UNIT_ASSERT_EQUAL(request->Host, "test");
  135. UNIT_ASSERT_EQUAL(request->Headers, "Host: test\r\nSome-Header: 32344\r\n\r\n");
  136. }
  137. Y_UNIT_TEST(BasicPartialParsingChunkedBody) {
  138. NHttp::THttpOutgoingRequestPtr request = nullptr; //new NHttp::THttpOutgoingRequest();
  139. NHttp::THttpIncomingResponsePtr response = new NHttp::THttpIncomingResponse(request);
  140. EatPartialString(response, "HTTP/1.1 200 OK\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n4\r\nthis\r\n4\r\n is \r\n5\r\ntest.\r\n0\r\n\r\n");
  141. UNIT_ASSERT_EQUAL(response->Stage, NHttp::THttpIncomingResponse::EParseStage::Done);
  142. UNIT_ASSERT_EQUAL(response->Status, "200");
  143. UNIT_ASSERT_EQUAL(response->Connection, "close");
  144. UNIT_ASSERT_EQUAL(response->Protocol, "HTTP");
  145. UNIT_ASSERT_EQUAL(response->Version, "1.1");
  146. UNIT_ASSERT_EQUAL(response->TransferEncoding, "chunked");
  147. UNIT_ASSERT_EQUAL(response->Body, "this is test.");
  148. }
  149. Y_UNIT_TEST(BasicParsingContentLength0) {
  150. NHttp::THttpIncomingRequestPtr request = new NHttp::THttpIncomingRequest();
  151. EatPartialString(request, "GET /test HTTP/1.1\r\nHost: test\r\nContent-Length: 0\r\n\r\n");
  152. UNIT_ASSERT_EQUAL(request->Stage, NHttp::THttpIncomingRequest::EParseStage::Done);
  153. UNIT_ASSERT_EQUAL(request->Method, "GET");
  154. UNIT_ASSERT_EQUAL(request->URL, "/test");
  155. UNIT_ASSERT_EQUAL(request->Protocol, "HTTP");
  156. UNIT_ASSERT_EQUAL(request->Version, "1.1");
  157. UNIT_ASSERT_EQUAL(request->Host, "test");
  158. UNIT_ASSERT_EQUAL(request->Headers, "Host: test\r\nContent-Length: 0\r\n\r\n");
  159. }
  160. Y_UNIT_TEST(AdvancedParsing) {
  161. NHttp::THttpIncomingRequestPtr request = new NHttp::THttpIncomingRequest();
  162. EatWholeString(request, "GE");
  163. EatWholeString(request, "T");
  164. EatWholeString(request, " ");
  165. EatWholeString(request, "/test");
  166. EatWholeString(request, " HTTP/1.1\r");
  167. EatWholeString(request, "\nHo");
  168. EatWholeString(request, "st: test");
  169. EatWholeString(request, "\r\n");
  170. EatWholeString(request, "Some-Header: 32344\r\n\r");
  171. EatWholeString(request, "\n");
  172. UNIT_ASSERT_EQUAL(request->Stage, NHttp::THttpIncomingRequest::EParseStage::Done);
  173. UNIT_ASSERT_EQUAL(request->Method, "GET");
  174. UNIT_ASSERT_EQUAL(request->URL, "/test");
  175. UNIT_ASSERT_EQUAL(request->Protocol, "HTTP");
  176. UNIT_ASSERT_EQUAL(request->Version, "1.1");
  177. UNIT_ASSERT_EQUAL(request->Host, "test");
  178. UNIT_ASSERT_EQUAL(request->Headers, "Host: test\r\nSome-Header: 32344\r\n\r\n");
  179. }
  180. Y_UNIT_TEST(AdvancedPartialParsing) {
  181. NHttp::THttpIncomingRequestPtr request = new NHttp::THttpIncomingRequest();
  182. EatPartialString(request, "GE");
  183. EatPartialString(request, "T");
  184. EatPartialString(request, " ");
  185. EatPartialString(request, "/test");
  186. EatPartialString(request, " HTTP/1.1\r");
  187. EatPartialString(request, "\nHo");
  188. EatPartialString(request, "st: test");
  189. EatPartialString(request, "\r\n");
  190. EatPartialString(request, "Some-Header: 32344\r\n\r");
  191. EatPartialString(request, "\n");
  192. UNIT_ASSERT_EQUAL(request->Stage, NHttp::THttpIncomingRequest::EParseStage::Done);
  193. UNIT_ASSERT_EQUAL(request->Method, "GET");
  194. UNIT_ASSERT_EQUAL(request->URL, "/test");
  195. UNIT_ASSERT_EQUAL(request->Protocol, "HTTP");
  196. UNIT_ASSERT_EQUAL(request->Version, "1.1");
  197. UNIT_ASSERT_EQUAL(request->Host, "test");
  198. UNIT_ASSERT_EQUAL(request->Headers, "Host: test\r\nSome-Header: 32344\r\n\r\n");
  199. }
  200. Y_UNIT_TEST(BasicRenderBodyWithHeadersAndCookies) {
  201. NHttp::THttpOutgoingRequestPtr request = NHttp::THttpOutgoingRequest::CreateRequestGet("http://www.yandex.ru/data/url");
  202. NHttp::THeadersBuilder headers;
  203. NHttp::TCookiesBuilder cookies;
  204. cookies.Set("cookie1", "123456");
  205. cookies.Set("cookie2", "45678");
  206. headers.Set("Cookie", cookies.Render());
  207. request->Set(headers);
  208. TString requestData = request->AsString();
  209. UNIT_ASSERT_VALUES_EQUAL(requestData, "GET /data/url HTTP/1.1\r\nHost: www.yandex.ru\r\nAccept: */*\r\nCookie: cookie1=123456; cookie2=45678;\r\n");
  210. }
  211. Y_UNIT_TEST(BasicRenderOutgoingResponse) {
  212. NHttp::THttpIncomingRequestPtr request = new NHttp::THttpIncomingRequest();
  213. EatWholeString(request, "GET /test HTTP/1.1\r\nHost: test\r\nSome-Header: 32344\r\n\r\n");
  214. NHttp::THttpOutgoingResponsePtr httpResponseOk = request->CreateResponseOK("response ok");
  215. UNIT_ASSERT_EQUAL(httpResponseOk->Stage, NHttp::THttpOutgoingResponse::ERenderStage::Done);
  216. UNIT_ASSERT_STRINGS_EQUAL(httpResponseOk->Status, "200");
  217. UNIT_ASSERT_STRINGS_EQUAL(httpResponseOk->Message, "OK");
  218. UNIT_ASSERT_STRINGS_EQUAL(httpResponseOk->ContentType, "text/html");
  219. UNIT_ASSERT_STRINGS_EQUAL(httpResponseOk->Body, "response ok");
  220. NHttp::THttpOutgoingResponsePtr httpResponseBadRequest = request->CreateResponseBadRequest();
  221. UNIT_ASSERT_EQUAL(httpResponseBadRequest->Stage, NHttp::THttpOutgoingResponse::ERenderStage::Done);
  222. UNIT_ASSERT_STRINGS_EQUAL(httpResponseBadRequest->Status, "400");
  223. UNIT_ASSERT_STRINGS_EQUAL(httpResponseBadRequest->Message, "Bad Request");
  224. UNIT_ASSERT(httpResponseBadRequest->ContentType.empty());
  225. UNIT_ASSERT(httpResponseBadRequest->Body.empty());
  226. NHttp::THttpOutgoingResponsePtr httpResponseNotFound = request->CreateResponseNotFound();
  227. UNIT_ASSERT_EQUAL(httpResponseNotFound->Stage, NHttp::THttpOutgoingResponse::ERenderStage::Done);
  228. UNIT_ASSERT_STRINGS_EQUAL(httpResponseNotFound->Status, "404");
  229. UNIT_ASSERT_STRINGS_EQUAL(httpResponseNotFound->Message, "Not Found");
  230. UNIT_ASSERT(httpResponseNotFound->ContentType.empty());
  231. UNIT_ASSERT(httpResponseNotFound->Body.empty());
  232. NHttp::THttpOutgoingResponsePtr httpResponseServiceUnavailable = request->CreateResponseServiceUnavailable();
  233. UNIT_ASSERT_EQUAL(httpResponseServiceUnavailable->Stage, NHttp::THttpOutgoingResponse::ERenderStage::Done);
  234. UNIT_ASSERT_STRINGS_EQUAL(httpResponseServiceUnavailable->Status, "503");
  235. UNIT_ASSERT_STRINGS_EQUAL(httpResponseServiceUnavailable->Message, "Service Unavailable");
  236. UNIT_ASSERT(httpResponseServiceUnavailable->ContentType.empty());
  237. UNIT_ASSERT(httpResponseServiceUnavailable->Body.empty());
  238. NHttp::THttpOutgoingResponsePtr httpResponseGatewayTimeout = request->CreateResponseGatewayTimeout("gateway timeout body");
  239. UNIT_ASSERT_EQUAL(httpResponseGatewayTimeout->Stage, NHttp::THttpOutgoingResponse::ERenderStage::Done);
  240. UNIT_ASSERT_STRINGS_EQUAL(httpResponseGatewayTimeout->Status, "504");
  241. UNIT_ASSERT_STRINGS_EQUAL(httpResponseGatewayTimeout->Message, "Gateway Timeout");
  242. UNIT_ASSERT_STRINGS_EQUAL(httpResponseGatewayTimeout->ContentType, "text/html");
  243. UNIT_ASSERT_STRINGS_EQUAL(httpResponseGatewayTimeout->Body, "gateway timeout body");
  244. NHttp::THttpOutgoingResponsePtr httpIncompleteResponse = request->CreateIncompleteResponse("401", "Unauthorized");
  245. UNIT_ASSERT_EQUAL(httpIncompleteResponse->Stage, NHttp::THttpOutgoingResponse::ERenderStage::Header);
  246. UNIT_ASSERT_STRINGS_EQUAL(httpIncompleteResponse->Status, "401");
  247. UNIT_ASSERT_STRINGS_EQUAL(httpIncompleteResponse->Message, "Unauthorized");
  248. NHttp::THttpOutgoingResponsePtr httpResponse = request->CreateResponse("401", "Unauthorized");
  249. UNIT_ASSERT_EQUAL(httpResponse->Stage, NHttp::THttpOutgoingResponse::ERenderStage::Done);
  250. UNIT_ASSERT_STRINGS_EQUAL(httpResponse->Status, "401");
  251. UNIT_ASSERT_STRINGS_EQUAL(httpResponse->Message, "Unauthorized");
  252. NHttp::THeadersBuilder headers;
  253. NHttp::TCookiesBuilder cookies;
  254. cookies.Set("cookie1", "123456");
  255. headers.Set("Set-Cookie", cookies.Render());
  256. headers.Set("Location", "http://www.yandex.ru/data/url");
  257. NHttp::THttpOutgoingResponsePtr httpResponseRedirect = request->CreateResponse("302", "Found", headers);
  258. UNIT_ASSERT_EQUAL(httpResponseRedirect->Stage, NHttp::THttpOutgoingResponse::ERenderStage::Done);
  259. UNIT_ASSERT_STRINGS_EQUAL(httpResponseRedirect->Status, "302");
  260. UNIT_ASSERT_STRINGS_EQUAL(httpResponseRedirect->Message, "Found");
  261. UNIT_ASSERT_STRING_CONTAINS(httpResponseRedirect->Headers, "Set-Cookie: cookie1=123456;");
  262. UNIT_ASSERT_STRING_CONTAINS(httpResponseRedirect->Headers, "Location: http://www.yandex.ru/data/url");
  263. }
  264. Y_UNIT_TEST(BasicRunning4) {
  265. NActors::TTestActorRuntimeBase actorSystem;
  266. TPortManager portManager;
  267. TIpPort port = portManager.GetTcpPort();
  268. TAutoPtr<NActors::IEventHandle> handle;
  269. actorSystem.Initialize();
  270. //actorSystem.SetLogPriority(NActorsServices::HTTP, NActors::NLog::PRI_DEBUG);
  271. NActors::IActor* proxy = NHttp::CreateHttpProxy();
  272. NActors::TActorId proxyId = actorSystem.Register(proxy);
  273. actorSystem.Send(new NActors::IEventHandle(proxyId, TActorId(), new NHttp::TEvHttpProxy::TEvAddListeningPort(port)), 0, true);
  274. actorSystem.DispatchEvents();
  275. NActors::TActorId serverId = actorSystem.AllocateEdgeActor();
  276. actorSystem.Send(new NActors::IEventHandle(proxyId, serverId, new NHttp::TEvHttpProxy::TEvRegisterHandler("/test", serverId)), 0, true);
  277. NActors::TActorId clientId = actorSystem.AllocateEdgeActor();
  278. NHttp::THttpOutgoingRequestPtr httpRequest = NHttp::THttpOutgoingRequest::CreateRequestGet("http://127.0.0.1:" + ToString(port) + "/test");
  279. actorSystem.Send(new NActors::IEventHandle(proxyId, clientId, new NHttp::TEvHttpProxy::TEvHttpOutgoingRequest(httpRequest)), 0, true);
  280. NHttp::TEvHttpProxy::TEvHttpIncomingRequest* request = actorSystem.GrabEdgeEvent<NHttp::TEvHttpProxy::TEvHttpIncomingRequest>(handle);
  281. UNIT_ASSERT_EQUAL(request->Request->URL, "/test");
  282. NHttp::THttpOutgoingResponsePtr httpResponse = request->Request->CreateResponseString("HTTP/1.1 200 Found\r\nConnection: Close\r\nTransfer-Encoding: chunked\r\n\r\n6\r\npassed\r\n0\r\n\r\n");
  283. actorSystem.Send(new NActors::IEventHandle(handle->Sender, serverId, new NHttp::TEvHttpProxy::TEvHttpOutgoingResponse(httpResponse)), 0, true);
  284. NHttp::TEvHttpProxy::TEvHttpIncomingResponse* response = actorSystem.GrabEdgeEvent<NHttp::TEvHttpProxy::TEvHttpIncomingResponse>(handle);
  285. UNIT_ASSERT_EQUAL(response->Response->Status, "200");
  286. UNIT_ASSERT_EQUAL(response->Response->Body, "passed");
  287. }
  288. Y_UNIT_TEST(BasicRunning6) {
  289. NActors::TTestActorRuntimeBase actorSystem;
  290. TPortManager portManager;
  291. TIpPort port = portManager.GetTcpPort();
  292. TAutoPtr<NActors::IEventHandle> handle;
  293. actorSystem.Initialize();
  294. //actorSystem.SetLogPriority(NActorsServices::HTTP, NActors::NLog::PRI_DEBUG);
  295. NActors::IActor* proxy = NHttp::CreateHttpProxy();
  296. NActors::TActorId proxyId = actorSystem.Register(proxy);
  297. actorSystem.Send(new NActors::IEventHandle(proxyId, TActorId(), new NHttp::TEvHttpProxy::TEvAddListeningPort(port)), 0, true);
  298. actorSystem.DispatchEvents();
  299. NActors::TActorId serverId = actorSystem.AllocateEdgeActor();
  300. actorSystem.Send(new NActors::IEventHandle(proxyId, serverId, new NHttp::TEvHttpProxy::TEvRegisterHandler("/test", serverId)), 0, true);
  301. NActors::TActorId clientId = actorSystem.AllocateEdgeActor();
  302. NHttp::THttpOutgoingRequestPtr httpRequest = NHttp::THttpOutgoingRequest::CreateRequestGet("http://[::1]:" + ToString(port) + "/test");
  303. actorSystem.Send(new NActors::IEventHandle(proxyId, clientId, new NHttp::TEvHttpProxy::TEvHttpOutgoingRequest(httpRequest)), 0, true);
  304. NHttp::TEvHttpProxy::TEvHttpIncomingRequest* request = actorSystem.GrabEdgeEvent<NHttp::TEvHttpProxy::TEvHttpIncomingRequest>(handle);
  305. UNIT_ASSERT_EQUAL(request->Request->URL, "/test");
  306. NHttp::THttpOutgoingResponsePtr httpResponse = request->Request->CreateResponseString("HTTP/1.1 200 Found\r\nConnection: Close\r\nTransfer-Encoding: chunked\r\n\r\n6\r\npassed\r\n0\r\n\r\n");
  307. actorSystem.Send(new NActors::IEventHandle(handle->Sender, serverId, new NHttp::TEvHttpProxy::TEvHttpOutgoingResponse(httpResponse)), 0, true);
  308. NHttp::TEvHttpProxy::TEvHttpIncomingResponse* response = actorSystem.GrabEdgeEvent<NHttp::TEvHttpProxy::TEvHttpIncomingResponse>(handle);
  309. UNIT_ASSERT_EQUAL(response->Response->Status, "200");
  310. UNIT_ASSERT_EQUAL(response->Response->Body, "passed");
  311. }
  312. Y_UNIT_TEST(TlsRunning) {
  313. NActors::TTestActorRuntimeBase actorSystem;
  314. TPortManager portManager;
  315. TIpPort port = portManager.GetTcpPort();
  316. TAutoPtr<NActors::IEventHandle> handle;
  317. actorSystem.Initialize();
  318. TString certificateContent = R"___(-----BEGIN PRIVATE KEY-----
  319. MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCzRZjodO7Aqe1w
  320. RyOj6kG6g2nn8ZGAxfao4mLT0jDTbVksrhV/h2s3uldLkFo5WrNQ8WZe+iIbXeFL
  321. s8tO6hslzreo9sih2IHoRcH5KnS/6YTqVhRTJb1jE2dM8NwYbwTi+T2Pe0FrBPjI
  322. kgVO50gAtYl9C+fc715uZiSKW+rRlP5OoFTwxrOjiU27RPZjFYyWK9wTI1Es9uRr
  323. lbZbLl5cY6dK2J1AViRraaYKCWO26VbOPWLsY4OD3e+ZXIc3OMCz6Yb0wmRPeJ60
  324. bbbkGfI8O27kDdv69MAWHIm0yYMzKEnom1dce7rNQNDEqJfocsYIsg+EvayT1yQ9
  325. KTBegw7LAgMBAAECggEBAKaOCrotqYQmXArsjRhFFDwMy+BKdzyEr93INrlFl0dX
  326. WHpCYobRcbOc1G3H94tB0UdqgAnNqtJyLlb+++ydZAuEOu4oGc8EL+10ofq0jzOd
  327. 6Xct8kQt0/6wkFDTlii9PHUDy0X65ZRgUiNGRtg/2I2QG+SpowmI+trm2xwQueFs
  328. VaWrjc3cVvXx0b8Lu7hqZUv08kgC38stzuRk/n2T5VWSAr7Z4ZWQbO918Dv35HUw
  329. Wy/0jNUFP9CBCvFJ4l0OoH9nYhWFG+HXWzNdw6/Hca4jciRKo6esCiOZ9uWYv/ec
  330. /NvX9rgFg8G8/SrTisX10+Bbeq+R1RKwq/IG409TH4ECgYEA14L+3QsgNIUMeYAx
  331. jSCyk22R/tOHI1BM+GtKPUhnwHlAssrcPcxXMJovl6WL93VauYjym0wpCz9urSpA
  332. I2CqTsG8GYciA6Dr3mHgD6cK0jj9UPAU6EnZ5S0mjhPqKZqutu9QegzD2uESvuN8
  333. 36xezwQthzAf0nI/P3sJGjVXjikCgYEA1POm5xcV6SmM6HnIdadEebhzZIJ9TXQz
  334. ry3Jj3a7CKyD5C7fAdkHUTCjgT/2ElxPi9ABkZnC+d/cW9GtJFa0II5qO/agm3KQ
  335. ZXYiutu9A7xACHYFXRiJEjVUdGG9dKMVOHUEa8IHEgrrcUVM/suy/GgutywIfaXs
  336. y58IFP24K9MCgYEAk6zjz7wL+XEiNy+sxLQfKf7vB9sSwxQHakK6wHuY/L8Zomp3
  337. uLEJHfjJm/SIkK0N2g0JkXkCtv5kbKyC/rsCeK0wo52BpVLjzaLr0k34kE0U6B1b
  338. dkEE2pGx1bG3x4KDLj+Wuct9ecK5Aa0IqIyI+vo16GkFpUM8K9e3SQo8UOECgYEA
  339. sCZYAkILYtJ293p9giz5rIISGasDAUXE1vxWBXEeJ3+kneTTnZCrx9Im/ewtnWR0
  340. fF90XL9HFDDD88POqAd8eo2zfKR2l/89SGBfPBg2EtfuU9FkgGyiPciVcqvC7q9U
  341. B15saMKX3KnhtdGwbfeLt9RqCCTJZT4SUSDcq5hwdvcCgYAxY4Be8mNipj8Cgg22
  342. mVWSolA0TEzbtUcNk6iGodpi+Z0LKpsPC0YRqPRyh1K+rIltG1BVdmUBHcMlOYxl
  343. lWWvbJH6PkJWy4n2MF7PO45kjN3pPZg4hgH63JjZeAineBwEArUGb9zHnvzcdRvF
  344. wuQ2pZHL/HJ0laUSieHDJ5917w==
  345. -----END PRIVATE KEY-----
  346. -----BEGIN CERTIFICATE-----
  347. MIIDjTCCAnWgAwIBAgIURt5IBx0J3xgEaQvmyrFH2A+NkpMwDQYJKoZIhvcNAQEL
  348. BQAwVjELMAkGA1UEBhMCUlUxDzANBgNVBAgMBk1vc2NvdzEPMA0GA1UEBwwGTW9z
  349. Y293MQ8wDQYDVQQKDAZZYW5kZXgxFDASBgNVBAMMC3Rlc3Qtc2VydmVyMB4XDTE5
  350. MDkyMDE3MTQ0MVoXDTQ3MDIwNDE3MTQ0MVowVjELMAkGA1UEBhMCUlUxDzANBgNV
  351. BAgMBk1vc2NvdzEPMA0GA1UEBwwGTW9zY293MQ8wDQYDVQQKDAZZYW5kZXgxFDAS
  352. BgNVBAMMC3Rlc3Qtc2VydmVyMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC
  353. AQEAs0WY6HTuwKntcEcjo+pBuoNp5/GRgMX2qOJi09Iw021ZLK4Vf4drN7pXS5Ba
  354. OVqzUPFmXvoiG13hS7PLTuobJc63qPbIodiB6EXB+Sp0v+mE6lYUUyW9YxNnTPDc
  355. GG8E4vk9j3tBawT4yJIFTudIALWJfQvn3O9ebmYkilvq0ZT+TqBU8Mazo4lNu0T2
  356. YxWMlivcEyNRLPbka5W2Wy5eXGOnStidQFYka2mmCgljtulWzj1i7GODg93vmVyH
  357. NzjAs+mG9MJkT3ietG225BnyPDtu5A3b+vTAFhyJtMmDMyhJ6JtXXHu6zUDQxKiX
  358. 6HLGCLIPhL2sk9ckPSkwXoMOywIDAQABo1MwUTAdBgNVHQ4EFgQUDv/xuJ4CvCgG
  359. fPrZP3hRAt2+/LwwHwYDVR0jBBgwFoAUDv/xuJ4CvCgGfPrZP3hRAt2+/LwwDwYD
  360. VR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAinKpMYaA2tjLpAnPVbjy
  361. /ZxSBhhB26RiQp3Re8XOKyhTWqgYE6kldYT0aXgK9x9mPC5obQannDDYxDc7lX+/
  362. qP/u1X81ZcDRo/f+qQ3iHfT6Ftt/4O3qLnt45MFM6Q7WabRm82x3KjZTqpF3QUdy
  363. tumWiuAP5DMd1IRDtnKjFHO721OsEsf6NLcqdX89bGeqXDvrkwg3/PNwTyW5E7cj
  364. feY8L2eWtg6AJUnIBu11wvfzkLiH3QKzHvO/SIZTGf5ihDsJ3aKEE9UNauTL3bVc
  365. CRA/5XcX13GJwHHj6LCoc3sL7mt8qV9HKY2AOZ88mpObzISZxgPpdKCfjsrdm63V
  366. 6g==
  367. -----END CERTIFICATE-----)___";
  368. TTempFileHandle certificateFile;
  369. certificateFile.Write(certificateContent.data(), certificateContent.size());
  370. NActors::IActor* proxy = NHttp::CreateHttpProxy();
  371. NActors::TActorId proxyId = actorSystem.Register(proxy);
  372. THolder<NHttp::TEvHttpProxy::TEvAddListeningPort> add = MakeHolder<NHttp::TEvHttpProxy::TEvAddListeningPort>(port);
  373. ///////// https configuration
  374. add->Secure = true;
  375. add->CertificateFile = certificateFile.Name();
  376. add->PrivateKeyFile = certificateFile.Name();
  377. /////////
  378. actorSystem.Send(new NActors::IEventHandle(proxyId, TActorId(), add.Release()), 0, true);
  379. actorSystem.DispatchEvents();
  380. NActors::TActorId serverId = actorSystem.AllocateEdgeActor();
  381. actorSystem.Send(new NActors::IEventHandle(proxyId, serverId, new NHttp::TEvHttpProxy::TEvRegisterHandler("/test", serverId)), 0, true);
  382. NActors::TActorId clientId = actorSystem.AllocateEdgeActor();
  383. NHttp::THttpOutgoingRequestPtr httpRequest = NHttp::THttpOutgoingRequest::CreateRequestGet("https://[::1]:" + ToString(port) + "/test");
  384. actorSystem.Send(new NActors::IEventHandle(proxyId, clientId, new NHttp::TEvHttpProxy::TEvHttpOutgoingRequest(httpRequest)), 0, true);
  385. NHttp::TEvHttpProxy::TEvHttpIncomingRequest* request = actorSystem.GrabEdgeEvent<NHttp::TEvHttpProxy::TEvHttpIncomingRequest>(handle);
  386. UNIT_ASSERT_EQUAL(request->Request->URL, "/test");
  387. NHttp::THttpOutgoingResponsePtr httpResponse = request->Request->CreateResponseString("HTTP/1.1 200 Found\r\nConnection: Close\r\nTransfer-Encoding: chunked\r\n\r\n6\r\npassed\r\n0\r\n\r\n");
  388. actorSystem.Send(new NActors::IEventHandle(handle->Sender, serverId, new NHttp::TEvHttpProxy::TEvHttpOutgoingResponse(httpResponse)), 0, true);
  389. NHttp::TEvHttpProxy::TEvHttpIncomingResponse* response = actorSystem.GrabEdgeEvent<NHttp::TEvHttpProxy::TEvHttpIncomingResponse>(handle);
  390. UNIT_ASSERT_EQUAL(response->Response->Status, "200");
  391. UNIT_ASSERT_EQUAL(response->Response->Body, "passed");
  392. }
  393. /*Y_UNIT_TEST(AdvancedRunning) {
  394. THolder<NActors::TActorSystemSetup> setup = MakeHolder<NActors::TActorSystemSetup>();
  395. setup->NodeId = 1;
  396. setup->ExecutorsCount = 1;
  397. setup->Executors = new TAutoPtr<NActors::IExecutorPool>[1];
  398. setup->Executors[0] = new NActors::TBasicExecutorPool(0, 2, 10);
  399. setup->Scheduler = new NActors::TBasicSchedulerThread(NActors::TSchedulerConfig(512, 100));
  400. NActors::TActorSystem actorSystem(setup);
  401. actorSystem.Start();
  402. NHttp::THttpProxy* incomingProxy = new NHttp::THttpProxy();
  403. NActors::TActorId incomingProxyId = actorSystem.Register(incomingProxy);
  404. actorSystem.Send(incomingProxyId, new NHttp::TEvHttpProxy::TEvAddListeningPort(13337));
  405. NHttp::THttpProxy* outgoingProxy = new NHttp::THttpProxy();
  406. NActors::TActorId outgoingProxyId = actorSystem.Register(outgoingProxy);
  407. THolder<NHttp::THttpStaticStringRequest> httpRequest = MakeHolder<NHttp::THttpStaticStringRequest>("GET /test HTTP/1.1\r\n\r\n");
  408. actorSystem.Send(outgoingProxyId, new NHttp::TEvHttpProxy::TEvHttpOutgoingRequest("[::]:13337", std::move(httpRequest)));
  409. Sleep(TDuration::Minutes(60));
  410. }*/
  411. Y_UNIT_TEST(TooLongHeader) {
  412. NActors::TTestActorRuntimeBase actorSystem;
  413. actorSystem.SetUseRealInterconnect();
  414. TPortManager portManager;
  415. TIpPort port = portManager.GetTcpPort();
  416. TAutoPtr<NActors::IEventHandle> handle;
  417. actorSystem.Initialize();
  418. NActors::IActor* proxy = NHttp::CreateHttpProxy();
  419. NActors::TActorId proxyId = actorSystem.Register(proxy);
  420. actorSystem.Send(new NActors::IEventHandle(proxyId, TActorId(), new NHttp::TEvHttpProxy::TEvAddListeningPort(port)), 0, true);
  421. actorSystem.DispatchEvents();
  422. NActors::TActorId serverId = actorSystem.AllocateEdgeActor();
  423. actorSystem.Send(new NActors::IEventHandle(proxyId, serverId, new NHttp::TEvHttpProxy::TEvRegisterHandler("/test", serverId)), 0, true);
  424. NActors::TActorId clientId = actorSystem.AllocateEdgeActor();
  425. NHttp::THttpOutgoingRequestPtr httpRequest = NHttp::THttpOutgoingRequest::CreateRequestGet("http://[::1]:" + ToString(port) + "/test");
  426. httpRequest->Set("Connection", "close");
  427. TString longHeader;
  428. longHeader.append(9000, 'X');
  429. httpRequest->Set(longHeader, "data");
  430. actorSystem.Send(new NActors::IEventHandle(proxyId, clientId, new NHttp::TEvHttpProxy::TEvHttpOutgoingRequest(httpRequest)), 0, true);
  431. NHttp::TEvHttpProxy::TEvHttpIncomingResponse* response = actorSystem.GrabEdgeEvent<NHttp::TEvHttpProxy::TEvHttpIncomingResponse>(handle);
  432. UNIT_ASSERT_EQUAL(response->Response->Status, "400");
  433. UNIT_ASSERT_EQUAL(response->Response->Body, "Invalid http header");
  434. }
  435. }