Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

703 linhas
26 KiB

17 anos atrás
17 anos atrás
13 anos atrás
11 anos atrás
17 anos atrás
17 anos atrás
17 anos atrás
17 anos atrás
17 anos atrás
17 anos atrás
17 anos atrás
17 anos atrás
17 anos atrás
17 anos atrás
17 anos atrás
17 anos atrás
17 anos atrás
17 anos atrás
17 anos atrás
17 anos atrás
17 anos atrás
17 anos atrás
17 anos atrás
14 anos atrás
13 anos atrás
17 anos atrás
14 anos atrás
14 anos atrás
17 anos atrás
14 anos atrás
14 anos atrás
14 anos atrás
14 anos atrás
14 anos atrás
14 anos atrás
14 anos atrás
14 anos atrás
17 anos atrás
14 anos atrás
14 anos atrás
17 anos atrás
17 anos atrás
17 anos atrás
17 anos atrás
14 anos atrás
17 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
11 anos atrás
  1. %%% File : ibrowse_test.erl
  2. %%% Author : Chandrashekhar Mullaparthi <chandrashekhar.mullaparthi@t-mobile.co.uk>
  3. %%% Description : Test ibrowse
  4. %%% Created : 14 Oct 2003 by Chandrashekhar Mullaparthi <chandrashekhar.mullaparthi@t-mobile.co.uk>
  5. -module(ibrowse_test).
  6. -export([
  7. load_test/3,
  8. send_reqs_1/3,
  9. do_send_req/2,
  10. unit_tests/0,
  11. unit_tests/1,
  12. unit_tests_1/2,
  13. ue_test/0,
  14. ue_test/1,
  15. verify_chunked_streaming/0,
  16. verify_chunked_streaming/1,
  17. test_chunked_streaming_once/0,
  18. i_do_async_req_list/4,
  19. test_stream_once/3,
  20. test_stream_once/4,
  21. test_20122010/0,
  22. test_20122010/1,
  23. test_pipeline_head_timeout/0,
  24. test_pipeline_head_timeout/1,
  25. do_test_pipeline_head_timeout/4,
  26. test_head_transfer_encoding/0,
  27. test_head_transfer_encoding/1,
  28. test_head_response_with_body/0,
  29. test_head_response_with_body/1,
  30. test_303_response_with_no_body/0,
  31. test_303_response_with_no_body/1,
  32. test_303_response_with_a_body/0,
  33. test_303_response_with_a_body/1,
  34. test_generate_body_0/0
  35. ]).
  36. test_stream_once(Url, Method, Options) ->
  37. test_stream_once(Url, Method, Options, 5000).
  38. test_stream_once(Url, Method, Options, Timeout) ->
  39. case ibrowse:send_req(Url, [], Method, [], [{stream_to, {self(), once}} | Options], Timeout) of
  40. {ibrowse_req_id, Req_id} ->
  41. case ibrowse:stream_next(Req_id) of
  42. ok ->
  43. test_stream_once(Req_id);
  44. Err ->
  45. Err
  46. end;
  47. Err ->
  48. Err
  49. end.
  50. test_stream_once(Req_id) ->
  51. receive
  52. {ibrowse_async_headers, Req_id, StatCode, Headers} ->
  53. io:format("Recvd headers~n~p~n", [{ibrowse_async_headers, Req_id, StatCode, Headers}]),
  54. case ibrowse:stream_next(Req_id) of
  55. ok ->
  56. test_stream_once(Req_id);
  57. Err ->
  58. Err
  59. end;
  60. {ibrowse_async_response, Req_id, {error, Err}} ->
  61. io:format("Recvd error: ~p~n", [Err]);
  62. {ibrowse_async_response, Req_id, Body_1} ->
  63. io:format("Recvd body part: ~n~p~n", [{ibrowse_async_response, Req_id, Body_1}]),
  64. case ibrowse:stream_next(Req_id) of
  65. ok ->
  66. test_stream_once(Req_id);
  67. Err ->
  68. Err
  69. end;
  70. {ibrowse_async_response_end, Req_id} ->
  71. ok
  72. end.
  73. %% Use ibrowse:set_max_sessions/3 and ibrowse:set_max_pipeline_size/3 to
  74. %% tweak settings before running the load test. The defaults are 10 and 10.
  75. load_test(Url, NumWorkers, NumReqsPerWorker) when is_list(Url),
  76. is_integer(NumWorkers),
  77. is_integer(NumReqsPerWorker),
  78. NumWorkers > 0,
  79. NumReqsPerWorker > 0 ->
  80. proc_lib:spawn(?MODULE, send_reqs_1, [Url, NumWorkers, NumReqsPerWorker]).
  81. send_reqs_1(Url, NumWorkers, NumReqsPerWorker) ->
  82. Start_time = now(),
  83. ets:new(pid_table, [named_table, public]),
  84. ets:new(ibrowse_test_results, [named_table, public]),
  85. ets:new(ibrowse_errors, [named_table, public, ordered_set]),
  86. init_results(),
  87. process_flag(trap_exit, true),
  88. log_msg("Starting spawning of workers...~n", []),
  89. spawn_workers(Url, NumWorkers, NumReqsPerWorker),
  90. log_msg("Finished spawning workers...~n", []),
  91. do_wait(Url),
  92. End_time = now(),
  93. log_msg("All workers are done...~n", []),
  94. log_msg("ibrowse_test_results table: ~n~p~n", [ets:tab2list(ibrowse_test_results)]),
  95. log_msg("Start time: ~1000.p~n", [calendar:now_to_local_time(Start_time)]),
  96. log_msg("End time : ~1000.p~n", [calendar:now_to_local_time(End_time)]),
  97. Elapsed_time_secs = trunc(timer:now_diff(End_time, Start_time) / 1000000),
  98. log_msg("Elapsed : ~p~n", [Elapsed_time_secs]),
  99. log_msg("Reqs/sec : ~p~n", [round(trunc((NumWorkers*NumReqsPerWorker) / Elapsed_time_secs))]),
  100. dump_errors().
  101. init_results() ->
  102. ets:insert(ibrowse_test_results, {crash, 0}),
  103. ets:insert(ibrowse_test_results, {send_failed, 0}),
  104. ets:insert(ibrowse_test_results, {other_error, 0}),
  105. ets:insert(ibrowse_test_results, {success, 0}),
  106. ets:insert(ibrowse_test_results, {retry_later, 0}),
  107. ets:insert(ibrowse_test_results, {trid_mismatch, 0}),
  108. ets:insert(ibrowse_test_results, {success_no_trid, 0}),
  109. ets:insert(ibrowse_test_results, {failed, 0}),
  110. ets:insert(ibrowse_test_results, {timeout, 0}),
  111. ets:insert(ibrowse_test_results, {req_id, 0}).
  112. spawn_workers(_Url, 0, _) ->
  113. ok;
  114. spawn_workers(Url, NumWorkers, NumReqsPerWorker) ->
  115. Pid = proc_lib:spawn_link(?MODULE, do_send_req, [Url, NumReqsPerWorker]),
  116. ets:insert(pid_table, {Pid, []}),
  117. spawn_workers(Url, NumWorkers - 1, NumReqsPerWorker).
  118. do_wait(Url) ->
  119. receive
  120. {'EXIT', _, normal} ->
  121. catch ibrowse:show_dest_status(Url),
  122. catch ibrowse:show_dest_status(),
  123. do_wait(Url);
  124. {'EXIT', Pid, Reason} ->
  125. ets:delete(pid_table, Pid),
  126. ets:insert(ibrowse_errors, {Pid, Reason}),
  127. ets:update_counter(ibrowse_test_results, crash, 1),
  128. do_wait(Url);
  129. Msg ->
  130. io:format("Recvd unknown message...~p~n", [Msg]),
  131. do_wait(Url)
  132. after 1000 ->
  133. case ets:info(pid_table, size) of
  134. 0 ->
  135. done;
  136. _ ->
  137. catch ibrowse:show_dest_status(Url),
  138. catch ibrowse:show_dest_status(),
  139. do_wait(Url)
  140. end
  141. end.
  142. do_send_req(Url, NumReqs) ->
  143. do_send_req_1(Url, NumReqs).
  144. do_send_req_1(_Url, 0) ->
  145. ets:delete(pid_table, self());
  146. do_send_req_1(Url, NumReqs) ->
  147. Counter = integer_to_list(ets:update_counter(ibrowse_test_results, req_id, 1)),
  148. case ibrowse:send_req(Url, [{"ib_req_id", Counter}], get, [], [], 10000) of
  149. {ok, _Status, Headers, _Body} ->
  150. case lists:keysearch("ib_req_id", 1, Headers) of
  151. {value, {_, Counter}} ->
  152. ets:update_counter(ibrowse_test_results, success, 1);
  153. {value, _} ->
  154. ets:update_counter(ibrowse_test_results, trid_mismatch, 1);
  155. false ->
  156. ets:update_counter(ibrowse_test_results, success_no_trid, 1)
  157. end;
  158. {error, req_timedout} ->
  159. ets:update_counter(ibrowse_test_results, timeout, 1);
  160. {error, send_failed} ->
  161. ets:update_counter(ibrowse_test_results, send_failed, 1);
  162. {error, retry_later} ->
  163. ets:update_counter(ibrowse_test_results, retry_later, 1);
  164. Err ->
  165. ets:insert(ibrowse_errors, {now(), Err}),
  166. ets:update_counter(ibrowse_test_results, other_error, 1),
  167. ok
  168. end,
  169. do_send_req_1(Url, NumReqs-1).
  170. dump_errors() ->
  171. case ets:info(ibrowse_errors, size) of
  172. 0 ->
  173. ok;
  174. _ ->
  175. {A, B, C} = now(),
  176. Filename = lists:flatten(
  177. io_lib:format("ibrowse_errors_~p_~p_~p.txt" , [A, B, C])),
  178. case file:open(Filename, [write, delayed_write, raw]) of
  179. {ok, Iod} ->
  180. dump_errors(ets:first(ibrowse_errors), Iod);
  181. Err ->
  182. io:format("failed to create file ~s. Reason: ~p~n", [Filename, Err]),
  183. ok
  184. end
  185. end.
  186. dump_errors('$end_of_table', Iod) ->
  187. file:close(Iod);
  188. dump_errors(Key, Iod) ->
  189. [{_, Term}] = ets:lookup(ibrowse_errors, Key),
  190. file:write(Iod, io_lib:format("~p~n", [Term])),
  191. dump_errors(ets:next(ibrowse_errors, Key), Iod).
  192. %%------------------------------------------------------------------------------
  193. %% Unit Tests
  194. %%------------------------------------------------------------------------------
  195. -define(TEST_LIST, [{"http://intranet/messenger", get},
  196. {"http://www.google.co.uk", get},
  197. {"http://www.google.com", get},
  198. {"http://www.google.com", options},
  199. {"https://mail.google.com", get},
  200. {"http://www.sun.com", get},
  201. {"http://www.oracle.com", get},
  202. {"http://www.bbc.co.uk", get},
  203. {"http://www.bbc.co.uk", trace},
  204. {"http://www.bbc.co.uk", options},
  205. {"http://yaws.hyber.org", get},
  206. {"http://jigsaw.w3.org/HTTP/ChunkedScript", get},
  207. {"http://jigsaw.w3.org/HTTP/TE/foo.txt", get},
  208. {"http://jigsaw.w3.org/HTTP/TE/bar.txt", get},
  209. {"http://jigsaw.w3.org/HTTP/connection.html", get},
  210. {"http://jigsaw.w3.org/HTTP/cc.html", get},
  211. {"http://jigsaw.w3.org/HTTP/cc-private.html", get},
  212. {"http://jigsaw.w3.org/HTTP/cc-proxy-revalidate.html", get},
  213. {"http://jigsaw.w3.org/HTTP/cc-nocache.html", get},
  214. {"http://jigsaw.w3.org/HTTP/h-content-md5.html", get},
  215. {"http://jigsaw.w3.org/HTTP/h-retry-after.html", get},
  216. {"http://jigsaw.w3.org/HTTP/h-retry-after-date.html", get},
  217. {"http://jigsaw.w3.org/HTTP/neg", get},
  218. {"http://jigsaw.w3.org/HTTP/negbad", get},
  219. {"http://jigsaw.w3.org/HTTP/400/toolong/", get},
  220. {"http://jigsaw.w3.org/HTTP/300/", get},
  221. {"http://jigsaw.w3.org/HTTP/Basic/", get, [{basic_auth, {"guest", "guest"}}]},
  222. {"http://jigsaw.w3.org/HTTP/CL/", get},
  223. {"http://www.httpwatch.com/httpgallery/chunked/", get},
  224. {"https://github.com", get, [{ssl_options, [{depth, 2}]}]},
  225. {local_test_fun, test_20122010, []},
  226. {local_test_fun, test_pipeline_head_timeout, []},
  227. {local_test_fun, test_head_transfer_encoding, []},
  228. {local_test_fun, test_head_response_with_body, []},
  229. {local_test_fun, test_303_response_with_a_body, []}
  230. ]).
  231. unit_tests() ->
  232. unit_tests([]).
  233. unit_tests(Options) ->
  234. application:start(crypto),
  235. application:start(public_key),
  236. application:start(ssl),
  237. (catch ibrowse_test_server:start_server(8181, tcp)),
  238. ibrowse:start(),
  239. Options_1 = Options ++ [{connect_timeout, 5000}],
  240. Test_timeout = proplists:get_value(test_timeout, Options, 60000),
  241. {Pid, Ref} = erlang:spawn_monitor(?MODULE, unit_tests_1, [self(), Options_1]),
  242. receive
  243. {done, Pid} ->
  244. ok;
  245. {'DOWN', Ref, _, _, Info} ->
  246. io:format("Test process crashed: ~p~n", [Info])
  247. after Test_timeout ->
  248. exit(Pid, kill),
  249. io:format("Timed out waiting for tests to complete~n", [])
  250. end,
  251. catch ibrowse_test_server:stop_server(8181),
  252. ok.
  253. unit_tests_1(Parent, Options) ->
  254. lists:foreach(fun({local_test_fun, Fun_name, Args}) ->
  255. execute_req(local_test_fun, Fun_name, Args);
  256. ({Url, Method}) ->
  257. execute_req(Url, Method, Options);
  258. ({Url, Method, X_Opts}) ->
  259. execute_req(Url, Method, X_Opts ++ Options)
  260. end, ?TEST_LIST),
  261. Parent ! {done, self()}.
  262. verify_chunked_streaming() ->
  263. verify_chunked_streaming([]).
  264. verify_chunked_streaming(Options) ->
  265. io:format("~nVerifying that chunked streaming is working...~n", []),
  266. Url = "http://www.httpwatch.com/httpgallery/chunked/",
  267. io:format(" URL: ~s~n", [Url]),
  268. io:format(" Fetching data without streaming...~n", []),
  269. Result_without_streaming = ibrowse:send_req(
  270. Url, [], get, [],
  271. [{response_format, binary} | Options]),
  272. io:format(" Fetching data with streaming as list...~n", []),
  273. Async_response_list = do_async_req_list(
  274. Url, get, [{response_format, list} | Options]),
  275. io:format(" Fetching data with streaming as binary...~n", []),
  276. Async_response_bin = do_async_req_list(
  277. Url, get, [{response_format, binary} | Options]),
  278. io:format(" Fetching data with streaming as binary, {active, once}...~n", []),
  279. Async_response_bin_once = do_async_req_list(
  280. Url, get, [once, {response_format, binary} | Options]),
  281. Res1 = compare_responses(Result_without_streaming, Async_response_list, Async_response_bin),
  282. Res2 = compare_responses(Result_without_streaming, Async_response_list, Async_response_bin_once),
  283. case {Res1, Res2} of
  284. {success, success} ->
  285. io:format(" Chunked streaming working~n", []);
  286. _ ->
  287. ok
  288. end.
  289. test_chunked_streaming_once() ->
  290. test_chunked_streaming_once([]).
  291. test_chunked_streaming_once(Options) ->
  292. io:format("~nTesting chunked streaming with the {stream_to, {Pid, once}} option...~n", []),
  293. Url = "http://www.httpwatch.com/httpgallery/chunked/",
  294. io:format(" URL: ~s~n", [Url]),
  295. io:format(" Fetching data with streaming as binary, {active, once}...~n", []),
  296. case do_async_req_list(Url, get, [once, {response_format, binary} | Options]) of
  297. {ok, _, _, _} ->
  298. io:format(" Success!~n", []);
  299. Err ->
  300. io:format(" Fail: ~p~n", [Err])
  301. end.
  302. compare_responses({ok, St_code, _, Body}, {ok, St_code, _, Body}, {ok, St_code, _, Body}) ->
  303. success;
  304. compare_responses({ok, St_code, _, Body_1}, {ok, St_code, _, Body_2}, {ok, St_code, _, Body_3}) ->
  305. case Body_1 of
  306. Body_2 ->
  307. io:format("Body_1 and Body_2 match~n", []);
  308. Body_3 ->
  309. io:format("Body_1 and Body_3 match~n", []);
  310. _ when Body_2 == Body_3 ->
  311. io:format("Body_2 and Body_3 match~n", []);
  312. _ ->
  313. io:format("All three bodies are different!~n", [])
  314. end,
  315. io:format("Body_1 -> ~p~n", [Body_1]),
  316. io:format("Body_2 -> ~p~n", [Body_2]),
  317. io:format("Body_3 -> ~p~n", [Body_3]),
  318. fail_bodies_mismatch;
  319. compare_responses(R1, R2, R3) ->
  320. io:format("R1 -> ~p~n", [R1]),
  321. io:format("R2 -> ~p~n", [R2]),
  322. io:format("R3 -> ~p~n", [R3]),
  323. fail.
  324. %% do_async_req_list(Url) ->
  325. %% do_async_req_list(Url, get).
  326. %% do_async_req_list(Url, Method) ->
  327. %% do_async_req_list(Url, Method, [{stream_to, self()},
  328. %% {stream_chunk_size, 1000}]).
  329. do_async_req_list(Url, Method, Options) ->
  330. {Pid,_} = erlang:spawn_monitor(?MODULE, i_do_async_req_list,
  331. [self(), Url, Method,
  332. Options ++ [{stream_chunk_size, 1000}]]),
  333. %% io:format("Spawned process ~p~n", [Pid]),
  334. wait_for_resp(Pid).
  335. wait_for_resp(Pid) ->
  336. receive
  337. {async_result, Pid, Res} ->
  338. Res;
  339. {async_result, Other_pid, _} ->
  340. io:format("~p: Waiting for result from ~p: got from ~p~n", [self(), Pid, Other_pid]),
  341. wait_for_resp(Pid);
  342. {'DOWN', _, _, Pid, Reason} ->
  343. {'EXIT', Reason};
  344. {'DOWN', _, _, _, _} ->
  345. wait_for_resp(Pid);
  346. Msg ->
  347. io:format("Recvd unknown message: ~p~n", [Msg]),
  348. wait_for_resp(Pid)
  349. after 100000 ->
  350. {error, timeout}
  351. end.
  352. i_do_async_req_list(Parent, Url, Method, Options) ->
  353. Options_1 = case lists:member(once, Options) of
  354. true ->
  355. [{stream_to, {self(), once}} | (Options -- [once])];
  356. false ->
  357. [{stream_to, self()} | Options]
  358. end,
  359. Res = ibrowse:send_req(Url, [], Method, [], Options_1),
  360. case Res of
  361. {ibrowse_req_id, Req_id} ->
  362. Result = wait_for_async_resp(Req_id, Options, undefined, undefined, []),
  363. Parent ! {async_result, self(), Result};
  364. Err ->
  365. Parent ! {async_result, self(), Err}
  366. end.
  367. wait_for_async_resp(Req_id, Options, Acc_Stat_code, Acc_Headers, Body) ->
  368. receive
  369. {ibrowse_async_headers, Req_id, StatCode, Headers} ->
  370. %% io:format("Recvd headers...~n", []),
  371. maybe_stream_next(Req_id, Options),
  372. wait_for_async_resp(Req_id, Options, StatCode, Headers, Body);
  373. {ibrowse_async_response_end, Req_id} ->
  374. %% io:format("Recvd end of response.~n", []),
  375. Body_1 = list_to_binary(lists:reverse(Body)),
  376. {ok, Acc_Stat_code, Acc_Headers, Body_1};
  377. {ibrowse_async_response, Req_id, Data} ->
  378. maybe_stream_next(Req_id, Options),
  379. %% io:format("Recvd data...~n", []),
  380. wait_for_async_resp(Req_id, Options, Acc_Stat_code, Acc_Headers, [Data | Body]);
  381. {ibrowse_async_response, Req_id, {error, _} = Err} ->
  382. {ok, Acc_Stat_code, Acc_Headers, Err};
  383. Err ->
  384. {ok, Acc_Stat_code, Acc_Headers, Err}
  385. after 10000 ->
  386. {timeout, Acc_Stat_code, Acc_Headers, Body}
  387. end.
  388. maybe_stream_next(Req_id, Options) ->
  389. case lists:member(once, Options) of
  390. true ->
  391. ibrowse:stream_next(Req_id);
  392. false ->
  393. ok
  394. end.
  395. execute_req(local_test_fun, Method, Args) ->
  396. io:format(" ~-54.54w: ", [Method]),
  397. Result = (catch apply(?MODULE, Method, Args)),
  398. io:format("~p~n", [Result]);
  399. execute_req(Url, Method, Options) ->
  400. io:format("~7.7w, ~50.50s: ", [Method, Url]),
  401. Result = (catch ibrowse:send_req(Url, [], Method, [], Options)),
  402. case Result of
  403. {ok, SCode, _H, _B} ->
  404. io:format("Status code: ~p~n", [SCode]);
  405. Err ->
  406. io:format("~p~n", [Err])
  407. end.
  408. ue_test() ->
  409. ue_test(lists:duplicate(1024, $?)).
  410. ue_test(Data) ->
  411. {Time, Res} = timer:tc(ibrowse_lib, url_encode, [Data]),
  412. io:format("Time -> ~p~n", [Time]),
  413. io:format("Data Length -> ~p~n", [length(Data)]),
  414. io:format("Res Length -> ~p~n", [length(Res)]).
  415. % io:format("Result -> ~s~n", [Res]).
  416. log_msg(Fmt, Args) ->
  417. io:format("~s -- " ++ Fmt,
  418. [ibrowse_lib:printable_date() | Args]).
  419. %%------------------------------------------------------------------------------
  420. %% Test what happens when the response to a HEAD request is a
  421. %% Chunked-Encoding response with a non-empty body. Issue #67 on
  422. %% Github
  423. %% ------------------------------------------------------------------------------
  424. test_head_transfer_encoding() ->
  425. clear_msg_q(),
  426. test_head_transfer_encoding("http://localhost:8181/ibrowse_head_test").
  427. test_head_transfer_encoding(Url) ->
  428. case ibrowse:send_req(Url, [], head) of
  429. {ok, "200", _, _} ->
  430. success;
  431. Res ->
  432. {test_failed, Res}
  433. end.
  434. %%------------------------------------------------------------------------------
  435. %% Test what happens when the response to a HEAD request is a
  436. %% Chunked-Encoding response with a non-empty body. Issue #67 on
  437. %% Github
  438. %% ------------------------------------------------------------------------------
  439. test_head_response_with_body() ->
  440. clear_msg_q(),
  441. test_head_response_with_body("http://localhost:8181/ibrowse_head_transfer_enc").
  442. test_head_response_with_body(Url) ->
  443. case ibrowse:send_req(Url, [], head, [], [{workaround, head_response_with_body}]) of
  444. {ok, "400", _, _} ->
  445. success;
  446. Res ->
  447. {test_failed, Res}
  448. end.
  449. %%------------------------------------------------------------------------------
  450. %% Test what happens when a 303 response has no body
  451. %% Github issue #97
  452. %% ------------------------------------------------------------------------------
  453. test_303_response_with_no_body() ->
  454. clear_msg_q(),
  455. test_303_response_with_no_body("http://localhost:8181/ibrowse_303_no_body_test").
  456. test_303_response_with_no_body(Url) ->
  457. ibrowse:add_config([{allow_303_with_no_body, true}]),
  458. case ibrowse:send_req(Url, [], post) of
  459. {ok, "303", _, _} ->
  460. success;
  461. Res ->
  462. {test_failed, Res}
  463. end.
  464. %% Make sure we don't break requests that do have a body.
  465. test_303_response_with_a_body() ->
  466. clear_msg_q(),
  467. test_303_response_with_no_body("http://localhost:8181/ibrowse_303_with_body_test").
  468. test_303_response_with_a_body(Url) ->
  469. ibrowse:add_config([{allow_303_with_no_body, true}]),
  470. case ibrowse:send_req(Url, [], post) of
  471. {ok, "303", _, "abcde"} ->
  472. success;
  473. Res ->
  474. {test_failed, Res}
  475. end.
  476. %%------------------------------------------------------------------------------
  477. %% Test what happens when the request at the head of a pipeline times out
  478. %%------------------------------------------------------------------------------
  479. test_pipeline_head_timeout() ->
  480. clear_msg_q(),
  481. test_pipeline_head_timeout("http://localhost:8181/ibrowse_inac_timeout_test").
  482. test_pipeline_head_timeout(Url) ->
  483. {ok, Pid} = ibrowse:spawn_worker_process(Url),
  484. Test_parent = self(),
  485. Fun = fun({fixed, Timeout}) ->
  486. spawn(fun() ->
  487. do_test_pipeline_head_timeout(Url, Pid, Test_parent, Timeout)
  488. end);
  489. (Timeout_mult) ->
  490. spawn(fun() ->
  491. Timeout = 1000 + Timeout_mult*1000,
  492. do_test_pipeline_head_timeout(Url, Pid, Test_parent, Timeout)
  493. end)
  494. end,
  495. Pids = [Fun(X) || X <- [{fixed, 32000} | lists:seq(1,10)]],
  496. Result = accumulate_worker_resp(Pids),
  497. case lists:all(fun({_, X_res}) ->
  498. X_res == {error,req_timedout}
  499. end, Result) of
  500. true ->
  501. success;
  502. false ->
  503. {test_failed, Result}
  504. end.
  505. do_test_pipeline_head_timeout(Url, Pid, Test_parent, Req_timeout) ->
  506. Resp = ibrowse:send_req_direct(
  507. Pid,
  508. Url,
  509. [], get, [],
  510. [{socket_options,[{keepalive,true}]},
  511. {inactivity_timeout,180000},
  512. {connect_timeout,180000}], Req_timeout),
  513. Test_parent ! {self(), Resp}.
  514. accumulate_worker_resp(Pids) ->
  515. accumulate_worker_resp(Pids, []).
  516. accumulate_worker_resp([_ | _] = Pids, Acc) ->
  517. receive
  518. {Pid, Res} when is_pid(Pid) ->
  519. accumulate_worker_resp(Pids -- [Pid], [{Pid, Res} | Acc]);
  520. Err ->
  521. io:format("Received unexpected: ~p~n", [Err])
  522. end;
  523. accumulate_worker_resp([], Acc) ->
  524. lists:reverse(Acc).
  525. clear_msg_q() ->
  526. receive
  527. _ ->
  528. clear_msg_q()
  529. after 0 ->
  530. ok
  531. end.
  532. %%------------------------------------------------------------------------------
  533. %%
  534. %%------------------------------------------------------------------------------
  535. test_20122010() ->
  536. test_20122010("http://localhost:8181").
  537. test_20122010(Url) ->
  538. {ok, Pid} = ibrowse:spawn_worker_process(Url),
  539. Expected_resp = <<"1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22-23-24-25-26-27-28-29-30-31-32-33-34-35-36-37-38-39-40-41-42-43-44-45-46-47-48-49-50-51-52-53-54-55-56-57-58-59-60-61-62-63-64-65-66-67-68-69-70-71-72-73-74-75-76-77-78-79-80-81-82-83-84-85-86-87-88-89-90-91-92-93-94-95-96-97-98-99-100">>,
  540. Test_parent = self(),
  541. Fun = fun() ->
  542. do_test_20122010(Url, Pid, Expected_resp, Test_parent)
  543. end,
  544. Pids = [erlang:spawn_monitor(Fun) || _ <- lists:seq(1,10)],
  545. wait_for_workers(Pids).
  546. wait_for_workers([{Pid, _Ref} | Pids]) ->
  547. receive
  548. {Pid, success} ->
  549. wait_for_workers(Pids)
  550. after 60000 ->
  551. test_failed
  552. end;
  553. wait_for_workers([]) ->
  554. success.
  555. do_test_20122010(Url, Pid, Expected_resp, Test_parent) ->
  556. do_test_20122010(10, Url, Pid, Expected_resp, Test_parent).
  557. do_test_20122010(0, _Url, _Pid, _Expected_resp, Test_parent) ->
  558. Test_parent ! {self(), success};
  559. do_test_20122010(Rem_count, Url, Pid, Expected_resp, Test_parent) ->
  560. {ibrowse_req_id, Req_id} = ibrowse:send_req_direct(
  561. Pid,
  562. Url ++ "/ibrowse_stream_once_chunk_pipeline_test",
  563. [], get, [],
  564. [{stream_to, {self(), once}},
  565. {inactivity_timeout, 10000},
  566. {include_ibrowse_req_id, true}]),
  567. do_trace("~p -- sent request ~1000.p~n", [self(), Req_id]),
  568. Req_id_str = lists:flatten(io_lib:format("~1000.p",[Req_id])),
  569. receive
  570. {ibrowse_async_headers, Req_id, "200", Headers} ->
  571. case lists:keysearch("x-ibrowse-request-id", 1, Headers) of
  572. {value, {_, Req_id_str}} ->
  573. ok;
  574. {value, {_, Req_id_1}} ->
  575. do_trace("~p -- Sent req-id: ~1000.p. Recvd: ~1000.p~n",
  576. [self(), Req_id, Req_id_1]),
  577. exit(req_id_mismatch)
  578. end
  579. after 5000 ->
  580. do_trace("~p -- response headers not received~n", [self()]),
  581. exit({timeout, test_failed})
  582. end,
  583. do_trace("~p -- response headers received~n", [self()]),
  584. ok = ibrowse:stream_next(Req_id),
  585. case do_test_20122010_1(Expected_resp, Req_id, []) of
  586. true ->
  587. do_test_20122010(Rem_count - 1, Url, Pid, Expected_resp, Test_parent);
  588. false ->
  589. Test_parent ! {self(), failed}
  590. end.
  591. do_test_20122010_1(Expected_resp, Req_id, Acc) ->
  592. receive
  593. {ibrowse_async_response, Req_id, Body_part} ->
  594. ok = ibrowse:stream_next(Req_id),
  595. do_test_20122010_1(Expected_resp, Req_id, [Body_part | Acc]);
  596. {ibrowse_async_response_end, Req_id} ->
  597. Acc_1 = list_to_binary(lists:reverse(Acc)),
  598. Result = Acc_1 == Expected_resp,
  599. do_trace("~p -- End of response. Result: ~p~n", [self(), Result]),
  600. Result
  601. after 1000 ->
  602. exit({timeout, test_failed})
  603. end.
  604. %%------------------------------------------------------------------------------
  605. %% Test requests where body is generated using a Fun
  606. %%------------------------------------------------------------------------------
  607. test_generate_body_0() ->
  608. io:format("Testing that generation of body using fun works...~n", []),
  609. Tid = ets:new(ibrowse_test_state, [public]),
  610. try
  611. Body_1 = <<"Part 1 of the body">>,
  612. Body_2 = <<"Part 2 of the body\r\n\r\n">>,
  613. Size = size(Body_1) + size(Body_2),
  614. Body = list_to_binary([Body_1, Body_2]),
  615. Fun = fun() ->
  616. case ets:lookup(Tid, body_gen_state) of
  617. [] ->
  618. ets:insert(Tid, {body_gen_state, 1}),
  619. {ok, Body_1};
  620. [{_, 1}]->
  621. ets:insert(Tid, {body_gen_state, 2}),
  622. {ok, Body_2};
  623. [{_, 2}] ->
  624. eof
  625. end
  626. end,
  627. case ibrowse:send_req("http://localhost:8181/echo_body",
  628. [{"Content-Length", Size}],
  629. post,
  630. Fun,
  631. [{response_format, binary},
  632. {http_vsn, {1,0}}]) of
  633. {ok, "200", _, Body} ->
  634. io:format(" Success~n", []),
  635. success;
  636. Err ->
  637. io:format("Test failed : ~p~n", [Err]),
  638. {test_failed, Err}
  639. end
  640. after
  641. ets:delete(Tid)
  642. end.
  643. do_trace(Fmt, Args) ->
  644. do_trace(get(my_trace_flag), Fmt, Args).
  645. do_trace(true, Fmt, Args) ->
  646. io:format("~s -- " ++ Fmt, [ibrowse_lib:printable_date() | Args]);
  647. do_trace(_, _, _) ->
  648. ok.