]> review.fuel-infra Code Review - packages/trusty/rabbitmq-server.git/blob
d0a5c1beb93a7b54726faaee5120a68a820b4d1a
[packages/trusty/rabbitmq-server.git] /
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/
5 %%
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
9 %%   under the License.
10 %%
11 %%   The Original Code is RabbitMQ Management Console.
12 %%
13 %%   The Initial Developer of the Original Code is GoPivotal, Inc.
14 %%   Copyright (c) 2010-2014 GoPivotal, Inc.  All rights reserved.
15 %%
16
17 -module(rabbit_shovel_mgmt_test_http).
18
19 -include_lib("rabbitmq_management/include/rabbit_mgmt_test.hrl").
20
21 -import(rabbit_misc, [pget/2]).
22
23 shovels_test() ->
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, <<".*">>},
30              {write,     <<".*">>},
31              {read,      <<".*">>}],
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),
35
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">>},
45                 {vhost, <<"/">>},
46                 {type,  <<"dynamic">>}],
47     Dynamic2 = [{name,  <<"my-dynamic">>},
48                 {vhost, <<"v">>},
49                 {type,  <<"dynamic">>}],
50     Assert = fun (Req, User, Res) ->
51                      assert_list(Res, http_get(Req, User, User, ?OK))
52              end,
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]),
62
63     http_delete("/vhosts/v", ?NO_CONTENT),
64     http_delete("/users/admin", ?NO_CONTENT),
65     http_delete("/users/mon", ?NO_CONTENT),
66     ok.
67
68 %%---------------------------------------------------------------------------
69 %% TODO this is all copypasta from the mgmt tests
70
71 http_get(Path) ->
72     http_get(Path, ?OK).
73
74 http_get(Path, CodeExp) ->
75     http_get(Path, "guest", "guest", CodeExp).
76
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).
82
83 http_put(Path, List, CodeExp) ->
84     http_put_raw(Path, format_for_upload(List), CodeExp).
85
86 http_put(Path, List, User, Pass, CodeExp) ->
87     http_put_raw(Path, format_for_upload(List), User, Pass, CodeExp).
88
89 http_post(Path, List, CodeExp) ->
90     http_post_raw(Path, format_for_upload(List), CodeExp).
91
92 http_post(Path, List, User, Pass, CodeExp) ->
93     http_post_raw(Path, format_for_upload(List), User, Pass, CodeExp).
94
95 format_for_upload(none) ->
96     <<"">>;
97 format_for_upload(List) ->
98     iolist_to_binary(mochijson2:encode({struct, List})).
99
100 http_put_raw(Path, Body, CodeExp) ->
101     http_upload_raw(put, Path, Body, "guest", "guest", CodeExp).
102
103 http_put_raw(Path, Body, User, Pass, CodeExp) ->
104     http_upload_raw(put, Path, Body, User, Pass, CodeExp).
105
106 http_post_raw(Path, Body, CodeExp) ->
107     http_upload_raw(post, Path, Body, "guest", "guest", CodeExp).
108
109 http_post_raw(Path, Body, User, Pass, CodeExp) ->
110     http_upload_raw(post, Path, Body, User, Pass, CodeExp).
111
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).
117
118 http_delete(Path, CodeExp) ->
119     http_delete(Path, "guest", "guest", CodeExp).
120
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).
126
127 assert_code(CodeExp, CodeAct, Type, Path, Body) ->
128     case CodeExp of
129         CodeAct -> ok;
130         _       -> throw({expected, CodeExp, got, CodeAct, type, Type,
131                           path, Path, body, Body})
132     end.
133
134 req(Type, Path, Headers) ->
135     httpc:request(Type, {?PREFIX ++ Path, Headers}, ?HTTPC_OPTS, []).
136
137 req(Type, Path, Headers, Body) ->
138     httpc:request(Type, {?PREFIX ++ Path, Headers, "application/json", Body},
139                   ?HTTPC_OPTS, []).
140
141 decode(?OK, _Headers,  ResBody) -> cleanup(mochijson2:decode(ResBody));
142 decode(_,    Headers, _ResBody) -> Headers.
143
144 cleanup(L) when is_list(L) ->
145     [cleanup(I) || I <- L];
146 cleanup({struct, I}) ->
147     cleanup(I);
148 cleanup({K, V}) when is_binary(K) ->
149     {list_to_atom(binary_to_list(K)), cleanup(V)};
150 cleanup(I) ->
151     I.
152
153 auth_header(Username, Password) ->
154     {"Authorization",
155      "Basic " ++ binary_to_list(base64:encode(Username ++ ":" ++ Password))}.
156
157 assert_list(Exp, Act) ->
158     case length(Exp) == length(Act) of
159         true  -> ok;
160         false -> throw({expected, Exp, actual, Act})
161     end,
162     [case length(lists:filter(fun(ActI) -> test_item(ExpI, ActI) end, Act)) of
163          1 -> ok;
164          N -> throw({found, N, ExpI, in, Act})
165      end || ExpI <- Exp].
166
167 assert_item(Exp, Act) ->
168     case test_item0(Exp, Act) of
169         [] -> ok;
170         Or -> throw(Or)
171     end.
172
173 test_item(Exp, Act) ->
174     case test_item0(Exp, Act) of
175         [] -> true;
176         _  -> false
177     end.
178
179 test_item0(Exp, Act) ->
180     [{did_not_find, ExpI, in, Act} || ExpI <- Exp,
181                                       not lists:member(ExpI, Act)].