Permalink
Browse files

test_that_you_can_get_a_request_token

  • Loading branch information...
1 parent 98f23c6 commit 614366464bd4512c4b6021afbadd77ae999c2a62 @ben-biddington ben-biddington committed Nov 9, 2011
Showing with 71 additions and 2 deletions.
  1. +2 −2 .bundle/config
  2. +21 −0 test/acceptance/example_tests.rb
  3. +48 −0 test/acceptance/t_internet.rb
View
@@ -1,2 +1,2 @@
----
-BUNDLE_DISABLE_SHARED_GEMS: "1"
+--- {}
+
@@ -0,0 +1,21 @@
+require File.expand_path File.join(File.dirname(__FILE__), "..", "test_helper")
+require "t_internet"
+
+Credential = Struct.new("Credential", :token, :token_secret)
+
+class Examples < Test::Unit::TestCase
+ def setup
+ credentials_okay = TestHelper::CONSUMER_SECRET && TestHelper::CONSUMER_KEY
+ fail "Missing required environment variables CONSUMER_KEY and CONSUMER_SECRET." unless credentials_okay
+ end
+
+ def test_that_you_can_get_a_request_token
+ the_url = "https://api.xero.com/oauth/RequestToken"
+
+ credential = Credential.new(TestHelper::CONSUMER_KEY, TestHelper::CONSUMER_SECRET)
+
+ response = TInternet.new(credential).get(the_url)
+
+ assert_match /oauth_token=.+&oauth_token_secret=.+&/, response
+ end
+end
@@ -0,0 +1,48 @@
+class TInternet
+ require "oauth"
+
+ def initialize(credential)
+ @credential = credential
+ end
+
+ def get(url)
+ consumer = OAuth::Consumer.new(
+ @credential.token,
+ @credential.token_secret,
+ {
+ :scheme => :header,
+ :http_method => :get
+ }
+ )
+
+ auth_header = get_auth_header url, consumer
+
+ get_core(url, {:authorization => auth_header})
+ end
+
+ private
+
+ def get_auth_header(url, consumer)
+ request = Net::HTTP::Get.new URI.parse(url).to_s
+
+ consumer.options[:signature_method] = 'HMAC-SHA1'
+ consumer.options[:nonce] = Time.now.to_i
+ consumer.options[:timestamp] = Time.now.to_i
+ consumer.options[:uri] = url
+
+ consumer.sign!(request)
+
+ request['authorization']
+ end
+
+ def get_core(url, headers = {})
+ require 'rest_client'
+
+ begin
+ RestClient.get url, headers
+ rescue => e
+ raise e unless e.respond_to?(:response)
+ e.response
+ end
+ end
+end

0 comments on commit 6143664

Please sign in to comment.