PageRenderTime 268ms CodeModel.GetById 131ms app.highlight 9ms RepoModel.GetById 122ms 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
20-include("uce.hrl").
21
22-export([ post/3
23        , post/5
24        , post_raw/5
25        , get_raw/3
26        , get/2
27        , get/3
28        , options_raw/2
29        , head_raw/2
30        , put/3
31        , delete/3
32        ]).
33-export([url_encode/1]).
34
35options_raw(BaseUrl, Path) ->
36    request(BaseUrl, Path, options, [], "", "").
37
38head_raw(BaseUrl, Path) ->
39    request(BaseUrl, Path, head, [], "", "").
40
41get_raw(BaseUrl, Path, Params) ->
42    request(BaseUrl, Path, get, Params, "", "").
43
44get(BaseUrl, Path) ->
45    get(BaseUrl, Path, []).
46get(BaseUrl, Path, Params) ->
47    {ok, _, _, JSON} = get_raw(BaseUrl, Path, Params),
48    mochijson:decode(JSON).
49
50post_raw(BaseUrl, Path, Params, ContentType, Body) ->
51    request(BaseUrl, Path, post, Params, ContentType, Body).
52
53post(BaseUrl, Path, Params) ->
54    post(BaseUrl, Path, [], "application/x-www-form-urlencoded", url_encode(Params)).
55post(BaseUrl, Path, Params, ContentType, Body) ->
56    {ok, _, _, JSON} = post_raw(BaseUrl, Path, Params, ContentType, Body),
57    mochijson:decode(JSON).
58
59put(BaseUrl, Path, Params) ->
60    {ok, _, _, JSON} = request(BaseUrl, Path, put, Params, "application/x-www-form-urlencoded", []),
61    mochijson:decode(JSON).
62
63delete(BaseUrl, Path, Params) ->
64    {ok, _, _, JSON} =
65        ibrowse:send_req(BaseUrl ++ Path ++ "?" ++ url_encode(Params), [], delete),
66    mochijson:decode(JSON).
67
68request(BaseUrl, Path, Method, Params, ContentType, Body) ->
69    Query = case Params of
70                [] ->
71                    "";
72                _ ->
73                    "?" ++ url_encode(Params)
74            end,
75   ibrowse:send_req(BaseUrl ++ Path ++ Query, [{"Content-Type", ContentType}], Method, Body, [{content_type, ContentType}]).
76
77url_encode(Params) ->
78    UrlEncodedParams = [yaws_api:url_encode(Elem) ++ "=" ++
79                            yaws_api:url_encode(Value) ||
80                           {Elem, Value} <- Params],
81    string:join(UrlEncodedParams, "&").