/
neighborparrot_spec.rb
108 lines (90 loc) · 2.72 KB
/
neighborparrot_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Neighborparrot" do
before :each do
@key = 'test_key'
@parrot = Neighborparrot.new(@key)
end
describe "Neigborparrot#post" do
it "should rails exception when instantiate without key" do
expect { Neighborparrot.new() }.to raise_error
end
it "should rails exception when nil key" do
expect { Neighborparrot.new(nil) }.to raise_error
end
it "should rails exception when empty key" do
expect { Neighborparrot.new('') }.to raise_error
end
it "should send post request with valid parameters" do
expec_params = { :key => @key, :channel => 'test', :data => 'test string' }
url = URI.parse('http://neighborparrot.net/post')
Net::HTTP.should_receive(:post_form).with(url, expec_params)
@parrot.post(expec_params[:channel], expec_params[:data])
end
it "should raise exception with nill channel" do
expect { @parrot.post(nil, 'test string') }.to raise_error
end
it "should raise exception with empty channel" do
expect { @parrot.post('', 'test string') }.to raise_error
end
it "should not send message with nil data" do
Net::HTTP.should_not_receive(:post_form)
@parrot.post('test-channel', nil)
end
it "should not send message with empty data" do
Net::HTTP.should_not_receive(:post_form)
@parrot.post('test-channel', '')
end
# TODO
it "should raise exception if can't send the request"
end
describe "Neigborparrot#open" do
after :each do
@parrot.close
end
it "should open a connection with correct values" do
connected = false
@parrot.on_connect do
connected = true
end
@parrot.open('test')
sleep(2)
connected.should be_true
end
it "should receive messages" do
received = nil
@parrot.on_message do |msg|
received = msg
end
@parrot.open('test')
sleep(2)
@parrot.post('test', 'message')
sleep(1)
received.should eq 'message'
end
it "should return false if already a connection active" do
@parrot.open('test')
sleep(2)
@parrot.open('other test').should be_false
end
end
describe "Neigborparrot#close" do
it "should close a connection" do
@parrot.open('test')
sleep(2)
@parrot.close()
@parrot.connected?.should be_false
end
end
describe "Neigborparrot#connected?" do
it "should be false before connected" do
@parrot.connected?.should be_false
@parrot.close
end
it "should be true when connected" do
@parrot.open('test')
sleep(2)
@parrot.connected?.should be_true
@parrot.close
end
end
end