You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

900 lines
33 KiB

13 years ago
11 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
14 years ago
14 years ago
9 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
9 years ago
14 years ago
14 years ago
14 years ago
9 years ago
14 years ago
14 years ago
14 years ago
9 years ago
14 years ago
13 years ago
13 years ago
13 years ago
9 years ago
9 years ago
9 years ago
9 years ago
11 years ago
9 years ago
11 years ago
9 years ago
11 years ago
  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. local_unit_tests/0,
  11. unit_tests/0,
  12. unit_tests/2,
  13. unit_tests_1/3,
  14. ue_test/0,
  15. ue_test/1,
  16. verify_chunked_streaming/0,
  17. verify_chunked_streaming/1,
  18. test_chunked_streaming_once/0,
  19. i_do_async_req_list/4,
  20. test_stream_once/3,
  21. test_stream_once/4,
  22. test_20122010/0,
  23. test_20122010/1,
  24. test_pipeline_head_timeout/0,
  25. test_pipeline_head_timeout/1,
  26. do_test_pipeline_head_timeout/4,
  27. test_head_transfer_encoding/0,
  28. test_head_transfer_encoding/1,
  29. test_head_response_with_body/0,
  30. test_head_response_with_body/1,
  31. test_303_response_with_no_body/0,
  32. test_303_response_with_no_body/1,
  33. test_303_response_with_a_body/0,
  34. test_303_response_with_a_body/1,
  35. test_preserve_status_line/0,
  36. test_binary_headers/0,
  37. test_binary_headers/1,
  38. test_generate_body_0/0,
  39. test_retry_of_requests/0,
  40. test_retry_of_requests/1,
  41. test_save_to_file_no_content_length/0,
  42. socks5_noauth_test/0,
  43. socks5_auth_succ_test/0,
  44. socks5_auth_fail_test/0
  45. ]).
  46. -include_lib("ibrowse/include/ibrowse.hrl").
  47. %%------------------------------------------------------------------------------
  48. %% Unit Tests
  49. %%------------------------------------------------------------------------------
  50. -define(LOCAL_TESTS, [
  51. {local_test_fun, socks5_noauth_test, []},
  52. {local_test_fun, socks5_auth_succ_test, []},
  53. {local_test_fun, socks5_auth_fail_test, []},
  54. {local_test_fun, test_preserve_status_line, []},
  55. {local_test_fun, test_save_to_file_no_content_length, []},
  56. {local_test_fun, test_20122010, []},
  57. {local_test_fun, test_pipeline_head_timeout, []},
  58. {local_test_fun, test_head_transfer_encoding, []},
  59. {local_test_fun, test_head_response_with_body, []},
  60. {local_test_fun, test_303_response_with_a_body, []},
  61. {local_test_fun, test_303_response_with_no_body, []},
  62. {local_test_fun, test_binary_headers, []},
  63. {local_test_fun, test_retry_of_requests, []},
  64. {local_test_fun, verify_chunked_streaming, []},
  65. {local_test_fun, test_chunked_streaming_once, []},
  66. {local_test_fun, test_generate_body_0, []}
  67. ]).
  68. -define(TEST_LIST, [{"http://intranet/messenger", get},
  69. {"http://www.google.co.uk", get},
  70. {"http://www.google.com", get},
  71. {"http://www.google.com", options},
  72. {"https://mail.google.com", get},
  73. {"http://www.sun.com", get},
  74. {"http://www.oracle.com", get},
  75. {"http://www.bbc.co.uk", get},
  76. {"http://www.bbc.co.uk", trace},
  77. {"http://www.bbc.co.uk", options},
  78. {"http://yaws.hyber.org", get},
  79. {"http://jigsaw.w3.org/HTTP/ChunkedScript", get},
  80. {"http://jigsaw.w3.org/HTTP/TE/foo.txt", get},
  81. {"http://jigsaw.w3.org/HTTP/TE/bar.txt", get},
  82. {"http://jigsaw.w3.org/HTTP/connection.html", get},
  83. {"http://jigsaw.w3.org/HTTP/cc.html", get},
  84. {"http://jigsaw.w3.org/HTTP/cc-private.html", get},
  85. {"http://jigsaw.w3.org/HTTP/cc-proxy-revalidate.html", get},
  86. {"http://jigsaw.w3.org/HTTP/cc-nocache.html", get},
  87. {"http://jigsaw.w3.org/HTTP/h-content-md5.html", get},
  88. {"http://jigsaw.w3.org/HTTP/h-retry-after.html", get},
  89. {"http://jigsaw.w3.org/HTTP/h-retry-after-date.html", get},
  90. {"http://jigsaw.w3.org/HTTP/neg", get},
  91. {"http://jigsaw.w3.org/HTTP/negbad", get},
  92. {"http://jigsaw.w3.org/HTTP/400/toolong/", get},
  93. {"http://jigsaw.w3.org/HTTP/300/", get},
  94. {"http://jigsaw.w3.org/HTTP/Basic/", get, [{basic_auth, {"guest", "guest"}}]},
  95. {"http://jigsaw.w3.org/HTTP/CL/", get},
  96. {"http://www.httpwatch.com/httpgallery/chunked/", get},
  97. {"https://github.com", get, [{ssl_options, [{depth, 2}]}]}
  98. ]).
  99. socks5_noauth_test() ->
  100. case ibrowse:send_req("http://localhost:8181/success", [], get, [],
  101. [{socks5_host, "localhost"}, {socks5_port, 8282}], 2000) of
  102. {ok, "200", _, _} ->
  103. success;
  104. Err ->
  105. Err
  106. end.
  107. socks5_auth_succ_test() ->
  108. case ibrowse:send_req("http://localhost:8181/success", [], get, [],
  109. [{socks5_host, "localhost"}, {socks5_port, 8383},
  110. {socks5_user, <<"user">>}, {socks5_password, <<"password">>}], 2000) of
  111. {ok, "200", _, _} ->
  112. success;
  113. Err ->
  114. Err
  115. end.
  116. socks5_auth_fail_test() ->
  117. case ibrowse:send_req("http://localhost:8181/success", [], get, [],
  118. [{socks5_host, "localhost"}, {socks5_port, 8282},
  119. {socks5_user, <<"user">>}, {socks5_password, <<"wrong_password">>}], 2000) of
  120. {error,{conn_failed,{error,unacceptable}}} ->
  121. success;
  122. Err ->
  123. Err
  124. end.
  125. test_stream_once(Url, Method, Options) ->
  126. test_stream_once(Url, Method, Options, 5000).
  127. test_stream_once(Url, Method, Options, Timeout) ->
  128. case ibrowse:send_req(Url, [], Method, [], [{stream_to, {self(), once}} | Options], Timeout) of
  129. {ibrowse_req_id, Req_id} ->
  130. case ibrowse:stream_next(Req_id) of
  131. ok ->
  132. test_stream_once(Req_id);
  133. Err ->
  134. Err
  135. end;
  136. Err ->
  137. Err
  138. end.
  139. test_stream_once(Req_id) ->
  140. receive
  141. {ibrowse_async_headers, Req_id, StatCode, Headers} ->
  142. io:format("Recvd headers~n~p~n", [{ibrowse_async_headers, Req_id, StatCode, Headers}]),
  143. case ibrowse:stream_next(Req_id) of
  144. ok ->
  145. test_stream_once(Req_id);
  146. Err ->
  147. Err
  148. end;
  149. {ibrowse_async_response, Req_id, {error, Err}} ->
  150. io:format("Recvd error: ~p~n", [Err]);
  151. {ibrowse_async_response, Req_id, Body_1} ->
  152. io:format("Recvd body part: ~n~p~n", [{ibrowse_async_response, Req_id, Body_1}]),
  153. case ibrowse:stream_next(Req_id) of
  154. ok ->
  155. test_stream_once(Req_id);
  156. Err ->
  157. Err
  158. end;
  159. {ibrowse_async_response_end, Req_id} ->
  160. ok
  161. end.
  162. %% Use ibrowse:set_max_sessions/3 and ibrowse:set_max_pipeline_size/3 to
  163. %% tweak settings before running the load test. The defaults are 10 and 10.
  164. load_test(Url, NumWorkers, NumReqsPerWorker) when is_list(Url),
  165. is_integer(NumWorkers),
  166. is_integer(NumReqsPerWorker),
  167. NumWorkers > 0,
  168. NumReqsPerWorker > 0 ->
  169. proc_lib:spawn(?MODULE, send_reqs_1, [Url, NumWorkers, NumReqsPerWorker]).
  170. send_reqs_1(Url, NumWorkers, NumReqsPerWorker) ->
  171. Start_time = os:timestamp(),
  172. ets:new(pid_table, [named_table, public]),
  173. ets:new(ibrowse_test_results, [named_table, public]),
  174. ets:new(ibrowse_errors, [named_table, public, ordered_set]),
  175. ets:new(ibrowse_counter, [named_table, public, ordered_set]),
  176. ets:insert(ibrowse_counter, {req_id, 1}),
  177. init_results(),
  178. process_flag(trap_exit, true),
  179. log_msg("Starting spawning of workers...~n", []),
  180. spawn_workers(Url, NumWorkers, NumReqsPerWorker),
  181. log_msg("Finished spawning workers...~n", []),
  182. do_wait(Url),
  183. End_time = os:timestamp(),
  184. log_msg("All workers are done...~n", []),
  185. log_msg("ibrowse_test_results table: ~n~p~n", [ets:tab2list(ibrowse_test_results)]),
  186. log_msg("Start time: ~1000.p~n", [calendar:now_to_local_time(Start_time)]),
  187. log_msg("End time : ~1000.p~n", [calendar:now_to_local_time(End_time)]),
  188. Elapsed_time_secs = trunc(timer:now_diff(End_time, Start_time) / 1000000),
  189. log_msg("Elapsed : ~p~n", [Elapsed_time_secs]),
  190. log_msg("Reqs/sec : ~p~n", [round(trunc((NumWorkers*NumReqsPerWorker) / Elapsed_time_secs))]),
  191. dump_errors().
  192. init_results() ->
  193. ets:insert(ibrowse_test_results, {crash, 0}),
  194. ets:insert(ibrowse_test_results, {send_failed, 0}),
  195. ets:insert(ibrowse_test_results, {other_error, 0}),
  196. ets:insert(ibrowse_test_results, {success, 0}),
  197. ets:insert(ibrowse_test_results, {retry_later, 0}),
  198. ets:insert(ibrowse_test_results, {trid_mismatch, 0}),
  199. ets:insert(ibrowse_test_results, {success_no_trid, 0}),
  200. ets:insert(ibrowse_test_results, {failed, 0}),
  201. ets:insert(ibrowse_test_results, {timeout, 0}),
  202. ets:insert(ibrowse_test_results, {req_id, 0}).
  203. spawn_workers(_Url, 0, _) ->
  204. ok;
  205. spawn_workers(Url, NumWorkers, NumReqsPerWorker) ->
  206. Pid = proc_lib:spawn_link(?MODULE, do_send_req, [Url, NumReqsPerWorker]),
  207. ets:insert(pid_table, {Pid, []}),
  208. spawn_workers(Url, NumWorkers - 1, NumReqsPerWorker).
  209. do_wait(Url) ->
  210. receive
  211. {'EXIT', _, normal} ->
  212. catch ibrowse:show_dest_status(Url),
  213. catch ibrowse:show_dest_status(),
  214. do_wait(Url);
  215. {'EXIT', Pid, Reason} ->
  216. ets:delete(pid_table, Pid),
  217. ets:insert(ibrowse_errors, {Pid, Reason}),
  218. ets:update_counter(ibrowse_test_results, crash, 1),
  219. do_wait(Url);
  220. Msg ->
  221. io:format("Recvd unknown message...~p~n", [Msg]),
  222. do_wait(Url)
  223. after 1000 ->
  224. case ets:info(pid_table, size) of
  225. 0 ->
  226. done;
  227. _ ->
  228. catch ibrowse:show_dest_status(Url),
  229. catch ibrowse:show_dest_status(),
  230. do_wait(Url)
  231. end
  232. end.
  233. do_send_req(Url, NumReqs) ->
  234. do_send_req_1(Url, NumReqs).
  235. do_send_req_1(_Url, 0) ->
  236. ets:delete(pid_table, self());
  237. do_send_req_1(Url, NumReqs) ->
  238. Counter = integer_to_list(ets:update_counter(ibrowse_test_results, req_id, 1)),
  239. case ibrowse:send_req(Url, [{"ib_req_id", Counter}], get, [], [], 10000) of
  240. {ok, _Status, Headers, _Body} ->
  241. case lists:keysearch("ib_req_id", 1, Headers) of
  242. {value, {_, Counter}} ->
  243. ets:update_counter(ibrowse_test_results, success, 1);
  244. {value, _} ->
  245. ets:update_counter(ibrowse_test_results, trid_mismatch, 1);
  246. false ->
  247. ets:update_counter(ibrowse_test_results, success_no_trid, 1)
  248. end;
  249. {error, req_timedout} ->
  250. ets:update_counter(ibrowse_test_results, timeout, 1);
  251. {error, send_failed} ->
  252. ets:update_counter(ibrowse_test_results, send_failed, 1);
  253. {error, retry_later} ->
  254. ets:update_counter(ibrowse_test_results, retry_later, 1);
  255. Err ->
  256. ets:insert(ibrowse_errors, {os:timestamp(), Err}),
  257. ets:update_counter(ibrowse_test_results, other_error, 1),
  258. ok
  259. end,
  260. do_send_req_1(Url, NumReqs-1).
  261. dump_errors() ->
  262. case ets:info(ibrowse_errors, size) of
  263. 0 ->
  264. ok;
  265. _ ->
  266. {A, B, C} = os:timestamp(),
  267. Filename = lists:flatten(
  268. io_lib:format("ibrowse_errors_~p_~p_~p.txt" , [A, B, C])),
  269. case file:open(Filename, [write, delayed_write, raw]) of
  270. {ok, Iod} ->
  271. dump_errors(ets:first(ibrowse_errors), Iod);
  272. Err ->
  273. io:format("failed to create file ~s. Reason: ~p~n", [Filename, Err]),
  274. ok
  275. end
  276. end.
  277. dump_errors('$end_of_table', Iod) ->
  278. file:close(Iod);
  279. dump_errors(Key, Iod) ->
  280. [{_, Term}] = ets:lookup(ibrowse_errors, Key),
  281. file:write(Iod, io_lib:format("~p~n", [Term])),
  282. dump_errors(ets:next(ibrowse_errors, Key), Iod).
  283. local_unit_tests() ->
  284. unit_tests([], ?LOCAL_TESTS).
  285. unit_tests() ->
  286. unit_tests([], ?TEST_LIST).
  287. unit_tests(Options, Test_list) ->
  288. error_logger:tty(false),
  289. application:start(crypto),
  290. application:start(asn1),
  291. application:start(public_key),
  292. application:start(ssl),
  293. (catch ibrowse_test_server:start_server(8181, tcp)),
  294. application:start(ibrowse),
  295. Options_1 = Options ++ [{connect_timeout, 5000}],
  296. Test_timeout = proplists:get_value(test_timeout, Options, 60000),
  297. {Pid, Ref} = erlang:spawn_monitor(?MODULE, unit_tests_1, [self(), Options_1, Test_list]),
  298. receive
  299. {done, Pid} ->
  300. ok;
  301. {'DOWN', Ref, _, _, Info} ->
  302. io:format("Test process crashed: ~p~n", [Info])
  303. after Test_timeout ->
  304. exit(Pid, kill),
  305. io:format("Timed out waiting for tests to complete~n", [])
  306. end,
  307. catch ibrowse_test_server:stop_server(8181),
  308. error_logger:tty(true),
  309. ok.
  310. unit_tests_1(Parent, Options, Test_list) ->
  311. lists:foreach(fun({local_test_fun, Fun_name, Args}) ->
  312. execute_req(local_test_fun, Fun_name, Args);
  313. ({Url, Method}) ->
  314. execute_req(Url, Method, Options);
  315. ({Url, Method, X_Opts}) ->
  316. execute_req(Url, Method, X_Opts ++ Options)
  317. end, Test_list),
  318. Parent ! {done, self()}.
  319. verify_chunked_streaming() ->
  320. verify_chunked_streaming([]).
  321. verify_chunked_streaming(Options) ->
  322. io:format("~nVerifying that chunked streaming is working...~n", []),
  323. Url = "http://www.httpwatch.com/httpgallery/chunked/",
  324. io:format(" URL: ~s~n", [Url]),
  325. io:format(" Fetching data without streaming...~n", []),
  326. Result_without_streaming = ibrowse:send_req(
  327. Url, [], get, [],
  328. [{response_format, binary} | Options]),
  329. io:format(" Fetching data with streaming as list...~n", []),
  330. Async_response_list = do_async_req_list(
  331. Url, get, [{response_format, list} | Options]),
  332. io:format(" Fetching data with streaming as binary...~n", []),
  333. Async_response_bin = do_async_req_list(
  334. Url, get, [{response_format, binary} | Options]),
  335. io:format(" Fetching data with streaming as binary, {active, once}...~n", []),
  336. Async_response_bin_once = do_async_req_list(
  337. Url, get, [once, {response_format, binary} | Options]),
  338. Res1 = compare_responses(Result_without_streaming, Async_response_list, Async_response_bin),
  339. Res2 = compare_responses(Result_without_streaming, Async_response_list, Async_response_bin_once),
  340. case {Res1, Res2} of
  341. {success, success} ->
  342. io:format(" Chunked streaming working~n", []),
  343. success;
  344. _ ->
  345. ok
  346. end.
  347. test_chunked_streaming_once() ->
  348. test_chunked_streaming_once([]).
  349. test_chunked_streaming_once(Options) ->
  350. io:format("~nTesting chunked streaming with the {stream_to, {Pid, once}} option...~n", []),
  351. Url = "http://www.httpwatch.com/httpgallery/chunked/",
  352. io:format(" URL: ~s~n", [Url]),
  353. io:format(" Fetching data with streaming as binary, {active, once}...~n", []),
  354. case do_async_req_list(Url, get, [once, {response_format, binary} | Options]) of
  355. {ok, _, _, _} ->
  356. success;
  357. Err ->
  358. io:format(" Fail: ~p~n", [Err])
  359. end.
  360. compare_responses({ok, St_code, _, Body}, {ok, St_code, _, Body}, {ok, St_code, _, Body}) ->
  361. success;
  362. compare_responses({ok, St_code, _, Body_1}, {ok, St_code, _, Body_2}, {ok, St_code, _, Body_3}) ->
  363. case Body_1 of
  364. Body_2 ->
  365. io:format("Body_1 and Body_2 match~n", []);
  366. Body_3 ->
  367. io:format("Body_1 and Body_3 match~n", []);
  368. _ when Body_2 == Body_3 ->
  369. io:format("Body_2 and Body_3 match~n", []);
  370. _ ->
  371. io:format("All three bodies are different!~n", [])
  372. end,
  373. io:format("Body_1 -> ~p~n", [Body_1]),
  374. io:format("Body_2 -> ~p~n", [Body_2]),
  375. io:format("Body_3 -> ~p~n", [Body_3]),
  376. fail_bodies_mismatch;
  377. compare_responses(R1, R2, R3) ->
  378. io:format("R1 -> ~p~n", [R1]),
  379. io:format("R2 -> ~p~n", [R2]),
  380. io:format("R3 -> ~p~n", [R3]),
  381. fail.
  382. %% do_async_req_list(Url) ->
  383. %% do_async_req_list(Url, get).
  384. %% do_async_req_list(Url, Method) ->
  385. %% do_async_req_list(Url, Method, [{stream_to, self()},
  386. %% {stream_chunk_size, 1000}]).
  387. do_async_req_list(Url, Method, Options) ->
  388. {Pid,_} = erlang:spawn_monitor(?MODULE, i_do_async_req_list,
  389. [self(), Url, Method,
  390. Options ++ [{stream_chunk_size, 1000}]]),
  391. %% io:format("Spawned process ~p~n", [Pid]),
  392. wait_for_resp(Pid).
  393. wait_for_resp(Pid) ->
  394. receive
  395. {async_result, Pid, Res} ->
  396. Res;
  397. {async_result, Other_pid, _} ->
  398. io:format("~p: Waiting for result from ~p: got from ~p~n", [self(), Pid, Other_pid]),
  399. wait_for_resp(Pid);
  400. {'DOWN', _, _, Pid, Reason} ->
  401. {'EXIT', Reason};
  402. {'DOWN', _, _, _, _} ->
  403. wait_for_resp(Pid);
  404. {'EXIT', _, normal} ->
  405. wait_for_resp(Pid);
  406. Msg ->
  407. io:format("Recvd unknown message: ~p~n", [Msg]),
  408. wait_for_resp(Pid)
  409. after 100000 ->
  410. {error, timeout}
  411. end.
  412. i_do_async_req_list(Parent, Url, Method, Options) ->
  413. Options_1 = case lists:member(once, Options) of
  414. true ->
  415. [{stream_to, {self(), once}} | (Options -- [once])];
  416. false ->
  417. [{stream_to, self()} | Options]
  418. end,
  419. Res = ibrowse:send_req(Url, [], Method, [], Options_1),
  420. case Res of
  421. {ibrowse_req_id, Req_id} ->
  422. Result = wait_for_async_resp(Req_id, Options, undefined, undefined, []),
  423. Parent ! {async_result, self(), Result};
  424. Err ->
  425. Parent ! {async_result, self(), Err}
  426. end.
  427. wait_for_async_resp(Req_id, Options, Acc_Stat_code, Acc_Headers, Body) ->
  428. receive
  429. {ibrowse_async_headers, Req_id, StatCode, Headers} ->
  430. %% io:format("Recvd headers...~n", []),
  431. maybe_stream_next(Req_id, Options),
  432. wait_for_async_resp(Req_id, Options, StatCode, Headers, Body);
  433. {ibrowse_async_response_end, Req_id} ->
  434. %% io:format("Recvd end of response.~n", []),
  435. Body_1 = list_to_binary(lists:reverse(Body)),
  436. {ok, Acc_Stat_code, Acc_Headers, Body_1};
  437. {ibrowse_async_response, Req_id, Data} ->
  438. maybe_stream_next(Req_id, Options),
  439. %% io:format("Recvd data...~n", []),
  440. wait_for_async_resp(Req_id, Options, Acc_Stat_code, Acc_Headers, [Data | Body]);
  441. {ibrowse_async_response, Req_id, {error, _} = Err} ->
  442. {ok, Acc_Stat_code, Acc_Headers, Err};
  443. Err ->
  444. {ok, Acc_Stat_code, Acc_Headers, Err}
  445. after 10000 ->
  446. {timeout, Acc_Stat_code, Acc_Headers, Body}
  447. end.
  448. maybe_stream_next(Req_id, Options) ->
  449. case lists:member(once, Options) of
  450. true ->
  451. ibrowse:stream_next(Req_id);
  452. false ->
  453. ok
  454. end.
  455. execute_req(local_test_fun, Method, Args) ->
  456. reset_ibrowse(),
  457. Result = (catch apply(?MODULE, Method, Args)),
  458. io:format(" ~-54.54w: ", [Method]),
  459. io:format("~p~n", [Result]);
  460. execute_req(Url, Method, Options) ->
  461. io:format("~7.7w, ~50.50s: ", [Method, Url]),
  462. Result = (catch ibrowse:send_req(Url, [], Method, [], Options)),
  463. case Result of
  464. {ok, SCode, _H, _B} ->
  465. io:format("Status code: ~p~n", [SCode]);
  466. Err ->
  467. io:format("~p~n", [Err])
  468. end.
  469. ue_test() ->
  470. ue_test(lists:duplicate(1024, $?)).
  471. ue_test(Data) ->
  472. {Time, Res} = timer:tc(ibrowse_lib, url_encode, [Data]),
  473. io:format("Time -> ~p~n", [Time]),
  474. io:format("Data Length -> ~p~n", [length(Data)]),
  475. io:format("Res Length -> ~p~n", [length(Res)]).
  476. % io:format("Result -> ~s~n", [Res]).
  477. log_msg(Fmt, Args) ->
  478. io:format("~s -- " ++ Fmt,
  479. [ibrowse_lib:printable_date() | Args]).
  480. %%------------------------------------------------------------------------------
  481. %% Test what happens when the response to a HEAD request is a
  482. %% Chunked-Encoding response with a non-empty body. Issue #67 on
  483. %% Github
  484. %% ------------------------------------------------------------------------------
  485. test_head_transfer_encoding() ->
  486. clear_msg_q(),
  487. test_head_transfer_encoding("http://localhost:8181/ibrowse_head_test").
  488. test_head_transfer_encoding(Url) ->
  489. case ibrowse:send_req(Url, [], head) of
  490. {ok, "200", _, _} ->
  491. success;
  492. Res ->
  493. {test_failed, Res}
  494. end.
  495. %%------------------------------------------------------------------------------
  496. %% Test what happens when the response to a HEAD request is a
  497. %% Chunked-Encoding response with a non-empty body. Issue #67 on
  498. %% Github
  499. %% ------------------------------------------------------------------------------
  500. test_binary_headers() ->
  501. clear_msg_q(),
  502. test_binary_headers("http://localhost:8181/ibrowse_echo_header").
  503. test_binary_headers(Url) ->
  504. case ibrowse:send_req(Url, [{<<"x-binary">>, <<"x-header">>}], get) of
  505. {ok, "200", Headers, _} ->
  506. case proplists:get_value("x-binary", Headers) of
  507. "x-header" ->
  508. success;
  509. V ->
  510. {fail, V}
  511. end;
  512. Res ->
  513. {test_failed, Res}
  514. end.
  515. %%------------------------------------------------------------------------------
  516. %% Test what happens when the response to a HEAD request is a
  517. %% Chunked-Encoding response with a non-empty body. Issue #67 on
  518. %% Github
  519. %% ------------------------------------------------------------------------------
  520. test_head_response_with_body() ->
  521. clear_msg_q(),
  522. test_head_response_with_body("http://localhost:8181/ibrowse_head_transfer_enc").
  523. test_head_response_with_body(Url) ->
  524. case ibrowse:send_req(Url, [], head, [], [{workaround, head_response_with_body}]) of
  525. {ok, "400", _, _} ->
  526. success;
  527. Res ->
  528. {test_failed, Res}
  529. end.
  530. %%------------------------------------------------------------------------------
  531. %% Test what happens when a 303 response has no body
  532. %% Github issue #97
  533. %% ------------------------------------------------------------------------------
  534. test_303_response_with_no_body() ->
  535. clear_msg_q(),
  536. test_303_response_with_no_body("http://localhost:8181/ibrowse_303_no_body_test").
  537. test_303_response_with_no_body(Url) ->
  538. ibrowse:add_config([{allow_303_with_no_body, true}]),
  539. case ibrowse:send_req(Url, [], post) of
  540. {ok, "303", _, _} ->
  541. success;
  542. Res ->
  543. {test_failed, Res}
  544. end.
  545. %% Make sure we don't break requests that do have a body.
  546. test_303_response_with_a_body() ->
  547. clear_msg_q(),
  548. test_303_response_with_no_body("http://localhost:8181/ibrowse_303_with_body_test").
  549. test_303_response_with_a_body(Url) ->
  550. ibrowse:add_config([{allow_303_with_no_body, true}]),
  551. case ibrowse:send_req(Url, [], post) of
  552. {ok, "303", _, "abcde"} ->
  553. success;
  554. Res ->
  555. {test_failed, Res}
  556. end.
  557. %% Test that the 'preserve_status_line' option works as expected
  558. test_preserve_status_line() ->
  559. case ibrowse:send_req("http://localhost:8181/ibrowse_preserve_status_line", [], get, [],
  560. [{preserve_status_line, true}]) of
  561. {ok, "200", [{ibrowse_status_line,<<"HTTP/1.1 200 OKBlah">>} | _], _} ->
  562. success;
  563. Res ->
  564. {test_failed, Res}
  565. end.
  566. %%------------------------------------------------------------------------------
  567. %% Test that when the save_response_to_file option is used with a server which
  568. %% does not send the Content-Length header, the response is saved correctly to
  569. %% a file
  570. %%------------------------------------------------------------------------------
  571. test_save_to_file_no_content_length() ->
  572. clear_msg_q(),
  573. {{Y, M, D}, {H, Mi, S}} = calendar:local_time(),
  574. Test_file = filename:join
  575. ([".",
  576. lists:flatten(
  577. io_lib:format("test_save_to_file_no_content_length_~p~p~p_~p~p~p.txt", [Y, M, D, H, Mi, S]))]),
  578. try
  579. case ibrowse:send_req("http://localhost:8181/ibrowse_send_file_conn_close", [], get, [],
  580. [{save_response_to_file, Test_file}]) of
  581. {ok, "200", _, {file, Test_file}} ->
  582. success;
  583. Res ->
  584. {test_failed, Res}
  585. end
  586. after
  587. file:delete(Test_file)
  588. end.
  589. %%------------------------------------------------------------------------------
  590. %% Test that retry of requests happens correctly, and that ibrowse doesn't retry
  591. %% if there is not enough time left
  592. %%------------------------------------------------------------------------------
  593. test_retry_of_requests() ->
  594. clear_msg_q(),
  595. test_retry_of_requests("http://localhost:8181/ibrowse_handle_one_request_only_with_delay").
  596. test_retry_of_requests(Url) ->
  597. reset_ibrowse(),
  598. Timeout_1 = 2050,
  599. Res_1 = test_retry_of_requests(Url, Timeout_1),
  600. case lists:filter(fun({_Pid, {ok, "200", _, _}}) ->
  601. true;
  602. (_) -> false
  603. end, Res_1) of
  604. [_|_] = X ->
  605. Res_1_1 = Res_1 -- X,
  606. case lists:all(
  607. fun({_Pid, {error, retry_later}}) ->
  608. true;
  609. (_) ->
  610. false
  611. end, Res_1_1) of
  612. true ->
  613. ok;
  614. false ->
  615. exit({failed, Timeout_1, Res_1})
  616. end;
  617. _ ->
  618. exit({failed, Timeout_1, Res_1})
  619. end,
  620. Timeout_2 = 2200,
  621. Res_2 = test_retry_of_requests(Url, Timeout_2),
  622. case lists:filter(fun({_Pid, {ok, "200", _, _}}) ->
  623. true;
  624. (_) -> false
  625. end, Res_2) of
  626. [_|_] = Res_2_X ->
  627. Res_2_1 = Res_2 -- Res_2_X,
  628. case lists:all(
  629. fun({_Pid, {error, X_err_2}}) ->
  630. (X_err_2 == retry_later) orelse (X_err_2 == req_timedout);
  631. (_) ->
  632. false
  633. end, Res_2_1) of
  634. true ->
  635. ok;
  636. false ->
  637. exit({failed, {?MODULE, ?LINE}, Timeout_2, Res_2})
  638. end;
  639. _ ->
  640. exit({failed, {?MODULE, ?LINE}, Timeout_2, Res_2})
  641. end,
  642. success.
  643. test_retry_of_requests(Url, Timeout) ->
  644. #url{host = Host, port = Port} = ibrowse_lib:parse_url(Url),
  645. ibrowse:set_max_sessions(Host, Port, 1),
  646. Parent = self(),
  647. Pids = lists:map(fun(_) ->
  648. spawn(fun() ->
  649. Res = (catch ibrowse:send_req(Url, [], get, [], [], Timeout)),
  650. Parent ! {self(), Res}
  651. end)
  652. end, lists:seq(1,10)),
  653. accumulate_worker_resp(Pids).
  654. %%------------------------------------------------------------------------------
  655. %% Test what happens when the request at the head of a pipeline times out
  656. %%------------------------------------------------------------------------------
  657. test_pipeline_head_timeout() ->
  658. clear_msg_q(),
  659. test_pipeline_head_timeout("http://localhost:8181/ibrowse_inac_timeout_test").
  660. test_pipeline_head_timeout(Url) ->
  661. {ok, Pid} = ibrowse:spawn_worker_process(Url),
  662. Fixed_timeout = 2000,
  663. Test_parent = self(),
  664. Fun = fun({fixed, Timeout}) ->
  665. X_pid = spawn(fun() ->
  666. do_test_pipeline_head_timeout(Url, Pid, Test_parent, Timeout)
  667. end),
  668. %% io:format("Pid ~p with a fixed timeout~n", [X_pid]),
  669. X_pid;
  670. (Timeout_mult) ->
  671. Timeout = Fixed_timeout + Timeout_mult*1000,
  672. X_pid = spawn(fun() ->
  673. do_test_pipeline_head_timeout(Url, Pid, Test_parent, Timeout)
  674. end),
  675. %% io:format("Pid ~p with a timeout of ~p~n", [X_pid, Timeout]),
  676. X_pid
  677. end,
  678. Pids = [Fun(X) || X <- [{fixed, Fixed_timeout} | lists:seq(1,10)]],
  679. Result = accumulate_worker_resp(Pids),
  680. case lists:all(fun({_, X_res}) ->
  681. (X_res == {error,req_timedout}) orelse (X_res == {error, connection_closed})
  682. end, Result) of
  683. true ->
  684. success;
  685. false ->
  686. {test_failed, Result}
  687. end.
  688. do_test_pipeline_head_timeout(Url, Pid, Test_parent, Req_timeout) ->
  689. Resp = ibrowse:send_req_direct(
  690. Pid,
  691. Url,
  692. [], get, [],
  693. [{socket_options,[{keepalive,true}]},
  694. {inactivity_timeout,180000},
  695. {connect_timeout,180000}], Req_timeout),
  696. Test_parent ! {self(), Resp}.
  697. accumulate_worker_resp(Pids) ->
  698. accumulate_worker_resp(Pids, []).
  699. accumulate_worker_resp([_ | _] = Pids, Acc) ->
  700. receive
  701. {Pid, Res} when is_pid(Pid) ->
  702. accumulate_worker_resp(Pids -- [Pid], [{Pid, Res} | Acc]);
  703. Err ->
  704. io:format("Received unexpected: ~p~n", [Err])
  705. end;
  706. accumulate_worker_resp([], Acc) ->
  707. lists:reverse(Acc).
  708. clear_msg_q() ->
  709. receive
  710. _ ->
  711. clear_msg_q()
  712. after 0 ->
  713. ok
  714. end.
  715. %%------------------------------------------------------------------------------
  716. %%
  717. %%------------------------------------------------------------------------------
  718. test_20122010() ->
  719. test_20122010("http://localhost:8181").
  720. test_20122010(Url) ->
  721. {ok, Pid} = ibrowse:spawn_worker_process(Url),
  722. 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">>,
  723. Test_parent = self(),
  724. Fun = fun() ->
  725. do_test_20122010(Url, Pid, Expected_resp, Test_parent)
  726. end,
  727. Pids = [erlang:spawn_monitor(Fun) || _ <- lists:seq(1,10)],
  728. wait_for_workers(Pids).
  729. wait_for_workers([{Pid, _Ref} | Pids]) ->
  730. receive
  731. {Pid, success} ->
  732. wait_for_workers(Pids)
  733. after 60000 ->
  734. test_failed
  735. end;
  736. wait_for_workers([]) ->
  737. success.
  738. do_test_20122010(Url, Pid, Expected_resp, Test_parent) ->
  739. do_test_20122010(10, Url, Pid, Expected_resp, Test_parent).
  740. do_test_20122010(0, _Url, _Pid, _Expected_resp, Test_parent) ->
  741. Test_parent ! {self(), success};
  742. do_test_20122010(Rem_count, Url, Pid, Expected_resp, Test_parent) ->
  743. {ibrowse_req_id, Req_id} = ibrowse:send_req_direct(
  744. Pid,
  745. Url ++ "/ibrowse_stream_once_chunk_pipeline_test",
  746. [], get, [],
  747. [{stream_to, {self(), once}},
  748. {inactivity_timeout, 10000},
  749. {include_ibrowse_req_id, true}]),
  750. do_trace("~p -- sent request ~1000.p~n", [self(), Req_id]),
  751. Req_id_str = lists:flatten(io_lib:format("~1000.p",[Req_id])),
  752. receive
  753. {ibrowse_async_headers, Req_id, "200", Headers} ->
  754. case lists:keysearch("x-ibrowse-request-id", 1, Headers) of
  755. {value, {_, Req_id_str}} ->
  756. ok;
  757. {value, {_, Req_id_1}} ->
  758. do_trace("~p -- Sent req-id: ~1000.p. Recvd: ~1000.p~n",
  759. [self(), Req_id, Req_id_1]),
  760. exit(req_id_mismatch)
  761. end
  762. after 5000 ->
  763. do_trace("~p -- response headers not received~n", [self()]),
  764. exit({timeout, test_failed})
  765. end,
  766. do_trace("~p -- response headers received~n", [self()]),
  767. ok = ibrowse:stream_next(Req_id),
  768. case do_test_20122010_1(Expected_resp, Req_id, []) of
  769. true ->
  770. do_test_20122010(Rem_count - 1, Url, Pid, Expected_resp, Test_parent);
  771. false ->
  772. Test_parent ! {self(), failed}
  773. end.
  774. do_test_20122010_1(Expected_resp, Req_id, Acc) ->
  775. receive
  776. {ibrowse_async_response, Req_id, Body_part} ->
  777. ok = ibrowse:stream_next(Req_id),
  778. do_test_20122010_1(Expected_resp, Req_id, [Body_part | Acc]);
  779. {ibrowse_async_response_end, Req_id} ->
  780. Acc_1 = list_to_binary(lists:reverse(Acc)),
  781. Result = Acc_1 == Expected_resp,
  782. do_trace("~p -- End of response. Result: ~p~n", [self(), Result]),
  783. Result
  784. after 1000 ->
  785. exit({timeout, test_failed})
  786. end.
  787. %%------------------------------------------------------------------------------
  788. %% Test requests where body is generated using a Fun
  789. %%------------------------------------------------------------------------------
  790. test_generate_body_0() ->
  791. Tid = ets:new(ibrowse_test_state, [public]),
  792. try
  793. Body_1 = <<"Part 1 of the body">>,
  794. Body_2 = <<"Part 2 of the body\r\n">>,
  795. Size = size(Body_1) + size(Body_2),
  796. Body = list_to_binary([Body_1, Body_2]),
  797. Fun = fun() ->
  798. case ets:lookup(Tid, body_gen_state) of
  799. [] ->
  800. ets:insert(Tid, {body_gen_state, 1}),
  801. {ok, Body_1};
  802. [{_, 1}]->
  803. ets:insert(Tid, {body_gen_state, 2}),
  804. {ok, Body_2};
  805. [{_, 2}] ->
  806. eof
  807. end
  808. end,
  809. case ibrowse:send_req("http://localhost:8181/echo_body",
  810. [{"Content-Length", Size}],
  811. post,
  812. Fun,
  813. [{response_format, binary},
  814. {http_vsn, {1,1}}]) of
  815. {ok, "200", _, Body} ->
  816. success;
  817. Err ->
  818. io:format("Test failed : ~p~n", [Err]),
  819. {test_failed, Err}
  820. end
  821. after
  822. ets:delete(Tid)
  823. end.
  824. do_trace(Fmt, Args) ->
  825. do_trace(get(my_trace_flag), Fmt, Args).
  826. do_trace(true, Fmt, Args) ->
  827. io:format("~s -- " ++ Fmt, [ibrowse_lib:printable_date() | Args]);
  828. do_trace(_, _, _) ->
  829. ok.
  830. reset_ibrowse() ->
  831. application:stop(ibrowse),
  832. application:start(ibrowse).