require 'spec_helper'
describe Twilio::TwiML::VoiceResponse do
context 'Testing Response' do
it 'should allow empty response' do
expected_doc = parse <<-XML
XML
response = Twilio::TwiML::VoiceResponse.new.to_s
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow using to_xml instead of to_s' do
expected_doc = parse <<-XML
XML
response = Twilio::TwiML::VoiceResponse.new.to_xml
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should have an xml declaration by default' do
twiml = Twilio::TwiML::VoiceResponse.new.to_xml
expect(twiml).to match(/\A<\?xml version="1.0" encoding="UTF-8"\?>/)
end
it 'should allow no xml_declaration' do
twiml = Twilio::TwiML::VoiceResponse.new.to_xml(false)
expect(twiml).not_to match(/\A<\?xml version="1.0" encoding="UTF-8"\?>/)
end
it 'should allow comments' do
twiml = Twilio::TwiML::VoiceResponse.new
twiml.comment 'This is awesome'
expect(twiml.to_xml).to match(//)
end
it 'should allow text nodes' do
expected_doc = parse <<-XML
Look no tags
XML
twiml = Twilio::TwiML::VoiceResponse.new
twiml.add_text 'Look no tags'
expect(parse(twiml)).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow generic child nodes' do
expected_doc = parse <<-XML
Kindle
XML
twiml = Twilio::TwiML::VoiceResponse.new
twiml.add_child('alexa', omnipresent: 'true') do |alexa|
alexa.add_child('purchase', 'Kindle')
end
expect(parse(twiml)).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow populated response' do
expected_doc = parse <<-XML
twilio sms
XML
response = Twilio::TwiML::VoiceResponse.new
response.hangup
response.leave
response.sms('twilio sms', to: '+11234567890', from: '+10987654321')
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow chaining' do
expected_doc = parse <<-XML
twilio sms
XML
response = Twilio::TwiML::VoiceResponse.new.hangup.leave.sms(
'twilio sms',
to: '+11234567890',
from: '+10987654321'
)
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow nesting' do
expected_doc = parse <<-XML
twilio sms
XML
response = Twilio::TwiML::VoiceResponse.new do |r|
r.hangup
r.leave
r.sms('twilio sms', to: '+11234567890', from: '+10987654321')
end
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
end
context 'Testing Say' do
it 'should allow empty say' do
expected_doc = parse <<-XML
XML
response = Twilio::TwiML::VoiceResponse.new
response.say(message: '')
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow populated say' do
expected_doc = parse <<-XML
Hello World
XML
response = Twilio::TwiML::VoiceResponse.new
response.say(message: 'Hello World')
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow unicode say' do
expected_doc = parse <<-XML
nécessaire et d'autres
XML
response = Twilio::TwiML::VoiceResponse.new
response.say(message: 'nécessaire et d\'autres')
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow looping' do
expected_doc = parse <<-XML
Hello Monkey
XML
response = Twilio::TwiML::VoiceResponse.new
response.say(message: 'Hello Monkey', loop: 3)
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow languages' do
expected_doc = parse <<-XML
Hello Monkey
XML
response = Twilio::TwiML::VoiceResponse.new
response.say(message: 'Hello Monkey', language: 'en-gb')
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow all attributes' do
expected_doc = parse <<-XML
Hello Monkey
XML
response = Twilio::TwiML::VoiceResponse.new
response.say(message: 'Hello Monkey', loop: 3, voice: 'man', language: 'fr')
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
end
context 'Testing Play' do
it 'should allow empty play' do
expected_doc = parse <<-XML
XML
response = Twilio::TwiML::VoiceResponse.new
response.play
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should play hello' do
expected_doc = parse <<-XML
http://hellomonkey.mp3
XML
response = Twilio::TwiML::VoiceResponse.new
response.play(url: 'http://hellomonkey.mp3')
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should play hello on loop' do
expected_doc = parse <<-XML
http://hellomonkey.mp3
XML
response = Twilio::TwiML::VoiceResponse.new
response.play(url: 'http://hellomonkey.mp3', loop: 3)
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should play digits' do
expected_doc = parse <<-XML
XML
response = Twilio::TwiML::VoiceResponse.new
response.play(digits: 'w123')
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
end
context 'Testing Record' do
it 'should allow empty record' do
expected_doc = parse <<-XML
XML
response = Twilio::TwiML::VoiceResponse.new
response.record
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow action and method' do
expected_doc = parse <<-XML
XML
response = Twilio::TwiML::VoiceResponse.new
response.record(action: 'example.com', method: 'GET')
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow max_length, finish_on_key, and timeout' do
expected_doc = parse <<-XML
XML
response = Twilio::TwiML::VoiceResponse.new
response.record(timeout: 4, finish_on_key: '#', max_length: 30)
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow transcribe' do
expected_doc = parse <<-XML
XML
response = Twilio::TwiML::VoiceResponse.new
response.record(transcribe_callback: 'example.com')
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
end
context 'Testing Redirect' do
it 'should allow empty redirect' do
expected_doc = parse <<-XML
XML
response = Twilio::TwiML::VoiceResponse.new
response.redirect ''
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow method' do
expected_doc = parse <<-XML
example.com
XML
response = Twilio::TwiML::VoiceResponse.new
response.redirect('example.com', method: 'POST')
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow method and params' do
expected_doc = parse <<-XML
example.com?id=34&action=hey
XML
response = Twilio::TwiML::VoiceResponse.new
response.redirect('example.com?id=34&action=hey', method: 'POST')
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
end
context 'Testing Hangup' do
it 'should allow hangup' do
expected_doc = parse <<-XML
XML
response = Twilio::TwiML::VoiceResponse.new
response.hangup
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should not allow content in the hangup' do
response = Twilio::TwiML::VoiceResponse.new
expect { response.hangup 'Goodbye' }.to raise_error(ArgumentError)
end
end
context 'Testing Leave' do
it 'should allow leave' do
expected_doc = parse <<-XML
XML
response = Twilio::TwiML::VoiceResponse.new
response.leave
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should not allow content in the leave' do
response = Twilio::TwiML::VoiceResponse.new
expect { response.leave 'Goodbye' }.to raise_error(ArgumentError)
end
end
context 'Testing Reject' do
it 'should reject with a default reason' do
expected_doc = parse <<-XML
XML
response = Twilio::TwiML::VoiceResponse.new
response.reject
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should not allow content in the reject' do
response = Twilio::TwiML::VoiceResponse.new
expect { response.reject 'Goodbye' }.to raise_error(ArgumentError)
end
end
context 'Testing SMS' do
it 'should allow empty SMS' do
expected_doc = parse <<-XML
XML
response = Twilio::TwiML::VoiceResponse.new
response.sms ''
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow SMS body' do
expected_doc = parse <<-XML
Hello, World
XML
response = Twilio::TwiML::VoiceResponse.new
response.sms 'Hello, World'
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow to, from, action, and status_callback' do
expected_doc = parse <<-XML
Hello, World
XML
response = Twilio::TwiML::VoiceResponse.new
response.sms('Hello, World', to: 1_231_231_234, from: 3_453_453_456, status_callback: 'example.com?id=34&action=hey')
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow action and method' do
expected_doc = parse <<-XML
Hello
XML
response = Twilio::TwiML::VoiceResponse.new
response.sms('Hello', method: 'POST', action: 'example.com?id=34&action=hey')
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
end
context 'Testing Conference' do
it 'should allow basic conferencing' do
expected_doc = parse <<-XML
TestConferenceAttributes
XML
dial = Twilio::TwiML::Dial.new
dial.conference(
'TestConferenceAttributes',
beep: false,
wait_url: '',
start_conference_on_enter: true,
end_conference_on_exit: true
)
response = Twilio::TwiML::VoiceResponse.new
response.append(dial)
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow muted conferencing' do
expected_doc = parse <<-XML
TestConferenceMutedAttribute
XML
dial = Twilio::TwiML::Dial.new
dial.conference(
'TestConferenceMutedAttribute',
beep: false,
muted: true,
wait_url: '',
start_conference_on_enter: true,
end_conference_on_exit: true
)
response = Twilio::TwiML::VoiceResponse.new
response.append(dial)
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
end
context 'Testing Queue' do
it 'should allow queues in VoiceResponse' do
expected_doc = parse <<-XML
TestQueueAttribute
XML
dial = Twilio::TwiML::Dial.new
dial.queue('TestQueueAttribute', url: '', method: 'GET')
response = Twilio::TwiML::VoiceResponse.new
response.append(dial)
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
end
context 'Testing Echo' do
it 'should allow VoiceResponse Echo' do
expected_doc = parse <<-XML
XML
response = Twilio::TwiML::VoiceResponse.new
response.echo
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
end
context 'Testing Enqueue' do
it 'should allow VoiceResponse.enqueue' do
expected_doc = parse <<-XML
TestEnqueueAttribute
XML
response = Twilio::TwiML::VoiceResponse.new
response.enqueue(
name: 'TestEnqueueAttribute',
action: 'act',
method: 'GET',
wait_url: 'wait',
wait_url_method: 'POST'
)
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow Enqueue.task and appending Enqueue' do
task_json = '{"account_sid": "AC123123123"}'
expected_doc = parse <<-XML
#{task_json}
XML
enqueue_elem = Twilio::TwiML::Enqueue.new(name: nil, workflow_sid: '123123123')
enqueue_elem.task(task_json)
response = Twilio::TwiML::VoiceResponse.new
response.append(enqueue_elem)
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow Enqueue.task with a dictionary and appending Enqueue' do
expected_doc = parse <<-XML
{"account_sid":"AC123123123"}
XML
enqueue_elem = Twilio::TwiML::Enqueue.new(name: nil, workflow_sid: '123123123')
enqueue_elem.task(account_sid: 'AC123123123')
response = Twilio::TwiML::VoiceResponse.new
response.append(enqueue_elem)
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
end
context 'Testing Dial' do
it 'should allow VoiceResponse.dial' do
expected_doc = parse <<-XML
1231231234
XML
response = Twilio::TwiML::VoiceResponse.new
response.dial(number: '1231231234')
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow Dial.sim' do
expected_doc = parse <<-XML
123123123
XML
dial = Twilio::TwiML::Dial.new
dial.sim '123123123'
response = Twilio::TwiML::VoiceResponse.new
response.append(dial)
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow Dial.sip' do
expected_doc = parse <<-XML
foo@example.com
XML
dial = Twilio::TwiML::Dial.new
dial.sip 'foo@example.com'
response = Twilio::TwiML::VoiceResponse.new
response.append(dial)
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow Dial.sip with username, password' do
expected_doc = parse <<-XML
foo@example.com
XML
dial = Twilio::TwiML::Dial.new
dial.sip('foo@example.com', username: 'foo', password: 'bar')
response = Twilio::TwiML::VoiceResponse.new
response.append(dial)
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow Dial.number' do
expected_doc = parse <<-XML
1231231234
XML
dial = Twilio::TwiML::Dial.new
dial.number '1231231234'
response = Twilio::TwiML::VoiceResponse.new
response.append(dial)
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow Dial.number with params' do
expected_doc = parse <<-XML
1231231234
XML
dial = Twilio::TwiML::Dial.new
dial.number('1231231234', status_callback: 'http://example.com', status_callback_event: 'initiated completed')
response = Twilio::TwiML::VoiceResponse.new
response.append(dial)
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow Dial.conference' do
expected_doc = parse <<-XML
My Room
XML
dial = Twilio::TwiML::Dial.new
dial.conference 'My Room'
response = Twilio::TwiML::VoiceResponse.new
response.append(dial)
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow Dial.queue' do
expected_doc = parse <<-XML
The Cute Queue
XML
dial = Twilio::TwiML::Dial.new
dial.queue 'The Cute Queue'
response = Twilio::TwiML::VoiceResponse.new
response.append(dial)
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow empty Dial.client' do
expected_doc = parse <<-XML
XML
dial = Twilio::TwiML::Dial.new
dial.client ''
response = Twilio::TwiML::VoiceResponse.new
response.append(dial)
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow populated Dial.client' do
expected_doc = parse <<-XML
alice
XML
dial = Twilio::TwiML::Dial.new
dial.client 'alice'
response = Twilio::TwiML::VoiceResponse.new
response.append(dial)
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
end
context 'Testing Gather' do
it 'should allow empty Gather' do
expected_doc = parse <<-XML
XML
response = Twilio::TwiML::VoiceResponse.new
response.gather
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow nested Say in Gather' do
expected_doc = parse <<-XML
Hello
XML
gather_elem = Twilio::TwiML::Gather.new
gather_elem.say(message: 'Hello')
response = Twilio::TwiML::VoiceResponse.new
response.append(gather_elem)
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
it 'should allow nested play and pause' do
expected_doc = parse <<-XML
Hey
hey.mp3
XML
gather = Twilio::TwiML::Gather.new
gather.say(message: 'Hey')
gather.play(url: 'hey.mp3')
gather.pause
response = Twilio::TwiML::VoiceResponse.new
response.append(gather)
doc = parse(response)
expect(doc).to be_equivalent_to(expected_doc).respecting_element_order
end
end
end