spec/mqtt_client_spec.rb in mqtt-0.0.7 vs spec/mqtt_client_spec.rb in mqtt-0.0.8
- old
+ new
@@ -60,11 +60,10 @@
'Unsupported number of arguments'
)
end
end
-
describe "when calling the 'connect' method" do
before(:each) do
TCPSocket.stubs(:new).returns(@socket)
Thread.stubs(:new)
@client.stubs(:receive_connack)
@@ -123,10 +122,43 @@
@client.client_id = nil
@client.clean_session = false
@client.connect
@client.clean_session.should be_true
end
+
+ context "with a last will and testament set" do
+ before(:each) do
+ @client.set_will('topic', 'hello', retain=false, qos=1)
+ end
+
+ it "should have set the Will's topic" do
+ @client.will_topic.should == 'topic'
+ end
+
+ it "should have set the Will's payload" do
+ @client.will_payload.should == 'hello'
+ end
+
+ it "should have set the Will's retain flag to true" do
+ @client.will_retain.should be_false
+ end
+
+ it "should have set the Will's retain QOS value to 1" do
+ @client.will_qos.should == 1
+ end
+
+ it "should include the will in the CONNECT message" do
+ @client.connect('myclient')
+ @socket.string.should ==
+ "\x10\x24"+
+ "\x00\x06MQIsdp"+
+ "\x03\x0e\x00\x0f"+
+ "\x00\x08myclient"+
+ "\x00\x05topic\x00\x05hello"
+ end
+ end
+
end
describe "when calling the 'receive_connack' method" do
before(:each) do
@client.instance_variable_set(:@socket, @socket)
@@ -348,9 +380,46 @@
it "should pass exceptions up to parent thread" do
@parent_thread.expects(:raise).once
MQTT::Packet.stubs(:read).raises(MQTT::Exception)
@client.send(:receive_packet)
end
+ end
+ describe "generating a client identifier" do
+ context "with default parameters" do
+ before :all do
+ @client_id = MQTT::Client.generate_client_id
+ end
+
+ it "should be less or equal to 23 characters long" do
+ @client_id.length.should <= 23
+ end
+
+ it "should have a prefix of ruby_" do
+ @client_id.should match(/^ruby_/)
+ end
+
+ it "should end in 16 characters of lowercase letters and numbers" do
+ @client_id.should match(/_[a-z0-9]{16}$/)
+ end
+ end
+
+ context "with an alternative prefix" do
+ before :all do
+ @client_id = MQTT::Client.generate_client_id('test_')
+ end
+
+ it "should be less or equal to 23 characters long" do
+ @client_id.length.should <= 23
+ end
+
+ it "should have a prefix of test_" do
+ @client_id.should match(/^test_/)
+ end
+
+ it "should end in 16 characters of lowercase letters and numbers" do
+ @client_id.should match(/_[a-z0-9]{16}$/)
+ end
+ end
end
end