Browse Source

http publish

huangdan 9 years ago
parent
commit
a318244be0
1 changed files with 32 additions and 1 deletions
  1. 32 1
      test/emqttd_SUITE.erl

+ 32 - 1
test/emqttd_SUITE.erl

@@ -22,6 +22,8 @@
 
 
 -include_lib("eunit/include/eunit.hrl").
 -include_lib("eunit/include/eunit.hrl").
 
 
+-define(CONTENT_TYPE, "application/x-www-form-urlencoded").
+
 all() ->
 all() ->
     [{group, protocol},
     [{group, protocol},
      {group, pubsub},
      {group, pubsub},
@@ -62,7 +64,10 @@ groups() ->
       [dispatch_retained_messages]},
       [dispatch_retained_messages]},
      {backend, [sequence],
      {backend, [sequence],
       []},
       []},
-    {http, [sequence], [request_status]},
+    {http, [sequence], 
+     [request_status,
+      request_publish
+     ]},
      {cli, [sequence],
      {cli, [sequence],
       [ctl_register_cmd,
       [ctl_register_cmd,
        cli_status,
        cli_status,
@@ -330,6 +335,32 @@ request_status(_) ->
     httpc:request(get, {Url, []}, [], []),
     httpc:request(get, {Url, []}, [], []),
     ?assertEqual(binary_to_list(Status), Return).
     ?assertEqual(binary_to_list(Status), Return).
 
 
+request_publish(_) ->
+    ok = emqttd:subscribe(<<"a/b/c">>, self(), [{qos, 1}]),
+    Params = "qos=1&retain=0&topic=a/b/c&message=hello",
+    ?assert(connect_emqttd_publish_(post, "mqtt/publish", Params, auth_header_("", ""))),
+    ?assert(receive {dispatch, <<"a/b/c">>, _} -> true after 2 -> false end),
+    emqttd:unsubscribe(<<"a/b/c">>).
+
+connect_emqttd_publish_(Method, Api, Params, Auth) ->
+    Url = "http://127.0.0.1:8083/" ++ Api,
+    case httpc:request(Method, {Url, [Auth], ?CONTENT_TYPE, Params}, [], []) of
+    {error, socket_closed_remotely} ->
+        false;
+    {ok, {{"HTTP/1.1", 200, "OK"}, _, _Return} }  ->
+        true;
+    {ok, {{"HTTP/1.1", 400, _}, _, []}} ->
+        false;
+    {ok, {{"HTTP/1.1", 404, _}, _, []}} ->
+        false
+    end.
+	
+auth_header_(User, Pass) ->
+    Encoded = base64:encode_to_string(lists:append([User,":",Pass])),
+    {"Authorization","Basic " ++ Encoded}.
+
+
+
 
 
 %% cli group
 %% cli group
 %%--------------------------------------------------------------------
 %%--------------------------------------------------------------------