Permalink
Browse files

Add support for specifying http method rather than defaulting to post.

* ttt_call()
* in Gather scope
* in Redirect scope
  • Loading branch information...
1 parent e2d36a6 commit c8bfaf09a623b786c5d70357fc6c6a986e521926 Jon Gilbraith committed Jan 30, 2013
View
@@ -47,9 +47,9 @@ TTT doesn't yet support (but you should contribute them!)
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.
@@ -89,18 +89,19 @@ This section describes how to use TTT and its basic functionality.
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:
@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.
* **to_number**. What to fill params[:To] with.
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.
* **is_machine**. Controls params[:AnsweredBy]. See Twilio's documentation for more information on how Twilio uses this.
@@ -158,7 +159,7 @@ These methods are available on any CallScope.
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*:
@@ -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.)
-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.
@@ -190,7 +191,7 @@ TTT doesn't attempt to validate your TwiML, so it's worth knowing that Gather on
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:
@@ -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:
* 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 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.
@@ -4,6 +4,7 @@ module TwilioTestToolkit
# Models a call
class CallInProgress < CallScope
# 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]
# * :is_machine - controls params[:AnsweredBy]
def initialize(initial_path, from_number, to_number, options = {})
@@ -12,6 +13,7 @@ def initialize(initial_path, from_number, to_number, options = {})
@from_number = from_number
@to_number = to_number
@is_machine = options[:is_machine]
+ @method = options[:method] || :post
# Generate an initial call SID if we don't have one
if (options[:call_sid].nil?)
@@ -24,7 +26,7 @@ def initialize(initial_path, from_number, to_number, options = {})
self.root_call = self
# Create the request
- post_for_twiml!(@initial_path, :is_machine => @is_machine)
+ request_for_twiml!(@initial_path, :method => @method, :is_machine => @is_machine)
end
def sid
@@ -46,5 +48,9 @@ def to_number
def is_machine
@is_machine
end
+
+ def method
+ @method
+ end
end
-end
+end
@@ -12,14 +12,14 @@ def follow_redirect
el = get_redirect_node
raise "No redirect" if el.nil?
- return CallScope.from_post(self, el.text)
+ return CallScope.from_request(self, el.text, :method =>el[:method])
end
def follow_redirect!
el = get_redirect_node
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
# Stuff for Says
@@ -70,14 +70,19 @@ def gather_action
return @xml["action"]
end
+ def gather_method
+ rasie "Not a gather" unless gather?
+ return @xml["method"]
+ end
+
def press(digits)
raise "Not a gather" unless gather?
# Fetch the path and then post
path = gather_action
# Update the root call
- root_call.post_for_twiml!(path, :digits => digits)
+ root_call.request_for_twiml!(path, :digits => digits, :method => gather_method)
end
# Some basic accessors
@@ -115,11 +120,13 @@ def set_xml(xml)
@xml = xml
end
- # Create a new object from a post
- def self.from_post(parent, path, digits = "")
+ # Create a new object from a post. Options:
+ # * :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.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
end
@@ -134,12 +141,12 @@ def normalize_redirect_path(path)
# Post and update the scope. Options:
# :digits - becomes params[:Digits], optional (becomes "")
# :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)
# Post the query
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,
:CallSid => @root_call.sid,
:From => @root_call.from_number,
@@ -4,6 +4,7 @@ module TwilioTestToolkit
# Adds the `ttt_call` method to the top-level namespace.
module DSL
# 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]
# * :is_machine - controls params[:AnsweredBy]
def ttt_call(initial_path, from_number, to_number, options = {})
@@ -1 +1 @@
-<Redirect><%= test_start_twilio_index_path %></Redirect>
+<Redirect method="post"><%= test_start_twilio_index_path %></Redirect>
@@ -1,5 +1,5 @@
<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>
</Gather>
@@ -1,5 +1,6 @@
Dummy::Application.routes.draw do
resources :twilio do
+ get "test_start", :on => :collection
post "test_start", :on => :collection
post "test_action", :on => :collection
View
@@ -20,6 +20,10 @@
@call.sid.should_not be_blank
end
+ it "should default the method to post" do
+ @call.method.should == :post
+ end
+
it "should have the right properties" do
@call.initial_path.should == test_start_twilio_index_path
@call.from_number.should == @our_number
@@ -28,10 +32,10 @@
end
end
- describe "with a sid and machine override" do
+ describe "with a sid, method and machine override" do
before(:each) do
@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
it "should have the right sid" do
@@ -41,6 +45,10 @@
it "should be a machine call" do
@call.is_machine.should be_true
end
+
+ it "should be a get call" do
+ @call.method.should == :get
+ end
end
end
end

0 comments on commit c8bfaf0

Please sign in to comment.