emqx_dashboard_haproxy_SUITE.erl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_dashboard_haproxy_SUITE).
  17. -compile(nowarn_export_all).
  18. -compile(export_all).
  19. -include_lib("eunit/include/eunit.hrl").
  20. -include("emqx_dashboard.hrl").
  21. all() ->
  22. emqx_common_test_helpers:all(?MODULE).
  23. init_per_suite(Config) ->
  24. emqx_mgmt_api_test_util:init_suite([emqx_management], fun set_special_configs/1),
  25. Config.
  26. set_special_configs(emqx_dashboard) ->
  27. emqx_dashboard_api_test_helpers:set_default_config(<<"admin">>, true),
  28. ok;
  29. set_special_configs(_) ->
  30. ok.
  31. end_per_suite(_Config) ->
  32. emqx_mgmt_api_test_util:end_suite([emqx_management]).
  33. t_status(_Config) ->
  34. ProxyInfo = #{
  35. version => 1,
  36. command => proxy,
  37. transport_family => ipv4,
  38. transport_protocol => stream,
  39. src_address => {127, 0, 0, 1},
  40. src_port => 444,
  41. dest_address => {192, 168, 0, 1},
  42. dest_port => 443
  43. },
  44. {ok, Socket} = gen_tcp:connect(
  45. "localhost",
  46. 18083,
  47. [binary, {active, false}, {packet, raw}]
  48. ),
  49. ok = gen_tcp:send(Socket, ranch_proxy_header:header(ProxyInfo)),
  50. {ok, _Role, Token} = emqx_dashboard_admin:sign_token(<<"admin">>, <<"public">>),
  51. ok = gen_tcp:send(
  52. Socket,
  53. "GET /status HTTP/1.1\r\n"
  54. "Host: localhost\r\n"
  55. "Authorization: Bearer " ++ binary_to_list(Token) ++
  56. "\r\n"
  57. "\r\n"
  58. ),
  59. {_, 200, _, Rest0} = cow_http:parse_status_line(raw_recv_head(Socket)),
  60. {Headers, Body0} = cow_http:parse_headers(Rest0),
  61. {_, LenBin} = lists:keyfind(<<"content-length">>, 1, Headers),
  62. Len = binary_to_integer(LenBin),
  63. Body =
  64. if
  65. byte_size(Body0) =:= Len ->
  66. Body0;
  67. true ->
  68. {ok, Body1} = gen_tcp:recv(Socket, Len - byte_size(Body0), 5000),
  69. <<Body0/bits, Body1/bits>>
  70. end,
  71. ?assertMatch({match, _}, re:run(Body, "Node .+ is started\nemqx is running")),
  72. ok.
  73. raw_recv_head(Socket) ->
  74. {ok, Data} = gen_tcp:recv(Socket, 0, 10000),
  75. raw_recv_head(Socket, Data).
  76. raw_recv_head(Socket, Buffer) ->
  77. case binary:match(Buffer, <<"\r\n\r\n">>) of
  78. nomatch ->
  79. {ok, Data} = gen_tcp:recv(Socket, 0, 10000),
  80. raw_recv_head(Socket, <<Buffer/binary, Data/binary>>);
  81. {_, _} ->
  82. Buffer
  83. end.