1 %% The contents of this file are subject to the Mozilla Public License
2 %% Version 1.1 (the "License"); you may not use this file except in
3 %% compliance with the License. You may obtain a copy of the License at
4 %% http://www.mozilla.org/MPL/
6 %% Software distributed under the License is distributed on an "AS IS"
7 %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
8 %% License for the specific language governing rights and limitations
11 %% The Original Code is RabbitMQ Management Console.
13 %% The Initial Developer of the Original Code is GoPivotal, Inc.
14 %% Copyright (c) 2010-2014 GoPivotal, Inc. All rights reserved.
17 -module(rabbit_shovel_mgmt_test_http).
19 -include_lib("rabbitmq_management/include/rabbit_mgmt_test.hrl").
21 -import(rabbit_misc, [pget/2]).
24 http_put("/users/admin", [{password, <<"admin">>},
25 {tags, <<"administrator">>}], ?NO_CONTENT),
26 http_put("/users/mon", [{password, <<"mon">>},
27 {tags, <<"monitoring">>}], ?NO_CONTENT),
28 http_put("/vhosts/v", none, ?NO_CONTENT),
29 Perms = [{configure, <<".*">>},
32 http_put("/permissions/v/guest", Perms, ?NO_CONTENT),
33 http_put("/permissions/v/admin", Perms, ?NO_CONTENT),
34 http_put("/permissions/v/mon", Perms, ?NO_CONTENT),
36 [http_put("/parameters/shovel/" ++ V ++ "/my-dynamic",
37 [{value, [{'src-uri', <<"amqp://">>},
38 {'dest-uri', <<"amqp://">>},
39 {'src-queue', <<"test">>},
40 {'dest-queue', <<"test2">>}]}], ?NO_CONTENT)
41 || V <- ["%2f", "v"]],
42 Static = [{name, <<"my-static">>},
43 {type, <<"static">>}],
44 Dynamic1 = [{name, <<"my-dynamic">>},
46 {type, <<"dynamic">>}],
47 Dynamic2 = [{name, <<"my-dynamic">>},
49 {type, <<"dynamic">>}],
50 Assert = fun (Req, User, Res) ->
51 assert_list(Res, http_get(Req, User, User, ?OK))
53 Assert("/shovels", "guest", [Static, Dynamic1, Dynamic2]),
54 Assert("/shovels/%2f", "guest", [Dynamic1]),
55 Assert("/shovels/v", "guest", [Dynamic2]),
56 Assert("/shovels", "admin", [Static, Dynamic2]),
57 Assert("/shovels/%2f", "admin", []),
58 Assert("/shovels/v", "admin", [Dynamic2]),
59 Assert("/shovels", "mon", [Dynamic2]),
60 Assert("/shovels/%2f", "mon", []),
61 Assert("/shovels/v", "mon", [Dynamic2]),
63 http_delete("/vhosts/v", ?NO_CONTENT),
64 http_delete("/users/admin", ?NO_CONTENT),
65 http_delete("/users/mon", ?NO_CONTENT),
68 %%---------------------------------------------------------------------------
69 %% TODO this is all copypasta from the mgmt tests
74 http_get(Path, CodeExp) ->
75 http_get(Path, "guest", "guest", CodeExp).
77 http_get(Path, User, Pass, CodeExp) ->
78 {ok, {{_HTTP, CodeAct, _}, Headers, ResBody}} =
79 req(get, Path, [auth_header(User, Pass)]),
80 assert_code(CodeExp, CodeAct, "GET", Path, ResBody),
81 decode(CodeExp, Headers, ResBody).
83 http_put(Path, List, CodeExp) ->
84 http_put_raw(Path, format_for_upload(List), CodeExp).
86 http_put(Path, List, User, Pass, CodeExp) ->
87 http_put_raw(Path, format_for_upload(List), User, Pass, CodeExp).
89 http_post(Path, List, CodeExp) ->
90 http_post_raw(Path, format_for_upload(List), CodeExp).
92 http_post(Path, List, User, Pass, CodeExp) ->
93 http_post_raw(Path, format_for_upload(List), User, Pass, CodeExp).
95 format_for_upload(none) ->
97 format_for_upload(List) ->
98 iolist_to_binary(mochijson2:encode({struct, List})).
100 http_put_raw(Path, Body, CodeExp) ->
101 http_upload_raw(put, Path, Body, "guest", "guest", CodeExp).
103 http_put_raw(Path, Body, User, Pass, CodeExp) ->
104 http_upload_raw(put, Path, Body, User, Pass, CodeExp).
106 http_post_raw(Path, Body, CodeExp) ->
107 http_upload_raw(post, Path, Body, "guest", "guest", CodeExp).
109 http_post_raw(Path, Body, User, Pass, CodeExp) ->
110 http_upload_raw(post, Path, Body, User, Pass, CodeExp).
112 http_upload_raw(Type, Path, Body, User, Pass, CodeExp) ->
113 {ok, {{_HTTP, CodeAct, _}, Headers, ResBody}} =
114 req(Type, Path, [auth_header(User, Pass)], Body),
115 assert_code(CodeExp, CodeAct, Type, Path, ResBody),
116 decode(CodeExp, Headers, ResBody).
118 http_delete(Path, CodeExp) ->
119 http_delete(Path, "guest", "guest", CodeExp).
121 http_delete(Path, User, Pass, CodeExp) ->
122 {ok, {{_HTTP, CodeAct, _}, Headers, ResBody}} =
123 req(delete, Path, [auth_header(User, Pass)]),
124 assert_code(CodeExp, CodeAct, "DELETE", Path, ResBody),
125 decode(CodeExp, Headers, ResBody).
127 assert_code(CodeExp, CodeAct, Type, Path, Body) ->
130 _ -> throw({expected, CodeExp, got, CodeAct, type, Type,
131 path, Path, body, Body})
134 req(Type, Path, Headers) ->
135 httpc:request(Type, {?PREFIX ++ Path, Headers}, ?HTTPC_OPTS, []).
137 req(Type, Path, Headers, Body) ->
138 httpc:request(Type, {?PREFIX ++ Path, Headers, "application/json", Body},
141 decode(?OK, _Headers, ResBody) -> cleanup(mochijson2:decode(ResBody));
142 decode(_, Headers, _ResBody) -> Headers.
144 cleanup(L) when is_list(L) ->
145 [cleanup(I) || I <- L];
146 cleanup({struct, I}) ->
148 cleanup({K, V}) when is_binary(K) ->
149 {list_to_atom(binary_to_list(K)), cleanup(V)};
153 auth_header(Username, Password) ->
155 "Basic " ++ binary_to_list(base64:encode(Username ++ ":" ++ Password))}.
157 assert_list(Exp, Act) ->
158 case length(Exp) == length(Act) of
160 false -> throw({expected, Exp, actual, Act})
162 [case length(lists:filter(fun(ActI) -> test_item(ExpI, ActI) end, Act)) of
164 N -> throw({found, N, ExpI, in, Act})
167 assert_item(Exp, Act) ->
168 case test_item0(Exp, Act) of
173 test_item(Exp, Act) ->
174 case test_item0(Exp, Act) of
179 test_item0(Exp, Act) ->
180 [{did_not_find, ExpI, in, Act} || ExpI <- Exp,
181 not lists:member(ExpI, Act)].