Skip to content

Commit

Permalink
Add support for specifying http method rather than defaulting to post.
Browse files Browse the repository at this point in the history
* ttt_call()
* in Gather scope
* in Redirect scope
  • Loading branch information
Jon Gilbraith committed Jan 30, 2013
1 parent e2d36a6 commit c8bfaf0
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 22 deletions.
16 changes: 8 additions & 8 deletions README.md
Expand Up @@ -47,9 +47,9 @@ TTT doesn't yet support (but you should contribute them!)
What's required What's required
================ ================


TTT depends on [Capybara](https://github.com/jnicklas/capybara). It uses Capybara's session object to POST requests to your controllers. TTT depends on [Capybara](https://github.com/jnicklas/capybara). It uses Capybara's session object to make requests to your controllers.


TTT expects your controller actions to behave like well-behaved Twilio callbacks. That is, you need to respond to XML-formatted requests, and need to respond with a 200 OK. TTT also requires that your controller actions be wired for POST, not GET (Twilio does support GET, but TTT lacks this support). Twilio will not follow 301 or 302 redirects properly, and neither will TTT (see below for more details). TTT expects your controller actions to behave like well-behaved Twilio callbacks. That is, you need to respond to XML-formatted requests, and need to respond with a 200 OK. Twilio will not follow 301 or 302 redirects properly, and neither will TTT (see below for more details).


TTT has only been tested with RSpec on Rails. It might work on other test frameworks or other Rack-based frameworks. Feel free to submit pull requests to improve compatibility with these. TTT has only been tested with RSpec on Rails. It might work on other test frameworks or other Rack-based frameworks. Feel free to submit pull requests to improve compatibility with these.


Expand Down Expand Up @@ -89,18 +89,19 @@ This section describes how to use TTT and its basic functionality.
ttt_call ttt_call
------------- -------------


The *ttt_call* method is the main entry point for working with TTT. You call this method to initiate a "Twilio phone call" to your controller actions. TTT simulates Twilio's behavior by POSTing to your action with the expected Twilio parameters (From, To, CallSid, etc). The *ttt_call* method is the main entry point for working with TTT. You call this method to initiate a "Twilio phone call" to your controller actions. TTT simulates Twilio's behavior by making requests to your action with the expected Twilio parameters (From, To, CallSid, etc).


*ttt_call* has three required parameters and an options hash: *ttt_call* has three required parameters and an options hash:


@call = ttt_call(action_path, from_number, to_number, options = {}) @call = ttt_call(action_path, from_number, to_number, options = {})


* **action_path**. Where to POST your request. It should be obvious by now, but whatever action you specify here should be a POST action. * **action_path**. Where to make your request. By default this will be a POST, but you can override it with the options hash.
* **from_number**. What to fill params[:From] with. If you don't care about this value, you can pass a blank one, as this is only used to pass along to your actions. * **from_number**. What to fill params[:From] with. If you don't care about this value, you can pass a blank one, as this is only used to pass along to your actions.
* **to_number**. What to fill params[:To] with. * **to_number**. What to fill params[:To] with.


Options are: Options are:


* **method** . Specify the http method of the initial request. By default this will be :post.
* **call_sid**. Specify an optional fixed value to be passed as params[:CallSid]. This is useful if you are expecting a specific SID. For instance, a common pattern is to initiate a call, store the SID in your database, and look up the call when you get the callback. If you don't pass a SID, TTT will generate one for you that's just a UUID. * **call_sid**. Specify an optional fixed value to be passed as params[:CallSid]. This is useful if you are expecting a specific SID. For instance, a common pattern is to initiate a call, store the SID in your database, and look up the call when you get the callback. If you don't pass a SID, TTT will generate one for you that's just a UUID.
* **is_machine**. Controls params[:AnsweredBy]. See Twilio's documentation for more information on how Twilio uses this. * **is_machine**. Controls params[:AnsweredBy]. See Twilio's documentation for more information on how Twilio uses this.


Expand Down Expand Up @@ -158,7 +159,7 @@ These methods are available on any CallScope.
Gathers Gathers
-------------- --------------


Gathers are used to collect digits from the caller (e.g. "press 1 to speak with a representative, press 2 to cancel"). Twilio handles Gathers by speaking the contents of the Gather and waiting for a digit or digits to be pressed. If nothing is pressed, it continues with the script. If something is pressed, it aborts processing the current script and POSTs the digits (via params[:Digits]) to the path specified by the action attribute. TTT handles Gathers in a similar way. Gathers are used to collect digits from the caller (e.g. "press 1 to speak with a representative, press 2 to cancel"). Twilio handles Gathers by speaking the contents of the Gather and waiting for a digit or digits to be pressed. If nothing is pressed, it continues with the script. If something is pressed, it aborts processing the current script and makes a request with the digits (via params[:Digits]) to the path specified by the action attribute. If no method is specified in the options, this will be a post TTT handles Gathers in a similar way.


You can only interact with a gather by calling *within_gather*: You can only interact with a gather by calling *within_gather*:


Expand All @@ -181,7 +182,7 @@ Within a gather CallScope, you can use the following methods:


You can also use other CallScope methods (e.g. *has_say?* and similar.) You can also use other CallScope methods (e.g. *has_say?* and similar.)


The *press* method has a few caveats worth knowing. It's only callable once per gather - when you call it, TTT will immediately POST to the gather's action. There is no way to simulate pressing buttons slowly, and you don't really need to do this anyways - Twilio doesn't care and just passes them all at once. *press* simply fills out the value of params[:Digits] and POSTs that to your method, just like Twilio does. The *press* method has a few caveats worth knowing. It's only callable once per gather - when you call it, TTT will immediately call the gather's action. There is no way to simulate pressing buttons slowly, and you don't really need to do this anyways - Twilio doesn't care and just passes them all at once. *press* simply fills out the value of params[:Digits] and calls your method, just like Twilio does.


Although you can technically pass whatever you want to *press*, in practice Twilio only sends digits and #. Still it's probably a good idea to test garbage data in this parameter with your actions, so TTT doesn't get in your way if you want to call press with "UNICORNSANDPONIES" as a parameter. Although you can technically pass whatever you want to *press*, in practice Twilio only sends digits and #. Still it's probably a good idea to test garbage data in this parameter with your actions, so TTT doesn't get in your way if you want to call press with "UNICORNSANDPONIES" as a parameter.


Expand All @@ -190,7 +191,7 @@ TTT doesn't attempt to validate your TwiML, so it's worth knowing that Gather on
Redirects Redirects
-------------- --------------


The Redirect element is used to tell Twilio to POST to a different page. It differs from a standard 301 or 302 redirect (created by a *redirect_to*) in that the 301/302 redirects don't support a POST, and if you try a *redirect_to* within a Twilio action, Twilio will fail and your caller will get the dreaded "I'm sorry, an application error has occurred" message. Because Twilio doesn't support 301/302 redirects, TTT doesn't either, and if you use one, TTT will complain. The Redirect element is used to tell Twilio to call a different page. It differs from a standard 301 or 302 redirect (created by a *redirect_to*) in that the 301/302 redirects don't support a POST, and if you try a *redirect_to* within a Twilio action, Twilio will fail and your caller will get the dreaded "I'm sorry, an application error has occurred" message. Because Twilio doesn't support 301/302 redirects, TTT doesn't either, and if you use one, TTT will complain.


There are several methods you can use within a CallScope related to redirects: There are several methods you can use within a CallScope related to redirects:


Expand All @@ -210,7 +211,6 @@ Contributing
TTT is pretty basic, but it should work for most people's needs. You might consider helping to improve it. Some things that could be done: TTT is pretty basic, but it should work for most people's needs. You might consider helping to improve it. Some things that could be done:


* Support more Twilio functionality * Support more Twilio functionality
* Support GET actions. Twilio technically supports this, although it doesn't seem to be used very often.
* Build more stringent checks into says and plays - e.g. verify that there's enough of a pause between sentences. * Build more stringent checks into says and plays - e.g. verify that there's enough of a pause between sentences.
* Build checks to make sure that numbers, dates, and other special text is spoken properly (e.g. "one two three" instead of "one hundred twenty three"). * Build checks to make sure that numbers, dates, and other special text is spoken properly (e.g. "one two three" instead of "one hundred twenty three").
* Build checks to validate the correctness of your TwiML. * Build checks to validate the correctness of your TwiML.
Expand Down
10 changes: 8 additions & 2 deletions lib/twilio-test-toolkit/call_in_progress.rb
Expand Up @@ -4,6 +4,7 @@ module TwilioTestToolkit
# Models a call # Models a call
class CallInProgress < CallScope class CallInProgress < CallScope
# Initiate a call. Options: # Initiate a call. Options:
# * :method - specify the http method of the initial api call
# * :call_sid - specify an optional fixed value to be passed as params[:CallSid] # * :call_sid - specify an optional fixed value to be passed as params[:CallSid]
# * :is_machine - controls params[:AnsweredBy] # * :is_machine - controls params[:AnsweredBy]
def initialize(initial_path, from_number, to_number, options = {}) def initialize(initial_path, from_number, to_number, options = {})
Expand All @@ -12,6 +13,7 @@ def initialize(initial_path, from_number, to_number, options = {})
@from_number = from_number @from_number = from_number
@to_number = to_number @to_number = to_number
@is_machine = options[:is_machine] @is_machine = options[:is_machine]
@method = options[:method] || :post


# Generate an initial call SID if we don't have one # Generate an initial call SID if we don't have one
if (options[:call_sid].nil?) if (options[:call_sid].nil?)
Expand All @@ -24,7 +26,7 @@ def initialize(initial_path, from_number, to_number, options = {})
self.root_call = self self.root_call = self


# Create the request # Create the request
post_for_twiml!(@initial_path, :is_machine => @is_machine) request_for_twiml!(@initial_path, :method => @method, :is_machine => @is_machine)
end end


def sid def sid
Expand All @@ -46,5 +48,9 @@ def to_number
def is_machine def is_machine
@is_machine @is_machine
end end

def method
@method
end
end end
end end
23 changes: 15 additions & 8 deletions lib/twilio-test-toolkit/call_scope.rb
Expand Up @@ -12,14 +12,14 @@ def follow_redirect
el = get_redirect_node el = get_redirect_node
raise "No redirect" if el.nil? raise "No redirect" if el.nil?


return CallScope.from_post(self, el.text) return CallScope.from_request(self, el.text, :method =>el[:method])
end end


def follow_redirect! def follow_redirect!
el = get_redirect_node el = get_redirect_node
raise "No redirect" if el.nil? raise "No redirect" if el.nil?


post_for_twiml!(normalize_redirect_path(el.text)) request_for_twiml!(normalize_redirect_path(el.text), :method => el[:method])
end end


# Stuff for Says # Stuff for Says
Expand Down Expand Up @@ -70,14 +70,19 @@ def gather_action
return @xml["action"] return @xml["action"]
end end


def gather_method
rasie "Not a gather" unless gather?
return @xml["method"]
end

def press(digits) def press(digits)
raise "Not a gather" unless gather? raise "Not a gather" unless gather?


# Fetch the path and then post # Fetch the path and then post
path = gather_action path = gather_action


# Update the root call # Update the root call
root_call.post_for_twiml!(path, :digits => digits) root_call.request_for_twiml!(path, :digits => digits, :method => gather_method)
end end


# Some basic accessors # Some basic accessors
Expand Down Expand Up @@ -115,11 +120,13 @@ def set_xml(xml)
@xml = xml @xml = xml
end end


# Create a new object from a post # Create a new object from a post. Options:
def self.from_post(parent, path, digits = "") # * :method - the http method of the request, defaults to :post
# * :digits - becomes params[:Digits], defaults to ""
def self.from_request(parent, path, options = {})
new_scope = CallScope.new new_scope = CallScope.new
new_scope.send(:root_call=, parent.root_call) new_scope.send(:root_call=, parent.root_call)
new_scope.send(:post_for_twiml!, path, :digits => digits) new_scope.send(:request_for_twiml!, path, :digits => options[:digits] || "", :method => options[:method] || :post)
return new_scope return new_scope
end end


Expand All @@ -134,12 +141,12 @@ def normalize_redirect_path(path)
# Post and update the scope. Options: # Post and update the scope. Options:
# :digits - becomes params[:Digits], optional (becomes "") # :digits - becomes params[:Digits], optional (becomes "")
# :is_machine - becomes params[:AnsweredBy], defaults to false / human # :is_machine - becomes params[:AnsweredBy], defaults to false / human
def post_for_twiml!(path, options = {}) def request_for_twiml!(path, options = {})
@current_path = normalize_redirect_path(path) @current_path = normalize_redirect_path(path)


# Post the query # Post the query
rack_test_session_wrapper = Capybara.current_session.driver rack_test_session_wrapper = Capybara.current_session.driver
@response = rack_test_session_wrapper.post(@current_path, @response = rack_test_session_wrapper.send(options[:method] || :post, @current_path,
:format => :xml, :format => :xml,
:CallSid => @root_call.sid, :CallSid => @root_call.sid,
:From => @root_call.from_number, :From => @root_call.from_number,
Expand Down
1 change: 1 addition & 0 deletions lib/twilio-test-toolkit/dsl.rb
Expand Up @@ -4,6 +4,7 @@ module TwilioTestToolkit
# Adds the `ttt_call` method to the top-level namespace. # Adds the `ttt_call` method to the top-level namespace.
module DSL module DSL
# Initiate a call. Options: # Initiate a call. Options:
# * :method - specify the http method of the initial api call
# * :call_sid - specify an optional fixed value to be passed as params[:CallSid] # * :call_sid - specify an optional fixed value to be passed as params[:CallSid]
# * :is_machine - controls params[:AnsweredBy] # * :is_machine - controls params[:AnsweredBy]
def ttt_call(initial_path, from_number, to_number, options = {}) def ttt_call(initial_path, from_number, to_number, options = {})
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/views/twilio/test_redirect.xml.erb
@@ -1 +1 @@
<Redirect><%= test_start_twilio_index_path %></Redirect> <Redirect method="post"><%= test_start_twilio_index_path %></Redirect>
2 changes: 1 addition & 1 deletion spec/dummy/app/views/twilio/test_start.xml.erb
@@ -1,5 +1,5 @@
<Say voice="woman">Hi there.</Say> <Say voice="woman">Hi there.</Say>


<Gather action="<%= test_action_twilio_index_path %>"> <Gather action="<%= test_action_twilio_index_path %>" method="post">
<Say>Please enter some digits.</Say> <Say>Please enter some digits.</Say>
</Gather> </Gather>
1 change: 1 addition & 0 deletions spec/dummy/config/routes.rb
@@ -1,5 +1,6 @@
Dummy::Application.routes.draw do Dummy::Application.routes.draw do
resources :twilio do resources :twilio do
get "test_start", :on => :collection
post "test_start", :on => :collection post "test_start", :on => :collection
post "test_action", :on => :collection post "test_action", :on => :collection


Expand Down
12 changes: 10 additions & 2 deletions spec/requests/dsl_spec.rb
Expand Up @@ -20,6 +20,10 @@
@call.sid.should_not be_blank @call.sid.should_not be_blank
end end


it "should default the method to post" do
@call.method.should == :post
end

it "should have the right properties" do it "should have the right properties" do
@call.initial_path.should == test_start_twilio_index_path @call.initial_path.should == test_start_twilio_index_path
@call.from_number.should == @our_number @call.from_number.should == @our_number
Expand All @@ -28,10 +32,10 @@
end end
end end


describe "with a sid and machine override" do describe "with a sid, method and machine override" do
before(:each) do before(:each) do
@mysid = "1234567" @mysid = "1234567"
@call = ttt_call(test_start_twilio_index_path, @our_number, @their_number, :call_sid => @mysid, :is_machine => true) @call = ttt_call(test_start_twilio_index_path, @our_number, @their_number, :call_sid => @mysid, :is_machine => true, :method => :get)
end end


it "should have the right sid" do it "should have the right sid" do
Expand All @@ -41,6 +45,10 @@
it "should be a machine call" do it "should be a machine call" do
@call.is_machine.should be_true @call.is_machine.should be_true
end end

it "should be a get call" do
@call.method.should == :get
end
end end
end end
end end

0 comments on commit c8bfaf0

Please sign in to comment.