Browse files

FakeWeb is now HTTP method-aware -- make Google auth specs use it

  • Loading branch information...
1 parent fca26cf commit 1c5f87e4648d286b591c9111d29cab30ebfd6855 @mislav committed Oct 8, 2008
Showing with 38 additions and 16 deletions.
  1. +2 −1 spec/fake_net_http.rb
  2. +11 −0 spec/fake_web.rb
  3. +24 −14 spec/gmail/auth_spec.rb
  4. +1 −1 spec/gmail/fetching_spec.rb
View
3 spec/fake_net_http.rb
@@ -55,14 +55,15 @@ def request(req, body = nil, &block)
prot = use_ssl ? "https" : "http"
uri = "#{prot}://#{self.address}#{req.path}"
uri, query_string = uri.split('?')
+ query_string = body unless req.method == 'GET'
if FakeWeb.registered_uri?(uri)
@socket = Net::HTTP.socket_type.new
return FakeWeb.response_for(uri, query_string, req, &block)
else
# original_net_http_connect
# return original_net_http_request(req, body, &block)
- raise "unexpected HTTP request to #{uri}"
+ raise "unexpected HTTP #{req.method} to #{uri}"
end
end
View
11 spec/fake_web.rb
@@ -112,12 +112,19 @@ def registered_uri(uri)
def response_for(uri, query_string, req, &block)
match = registered_uri(uri)
+ compare_methods(match, req)
compare_queries(match, query_string)
match.response(req, &block)
end
private
+ def compare_methods(registered, req)
+ unless registered.method == req.method
+ raise "HTTP method mismatch; expected #{registered.method}, got #{req.method} on #{registered.uri}"
+ end
+ end
+
def compare_queries(registered, query_string)
expected_query = registered.options[:query]
real_query = parse_query(query_string)
@@ -168,6 +175,10 @@ def initialize(uri, options)
self.uri = uri
self.options = options
end
+
+ def method
+ options[:method] || 'GET'
+ end
def response(req, &block)
verify_request(req)
View
38 spec/gmail/auth_spec.rb
@@ -3,6 +3,11 @@
require 'uri'
describe Contacts::Google, '.authentication_url' do
+
+ after :each do
+ FakeWeb.clean_registry
+ end
+
it 'generates a URL for target with default parameters' do
uri = parse_authentication_url('http://example.com/invite')
@@ -30,25 +35,29 @@
end
it 'should be able to exchange one-time for session token' do
- connection = mock_connection
- response = mock_response
- Net::HTTP.expects(:new).with('www.google.com', 443).returns(connection)
- connection.expects(:start)
- connection.expects(:get).with('/accounts/AuthSubSessionToken', 'Authorization' => %(AuthSub token="dummytoken")).returns(response)
- response.expects(:body).returns("Token=G25aZ-v_8B\nExpiration=20061004T123456Z")
+ # connection = mock_connection
+ # response = mock_response
+ # Net::HTTP.expects(:new).with('www.google.com', 443).returns(connection)
+ # connection.expects(:start)
+ # connection.expects(:get).with('/accounts/AuthSubSessionToken', 'Authorization' => %(AuthSub token="dummytoken")).returns(response)
+ # response.expects(:body).returns()
+ FakeWeb::register_uri 'https://www.google.com/accounts/AuthSubSessionToken',
+ :string => "Token=G25aZ-v_8B\nExpiration=20061004T123456Z",
+ :verify => lambda { |req|
+ req['Authorization'].should == %(AuthSub token="dummytoken")
+ }
Contacts::Google.session_token('dummytoken').should == 'G25aZ-v_8B'
end
it "should support client login" do
- connection = mock_connection
- response = mock_response
- Net::HTTP.expects(:new).with('www.google.com', 443).returns(connection)
- connection.expects(:start)
- connection.expects(:post).with('/accounts/ClientLogin', query_string('accountType' => 'GOOGLE',
- 'service' => 'cp', 'source' => 'Contacts-Ruby',
- 'Email' => 'mislav@example.com', 'Passwd' => 'dummyPassword')).returns(response)
- response.expects(:body).returns("SID=klw4pHhL_ry4jl6\nLSID=Ij6k-7Ypnc1sxm\nAuth=EuoqMSjN5uo-3B")
+ FakeWeb::register_uri 'https://www.google.com/accounts/ClientLogin',
+ :method => 'POST',
+ :query => {
+ 'accountType' => 'GOOGLE', 'service' => 'cp', 'source' => 'Contacts-Ruby',
+ 'Email' => 'mislav@example.com', 'Passwd' => 'dummyPassword'
+ },
+ :string => "SID=klw4pHhL_ry4jl6\nLSID=Ij6k-7Ypnc1sxm\nAuth=EuoqMSjN5uo-3B"
Contacts::Google.client_login('mislav@example.com', 'dummyPassword').should == 'EuoqMSjN5uo-3B'
end
@@ -61,4 +70,5 @@
def parse_authentication_url(*args)
URI.parse Contacts::Google.authentication_url(*args)
end
+
end
View
2 spec/gmail/fetching_spec.rb
@@ -17,7 +17,7 @@ def create
describe 'fetches contacts feed via HTTP GET' do
it 'with defaults' do
- FakeWeb.register_uri 'http://www.google.com/m8/feeds/contacts/default/thin',
+ FakeWeb::register_uri 'http://www.google.com/m8/feeds/contacts/default/thin',
:string => 'thin results',
:verify => lambda { |req|
req['Authorization'].should == %(AuthSub token="dummytoken")

0 comments on commit 1c5f87e

Please sign in to comment.