2010-10-01 19:27:10 +08:00
|
|
|
%% The contents of this file are subject to the Mozilla Public License
|
|
|
|
|
%% Version 1.1 (the "License"); you may not use this file except in
|
|
|
|
|
%% compliance with the License. You may obtain a copy of the License at
|
|
|
|
|
%% http://www.mozilla.org/MPL/
|
|
|
|
|
%%
|
|
|
|
|
%% Software distributed under the License is distributed on an "AS IS"
|
|
|
|
|
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
|
%% License for the specific language governing rights and limitations
|
|
|
|
|
%% under the License.
|
|
|
|
|
%%
|
|
|
|
|
%% The Original Code is RabbitMQ Management Console.
|
|
|
|
|
%%
|
|
|
|
|
%% The Initial Developers of the Original Code are Rabbit Technologies Ltd.
|
|
|
|
|
%%
|
|
|
|
|
%% Copyright (C) 2010 Rabbit Technologies Ltd.
|
|
|
|
|
%%
|
|
|
|
|
%% All Rights Reserved.
|
|
|
|
|
%%
|
|
|
|
|
%% Contributor(s): ______________________________________.
|
|
|
|
|
%%
|
2010-10-08 00:31:18 +08:00
|
|
|
-module(rabbit_stomp_test_util).
|
2010-10-01 19:27:10 +08:00
|
|
|
|
|
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
2010-10-28 19:04:40 +08:00
|
|
|
-include_lib("amqp_client/include/amqp_client.hrl").
|
2010-10-28 15:59:43 +08:00
|
|
|
-include("rabbit_stomp_frame.hrl").
|
|
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
2010-10-28 19:04:40 +08:00
|
|
|
%% Header Processing Tests
|
2010-10-28 15:59:43 +08:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
|
2010-10-28 16:17:38 +08:00
|
|
|
longstr_field_test() ->
|
|
|
|
|
{<<"ABC">>, longstr, <<"DEF">>} =
|
|
|
|
|
rabbit_stomp_util:longstr_field("ABC", "DEF").
|
|
|
|
|
|
2010-10-28 19:57:58 +08:00
|
|
|
message_properties_test() ->
|
|
|
|
|
Headers = [
|
|
|
|
|
{"content-type", "text/plain"},
|
|
|
|
|
{"content-encoding", "UTF-8"},
|
|
|
|
|
{"delivery-mode", "2"},
|
|
|
|
|
{"priority", "1"},
|
|
|
|
|
{"correlation-id", "123"},
|
|
|
|
|
{"reply-to", "something"},
|
|
|
|
|
{"amqp-message-id", "M123"},
|
|
|
|
|
{"X-str", "foo"},
|
|
|
|
|
{"X-int", "123"}
|
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
#'P_basic'{
|
|
|
|
|
content_type = <<"text/plain">>,
|
|
|
|
|
content_encoding = <<"UTF-8">>,
|
|
|
|
|
delivery_mode = 2,
|
|
|
|
|
priority = 1,
|
|
|
|
|
correlation_id = <<"123">>,
|
|
|
|
|
reply_to = <<"something">>,
|
|
|
|
|
message_id = <<"M123">>,
|
|
|
|
|
headers = [
|
|
|
|
|
{<<"str">>, longstr, <<"foo">>},
|
|
|
|
|
{<<"int">>, longstr, <<"123">>}]
|
|
|
|
|
} =
|
|
|
|
|
rabbit_stomp_util:message_properties(#stomp_frame{headers = Headers}).
|
|
|
|
|
|
2010-10-28 19:04:40 +08:00
|
|
|
message_headers_test() ->
|
|
|
|
|
Destination = "/queue/foo",
|
|
|
|
|
SessionId = "1234567",
|
|
|
|
|
|
|
|
|
|
Delivery = #'basic.deliver'{
|
|
|
|
|
consumer_tag = <<"Q_123">>,
|
|
|
|
|
delivery_tag = 123},
|
|
|
|
|
|
|
|
|
|
Properties = #'P_basic'{
|
|
|
|
|
headers = [{<<"str">>, longstr, <<"foo">>},
|
|
|
|
|
{<<"int">>, signedint, 123}],
|
|
|
|
|
content_type = <<"text/plain">>,
|
|
|
|
|
content_encoding = <<"UTF-8">>,
|
|
|
|
|
delivery_mode = 2,
|
|
|
|
|
priority = 1,
|
|
|
|
|
correlation_id = 123,
|
|
|
|
|
reply_to = <<"something">>,
|
|
|
|
|
message_id = <<"M123">>},
|
|
|
|
|
|
|
|
|
|
Headers = rabbit_stomp_util:message_headers(Destination, SessionId,
|
|
|
|
|
Delivery, Properties),
|
|
|
|
|
|
|
|
|
|
Expected = [
|
|
|
|
|
{"destination", Destination},
|
|
|
|
|
{"message-id", [<<"Q_123">>, "@@", SessionId, "@@", "123"]},
|
|
|
|
|
{"content-type", "text/plain"},
|
|
|
|
|
{"content-encoding", "UTF-8"},
|
|
|
|
|
{"delivery-mode", "2"},
|
|
|
|
|
{"priority", "1"},
|
|
|
|
|
{"correlation-id", "123"},
|
|
|
|
|
{"reply-to", "something"},
|
|
|
|
|
{"amqp-message-id", "M123"},
|
|
|
|
|
{"X-str", "foo"},
|
|
|
|
|
{"X-int", "123"}
|
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
[] = lists:subtract(Headers, Expected).
|
|
|
|
|
|
2010-10-28 16:17:38 +08:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
%% Frame Parsing Tests
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
|
2010-10-28 15:59:43 +08:00
|
|
|
ack_mode_auto_test() ->
|
|
|
|
|
Frame = #stomp_frame{headers = [{"ack", "auto"}]},
|
|
|
|
|
auto = rabbit_stomp_util:ack_mode(Frame).
|
|
|
|
|
|
|
|
|
|
ack_mode_auto_default_test() ->
|
|
|
|
|
Frame = #stomp_frame{headers = []},
|
|
|
|
|
auto = rabbit_stomp_util:ack_mode(Frame).
|
|
|
|
|
|
|
|
|
|
ack_mode_client_test() ->
|
|
|
|
|
Frame = #stomp_frame{headers = [{"ack", "client"}]},
|
|
|
|
|
client = rabbit_stomp_util:ack_mode(Frame).
|
|
|
|
|
|
2010-10-28 16:17:38 +08:00
|
|
|
consumer_tag_id_test() ->
|
|
|
|
|
Frame = #stomp_frame{headers = [{"id", "foo"}]},
|
|
|
|
|
{ok, <<"T_foo">>} = rabbit_stomp_util:consumer_tag(Frame).
|
|
|
|
|
|
|
|
|
|
consumer_tag_destination_test() ->
|
|
|
|
|
Frame = #stomp_frame{headers = [{"destination", "foo"}]},
|
|
|
|
|
{ok, <<"Q_foo">>} = rabbit_stomp_util:consumer_tag(Frame).
|
|
|
|
|
|
|
|
|
|
consumer_tag_invalid_test() ->
|
|
|
|
|
Frame = #stomp_frame{headers = []},
|
|
|
|
|
{error, missing_destination_header} = rabbit_stomp_util:consumer_tag(Frame).
|
2010-10-28 15:59:43 +08:00
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
%% Destination Parsing Tests
|
|
|
|
|
%%--------------------------------------------------------------------
|
2010-10-01 19:27:10 +08:00
|
|
|
|
|
|
|
|
valid_queue_test() ->
|
2010-10-01 21:49:23 +08:00
|
|
|
{ok, {queue, "test"}} = parse_destination("/queue/test").
|
2010-10-01 19:27:10 +08:00
|
|
|
|
2010-10-01 20:41:04 +08:00
|
|
|
valid_topic_test() ->
|
2010-10-01 21:49:23 +08:00
|
|
|
{ok, {topic, "test"}} = parse_destination("/topic/test").
|
2010-10-01 20:41:04 +08:00
|
|
|
|
|
|
|
|
valid_exchange_test() ->
|
2010-10-01 21:49:23 +08:00
|
|
|
{ok, {exchange, {"test", undefined}}} = parse_destination("/exchange/test").
|
2010-10-01 20:41:04 +08:00
|
|
|
|
|
|
|
|
valid_exchange_with_pattern_test() ->
|
2010-10-08 00:31:18 +08:00
|
|
|
{ok, {exchange, {"test", "pattern"}}} =
|
2010-10-01 20:41:04 +08:00
|
|
|
parse_destination("/exchange/test/pattern").
|
|
|
|
|
|
|
|
|
|
queue_with_no_name_test() ->
|
|
|
|
|
{error, {invalid_destination, queue, ""}} = parse_destination("/queue").
|
|
|
|
|
|
|
|
|
|
topic_with_no_name_test() ->
|
|
|
|
|
{error, {invalid_destination, topic, ""}} = parse_destination("/topic").
|
|
|
|
|
|
|
|
|
|
exchange_with_no_name_test() ->
|
2010-10-08 00:31:18 +08:00
|
|
|
{error, {invalid_destination, exchange, ""}} =
|
2010-10-01 20:41:04 +08:00
|
|
|
parse_destination("/exchange").
|
|
|
|
|
|
2010-10-19 00:16:33 +08:00
|
|
|
exchange_default_name_test() ->
|
|
|
|
|
{error, {invalid_destination, exchange, "//foo"}} =
|
|
|
|
|
parse_destination("/exchange//foo").
|
|
|
|
|
|
2010-10-01 20:41:04 +08:00
|
|
|
queue_with_no_name_slash_test() ->
|
|
|
|
|
{error, {invalid_destination, queue, "/"}} = parse_destination("/queue/").
|
|
|
|
|
|
|
|
|
|
topic_with_no_name_slash_test() ->
|
|
|
|
|
{error, {invalid_destination, topic, "/"}} = parse_destination("/topic/").
|
|
|
|
|
|
|
|
|
|
exchange_with_no_name_slash_test() ->
|
2010-10-08 00:31:18 +08:00
|
|
|
{error, {invalid_destination, exchange, "/"}} =
|
2010-10-01 21:49:23 +08:00
|
|
|
parse_destination("/exchange/").
|
|
|
|
|
|
|
|
|
|
queue_with_invalid_name_test() ->
|
2010-10-08 00:31:18 +08:00
|
|
|
{error, {invalid_destination, queue, "/foo/bar"}} =
|
2010-10-01 21:49:23 +08:00
|
|
|
parse_destination("/queue/foo/bar").
|
|
|
|
|
|
|
|
|
|
topic_with_invalid_name_test() ->
|
2010-10-08 00:31:18 +08:00
|
|
|
{error, {invalid_destination, topic, "/foo/bar"}} =
|
2010-10-01 21:49:23 +08:00
|
|
|
parse_destination("/topic/foo/bar").
|
|
|
|
|
|
|
|
|
|
exchange_with_invalid_name_test() ->
|
2010-10-08 00:31:18 +08:00
|
|
|
{error, {invalid_destination, exchange, "/foo/bar/baz"}} =
|
2010-10-01 21:49:23 +08:00
|
|
|
parse_destination("/exchange/foo/bar/baz").
|
|
|
|
|
|
|
|
|
|
unknown_destination_test() ->
|
2010-10-08 00:31:18 +08:00
|
|
|
{error, {unknown_destination, "/blah/boo"}} =
|
2010-10-05 20:36:12 +08:00
|
|
|
parse_destination("/blah/boo").
|
2010-10-01 20:41:04 +08:00
|
|
|
|
2010-10-18 20:29:51 +08:00
|
|
|
queue_with_escaped_name_test() ->
|
|
|
|
|
{ok, {queue, "te/st"}} = parse_destination("/queue/te%2Fst").
|
|
|
|
|
|
|
|
|
|
valid_exchange_with_escaped_name_and_pattern_test() ->
|
|
|
|
|
{ok, {exchange, {"te/st", "pa/tt/ern"}}} =
|
|
|
|
|
parse_destination("/exchange/te%2Fst/pa%2Ftt%2Fern").
|
|
|
|
|
|
2010-10-08 00:31:18 +08:00
|
|
|
parse_valid_message_id_test() ->
|
|
|
|
|
{ok, {<<"bar">>, "abc", 123}} =
|
|
|
|
|
rabbit_stomp_util:parse_message_id("bar@@abc@@123").
|
|
|
|
|
|
|
|
|
|
parse_invalid_message_id_test() ->
|
|
|
|
|
{error, invalid_message_id} =
|
|
|
|
|
rabbit_stomp_util:parse_message_id("blah").
|
|
|
|
|
|
2010-10-28 15:59:43 +08:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
%% Test Helpers
|
|
|
|
|
%%--------------------------------------------------------------------
|
2010-10-01 19:27:10 +08:00
|
|
|
parse_destination(Destination) ->
|
2010-10-08 00:31:18 +08:00
|
|
|
rabbit_stomp_util:parse_destination(Destination).
|