PageRenderTime 28ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/ucengine/src/tests/tests_utils.erl

http://github.com/AF83/ucengine
Erlang | 81 lines | 52 code | 12 blank | 17 comment | 0 complexity | ecefec3ca35eb7f57a8b93db7c59e99d MD5 | raw file
  1. %%
  2. %% U.C.Engine - Unified Collaboration Engine
  3. %% Copyright (C) 2011 af83
  4. %%
  5. %% This program is free software: you can redistribute it and/or modify
  6. %% it under the terms of the GNU Affero General Public License as published by
  7. %% the Free Software Foundation, either version 3 of the License, or
  8. %% (at your option) any later version.
  9. %%
  10. %% This program is distributed in the hope that it will be useful,
  11. %% but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. %% GNU Affero General Public License for more details.
  14. %%
  15. %% You should have received a copy of the GNU Affero General Public License
  16. %% along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. %%
  18. -module(tests_utils).
  19. -include("uce.hrl").
  20. -export([ post/3
  21. , post/5
  22. , post_raw/5
  23. , get_raw/3
  24. , get/2
  25. , get/3
  26. , options_raw/2
  27. , head_raw/2
  28. , put/3
  29. , delete/3
  30. ]).
  31. -export([url_encode/1]).
  32. options_raw(BaseUrl, Path) ->
  33. request(BaseUrl, Path, options, [], "", "").
  34. head_raw(BaseUrl, Path) ->
  35. request(BaseUrl, Path, head, [], "", "").
  36. get_raw(BaseUrl, Path, Params) ->
  37. request(BaseUrl, Path, get, Params, "", "").
  38. get(BaseUrl, Path) ->
  39. get(BaseUrl, Path, []).
  40. get(BaseUrl, Path, Params) ->
  41. {ok, _, _, JSON} = get_raw(BaseUrl, Path, Params),
  42. mochijson:decode(JSON).
  43. post_raw(BaseUrl, Path, Params, ContentType, Body) ->
  44. request(BaseUrl, Path, post, Params, ContentType, Body).
  45. post(BaseUrl, Path, Params) ->
  46. post(BaseUrl, Path, [], "application/x-www-form-urlencoded", url_encode(Params)).
  47. post(BaseUrl, Path, Params, ContentType, Body) ->
  48. {ok, _, _, JSON} = post_raw(BaseUrl, Path, Params, ContentType, Body),
  49. mochijson:decode(JSON).
  50. put(BaseUrl, Path, Params) ->
  51. {ok, _, _, JSON} = request(BaseUrl, Path, put, Params, "application/x-www-form-urlencoded", []),
  52. mochijson:decode(JSON).
  53. delete(BaseUrl, Path, Params) ->
  54. {ok, _, _, JSON} =
  55. ibrowse:send_req(BaseUrl ++ Path ++ "?" ++ url_encode(Params), [], delete),
  56. mochijson:decode(JSON).
  57. request(BaseUrl, Path, Method, Params, ContentType, Body) ->
  58. Query = case Params of
  59. [] ->
  60. "";
  61. _ ->
  62. "?" ++ url_encode(Params)
  63. end,
  64. ibrowse:send_req(BaseUrl ++ Path ++ Query, [{"Content-Type", ContentType}], Method, Body, [{content_type, ContentType}]).
  65. url_encode(Params) ->
  66. UrlEncodedParams = [yaws_api:url_encode(Elem) ++ "=" ++
  67. yaws_api:url_encode(Value) ||
  68. {Elem, Value} <- Params],
  69. string:join(UrlEncodedParams, "&").