Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add OAuth2MACClient #27

Merged
merged 1 commit into from

2 participants

Eric Oestrich Sam Goldman
Eric Oestrich
Owner

No description provided.

Sam Goldman samwgoldman merged commit e2038a4 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 24, 2012
  1. Eric Oestrich

    Add OAuth2MACClient

    oestrich authored
This page is out of date. Refresh to see the latest.
10 Gemfile.lock
View
@@ -22,6 +22,7 @@ GEM
cucumber (>= 1.1.1)
ffi (>= 1.0.11)
rspec (>= 2.7.0)
+ attr_required (0.0.5)
builder (3.0.0)
capybara (1.1.2)
mime-types (>= 1.16)
@@ -45,6 +46,7 @@ GEM
ffi (1.0.11)
gherkin (2.9.3)
json (>= 1.4.6)
+ httpclient (2.2.4)
i18n (0.6.0)
json (1.6.6)
libwebsocket (0.1.3)
@@ -54,6 +56,13 @@ GEM
mustache (0.99.4)
nokogiri (1.5.2)
rack (1.4.1)
+ rack-oauth2 (0.14.4)
+ activesupport (>= 2.3)
+ attr_required (>= 0.0.5)
+ httpclient (>= 2.2.0.2)
+ i18n
+ json (>= 1.4.3)
+ rack (>= 1.1)
rack-protection (1.2.0)
rack
rack-test (0.6.1)
@@ -93,6 +102,7 @@ DEPENDENCIES
aruba
capybara
fakefs
+ rack-oauth2
rake
rspec_api_documentation!
sinatra
1  example/Gemfile.lock
View
@@ -7,7 +7,6 @@ PATH
i18n (>= 0.1.0)
json (>= 1.4.0)
mustache (>= 0.99.0)
- rack-test (>= 0.5.5)
rspec (>= 2.6.0)
webmock (>= 1.7.0)
67 features/oauth2_mac_client.feature
View
@@ -0,0 +1,67 @@
+Feature: Use OAuth2 MAC client as a test client
+ Background:
+ Given a file named "app_spec.rb" with:
+ """
+ require "rspec_api_documentation"
+ require "rspec_api_documentation/dsl"
+ require "rack/builder"
+ require "rack/oauth2"
+
+ RspecApiDocumentation.configure do |config|
+ config.app = Rack::Builder.new do
+ map "/oauth2/token" do
+ app = lambda do |env|
+ headers = {"Pragma"=>"no-cache", "Content-Type"=>"application/json", "Content-Length"=>"274", "Cache-Control"=>"no-store"}
+ body = ["{\"mac_algorithm\":\"hmac-sha-256\",\"expires_in\":29,\"access_token\":\"HfIBIMe/hxNKSMogD33OJmLN+i9x3d2iM7WLzrN1RQvINOFz+QT8hiMiY+avbp2mc8IpzrxoupHyy0DeKuB05Q==\",\"token_type\":\"mac\",\"mac_key\":\"jb59zUztvDIC0AeaNZz+BptWvmFd4C41JyZS1DfWqKCkZTErxSMfkdjkePUcpE9/joqFt0ELyV/oIsFAf0V1ew==\"}"]
+ [200, headers, body]
+ end
+
+ run app
+ end
+
+ map "/" do
+ app = lambda do |env|
+ if env["HTTP_AUTHORIZATION"].blank?
+ return [401, {"Content-Type" => "text/plain"}, [""]]
+ end
+
+ request = Rack::Request.new(env)
+ response = Rack::Response.new
+ response["Content-Type"] = "text/plain"
+ response.write("hello #{request.params["target"]}")
+ response.finish
+ end
+
+ run app
+ end
+ end
+ end
+
+ resource "Greetings" do
+ let(:client) { RspecApiDocumentation::OAuth2MACClient.new(self, {:identifier => "1", :secret => "secret"}) }
+
+ get "/" do
+ parameter :target, "The thing you want to greet"
+
+ example "Greeting your favorite gem" do
+ do_request :target => "rspec_api_documentation"
+
+ response_headers["Content-Type"].should eq("text/plain")
+ status.should eq(200)
+ response_body.should eq('hello rspec_api_documentation')
+ end
+ end
+ end
+ """
+ When I run `rspec app_spec.rb --format RspecApiDocumentation::ApiFormatter`
+
+ Scenario: Output should contain
+ Then the output should contain:
+ """
+ Generating API Docs
+ Greetings
+ GET /
+ * Greeting your favorite gem
+ """
+ And the output should contain "1 example, 0 failures"
+ And the exit status should be 0
1  lib/rspec_api_documentation.rb
View
@@ -23,6 +23,7 @@ module RspecApiDocumentation
autoload :DSL
autoload :RackTestClient
+ autoload :OAuth2MACClient, "rspec_api_documentation/oauth2_mac_client"
autoload :TestServer
autoload :HtmlWriter
autoload :JsonWriter
8 lib/rspec_api_documentation/client_base.rb
View
@@ -67,5 +67,13 @@ def format_query_hash(query_hash)
"#{key}: #{CGI.unescape(value)}"
end.join("\n")
end
+
+ def headers(method, path, params)
+ if options && options[:headers]
+ options[:headers]
+ else
+ {}
+ end
+ end
end
end
52 lib/rspec_api_documentation/oauth2_mac_client.rb
View
@@ -0,0 +1,52 @@
+module RspecApiDocumentation
+ class OAuth2MACClient < ClientBase
+ include WebMock::API
+ attr_accessor :last_response, :last_request
+ private :last_response, :last_request
+
+ def request_headers
+ env_to_headers(last_request.env)
+ end
+
+ def response_headers
+ last_response.headers
+ end
+
+ def query_string
+ last_request.env["QUERY_STRING"]
+ end
+
+ def status
+ last_response.status
+ end
+
+ def response_body
+ last_response.body
+ end
+
+ def content_type
+ last_request.content_type
+ end
+
+ protected
+
+ def do_request(method, path, params)
+ self.last_response = access_token.send(method, "http://example.com#{path}", :body => params, :header => headers(method, path, params))
+ end
+
+ class ProxyApp < Struct.new(:client, :app)
+ def call(env)
+ client.last_request = Struct.new(:env, :content_type).new(env, env["CONTENT_TYPE"])
+ app.call(env.merge("SCRIPT_NAME" => ""))
+ end
+ end
+
+ def access_token
+ @access_token ||= begin
+ app = ProxyApp.new(self, context.app)
+ stub_request(:any, %r{http://example\.com}).to_rack(app)
+ Rack::OAuth2::Client.new(options.merge(:host => "example.com", :scheme => "http")).access_token!
+ end
+ end
+ end
+end
8 lib/rspec_api_documentation/rack_test_client.rb
View
@@ -47,13 +47,5 @@ def rack_test_session
end
end.new(app)
end
-
- def headers(method, path, params)
- if options && options[:headers]
- options[:headers]
- else
- {}
- end
- end
end
end
1  rspec_api_documentation.gemspec
View
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "aruba"
s.add_development_dependency "capybara"
s.add_development_dependency "rake"
+ s.add_development_dependency "rack-oauth2"
s.files = Dir.glob("lib/**/*") + Dir.glob("templates/**/*")
s.require_path = 'lib'
Something went wrong with that request. Please try again.