/
action_spec.rb
187 lines (146 loc) · 5.2 KB
/
action_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
require 'spec_helper'
module RubyFS
describe Action do
let(:name) { 'foobar' }
let(:headers) { {'foo' => 'bar'} }
subject do
Action.new name, headers do |response|
@foo = response
end
end
it { should be_new }
describe "SIPPeers actions" do
subject { Action.new('SIPPeers') }
its(:has_causal_events?) { should be true }
end
describe "Queues actions" do
subject { Action.new('Queues') }
its(:replies_with_action_id?) { should == false }
end
describe "IAXPeers actions" do
before { pending }
# FIXME: This test relies on the side effect that earlier tests have run
# and initialized the UnsupportedActionName::UNSUPPORTED_ACTION_NAMES
# constant for an "unknown" version of Asterisk. This should be fixed
# to be more specific about which version of Asterisk is under test.
# IAXPeers is supported (with Action IDs!) since Asterisk 1.8
subject { Action.new('IAXPeers') }
its(:replies_with_action_id?) { should == false }
end
describe "the ParkedCalls terminator event" do
subject { Action.new('ParkedCalls') }
its(:causal_event_terminator_name) { should == "parkedcallscomplete" }
end
it "should properly convert itself into a String when additional headers are given" do
string = Action.new("Hawtsawce", "Monkey" => "Zoo").to_s
string.should =~ /^Action: Hawtsawce\r\n/i
string.should =~ /[^\n]\r\n\r\n$/
string.should =~ /^(\w+:\s*[\w-]+\r\n){3}\r\n$/
end
it "should properly convert itself into a String when no additional headers are given" do
Action.new("Ping").to_s.should =~ /^Action: Ping\r\nActionID: [\w-]+\r\n\r\n$/i
Action.new("ParkedCalls").to_s.should =~ /^Action: ParkedCalls\r\nActionID: [\w-]+\r\n\r\n$/i
end
it 'should be able to be marked as sent' do
subject.state = :sent
subject.should be_sent
end
it 'should be able to be marked as complete' do
subject.state = :complete
subject.should be_complete
end
describe '#<<' do
describe 'for a non-causal action' do
context 'with a response' do
let(:response) { Response.new }
it 'should set the response' do
subject << response
subject.response.should be response
end
end
context 'with an error' do
let(:error) { Error.new.tap { |e| e.message = 'AMI error' } }
it 'should set the response and raise the error when reading it' do
subject << error
lambda { subject.response }.should raise_error Error, 'AMI error'
end
end
context 'with an event' do
it 'should raise an error' do
lambda { subject << Event.new('foo') }.should raise_error StandardError, /causal action/
end
end
end
describe 'for a causal action' do
let(:name) { 'Status' }
context 'with a response' do
let(:message) { Response.new }
before { subject << message }
it { should_not be_complete }
end
context 'with an event' do
let(:event) { Event.new 'foo' }
before { subject << event }
its(:events) { should == [event] }
end
context 'with a terminating event' do
let(:response) { Response.new }
let(:event) { Event.new 'StatusComplete' }
before do
subject << response
subject.should_not be_complete
subject << event
end
its(:events) { should == [event] }
it { should be_complete }
its(:response) { should be response }
end
end
end
describe 'setting the response' do
let(:response) { :bar }
before { subject.response = response }
it { should be_complete }
its(:response) { should == response }
it 'should call the response callback with the response' do
@foo.should == response
end
end
describe 'comparison' do
describe 'with another Action' do
context 'with identical name and headers' do
let(:other) { Action.new name, headers }
it { should == other }
end
context 'with identical name and different headers' do
let(:other) { Action.new name, 'boo' => 'baz' }
it { should_not == other }
end
context 'with different name and identical headers' do
let(:other) { Action.new 'BARBAZ', headers }
it { should_not == other }
end
end
it { should_not == :foo }
end
describe "#sync_timeout" do
it "should be 10 seconds" do
subject.sync_timeout.should be == 10
end
context "for an asynchronous Originate" do
let(:name) { 'Originate' }
let(:headers) { {:async => true} }
it "should be 60 seconds" do
subject.sync_timeout.should be == 10
end
end
context "for a synchronous Originate" do
let(:name) { 'Originate' }
let(:headers) { {:async => false} }
it "should be 60 seconds" do
subject.sync_timeout.should be == 60
end
end
end
end # Action
end # RubyFS