Skip to content
This repository

Add OAuth2MACClient #27

Merged
merged 1 commit into from almost 2 years ago

2 participants

Eric Oestrich Sam Goldman
Eric Oestrich
Collaborator

No description provided.

Sam Goldman samwgoldman merged commit e2038a4 into from
Sam Goldman samwgoldman closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Apr 23, 2012
Eric Oestrich Add OAuth2MACClient dbac4b2
This page is out of date. Refresh to see the latest.
10  Gemfile.lock
@@ -22,6 +22,7 @@ GEM
22 22
       cucumber (>= 1.1.1)
23 23
       ffi (>= 1.0.11)
24 24
       rspec (>= 2.7.0)
  25
+    attr_required (0.0.5)
25 26
     builder (3.0.0)
26 27
     capybara (1.1.2)
27 28
       mime-types (>= 1.16)
@@ -45,6 +46,7 @@ GEM
45 46
     ffi (1.0.11)
46 47
     gherkin (2.9.3)
47 48
       json (>= 1.4.6)
  49
+    httpclient (2.2.4)
48 50
     i18n (0.6.0)
49 51
     json (1.6.6)
50 52
     libwebsocket (0.1.3)
@@ -54,6 +56,13 @@ GEM
54 56
     mustache (0.99.4)
55 57
     nokogiri (1.5.2)
56 58
     rack (1.4.1)
  59
+    rack-oauth2 (0.14.4)
  60
+      activesupport (>= 2.3)
  61
+      attr_required (>= 0.0.5)
  62
+      httpclient (>= 2.2.0.2)
  63
+      i18n
  64
+      json (>= 1.4.3)
  65
+      rack (>= 1.1)
57 66
     rack-protection (1.2.0)
58 67
       rack
59 68
     rack-test (0.6.1)
@@ -93,6 +102,7 @@ DEPENDENCIES
93 102
   aruba
94 103
   capybara
95 104
   fakefs
  105
+  rack-oauth2
96 106
   rake
97 107
   rspec_api_documentation!
98 108
   sinatra
1  example/Gemfile.lock
@@ -7,7 +7,6 @@ PATH
7 7
       i18n (>= 0.1.0)
8 8
       json (>= 1.4.0)
9 9
       mustache (>= 0.99.0)
10  
-      rack-test (>= 0.5.5)
11 10
       rspec (>= 2.6.0)
12 11
       webmock (>= 1.7.0)
13 12
 
67  features/oauth2_mac_client.feature
... ...
@@ -0,0 +1,67 @@
  1
+Feature: Use OAuth2 MAC client as a test client
  2
+  Background:
  3
+    Given a file named "app_spec.rb" with:
  4
+      """
  5
+      require "rspec_api_documentation"
  6
+      require "rspec_api_documentation/dsl"
  7
+      require "rack/builder"
  8
+      require "rack/oauth2"
  9
+
  10
+      RspecApiDocumentation.configure do |config|
  11
+        config.app = Rack::Builder.new do
  12
+          map "/oauth2/token" do
  13
+            app = lambda do |env|
  14
+              headers = {"Pragma"=>"no-cache", "Content-Type"=>"application/json", "Content-Length"=>"274", "Cache-Control"=>"no-store"}
  15
+              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==\"}"]
  16
+              [200, headers, body]
  17
+            end
  18
+
  19
+            run app
  20
+          end
  21
+
  22
+          map "/" do
  23
+            app = lambda do |env|
  24
+              if env["HTTP_AUTHORIZATION"].blank?
  25
+                return [401, {"Content-Type" => "text/plain"}, [""]]
  26
+              end
  27
+
  28
+              request = Rack::Request.new(env)
  29
+              response = Rack::Response.new
  30
+              response["Content-Type"] = "text/plain"
  31
+              response.write("hello #{request.params["target"]}")
  32
+              response.finish
  33
+            end
  34
+
  35
+            run app
  36
+          end
  37
+        end
  38
+      end
  39
+
  40
+      resource "Greetings" do
  41
+        let(:client) { RspecApiDocumentation::OAuth2MACClient.new(self, {:identifier => "1", :secret => "secret"}) }
  42
+
  43
+        get "/" do
  44
+          parameter :target, "The thing you want to greet"
  45
+
  46
+          example "Greeting your favorite gem" do
  47
+            do_request :target => "rspec_api_documentation"
  48
+
  49
+            response_headers["Content-Type"].should eq("text/plain")
  50
+            status.should eq(200)
  51
+            response_body.should eq('hello rspec_api_documentation')
  52
+          end
  53
+        end
  54
+      end
  55
+      """
  56
+    When  I run `rspec app_spec.rb --format RspecApiDocumentation::ApiFormatter`
  57
+
  58
+  Scenario: Output should contain
  59
+    Then  the output should contain:
  60
+      """
  61
+      Generating API Docs
  62
+        Greetings
  63
+        GET /
  64
+          * Greeting your favorite gem
  65
+      """
  66
+    And   the output should contain "1 example, 0 failures"
  67
+    And   the exit status should be 0
1  lib/rspec_api_documentation.rb
@@ -23,6 +23,7 @@ module RspecApiDocumentation
23 23
 
24 24
   autoload :DSL
25 25
   autoload :RackTestClient
  26
+  autoload :OAuth2MACClient, "rspec_api_documentation/oauth2_mac_client"
26 27
   autoload :TestServer
27 28
   autoload :HtmlWriter
28 29
   autoload :JsonWriter
8  lib/rspec_api_documentation/client_base.rb
@@ -67,5 +67,13 @@ def format_query_hash(query_hash)
67 67
         "#{key}: #{CGI.unescape(value)}"
68 68
       end.join("\n")
69 69
     end
  70
+
  71
+    def headers(method, path, params)
  72
+      if options && options[:headers]
  73
+        options[:headers]
  74
+      else
  75
+        {}
  76
+      end
  77
+    end
70 78
   end
71 79
 end
52  lib/rspec_api_documentation/oauth2_mac_client.rb
... ...
@@ -0,0 +1,52 @@
  1
+module RspecApiDocumentation
  2
+  class OAuth2MACClient < ClientBase
  3
+    include WebMock::API
  4
+    attr_accessor :last_response, :last_request
  5
+    private :last_response, :last_request
  6
+
  7
+    def request_headers
  8
+      env_to_headers(last_request.env)
  9
+    end
  10
+
  11
+    def response_headers
  12
+      last_response.headers
  13
+    end
  14
+
  15
+    def query_string
  16
+      last_request.env["QUERY_STRING"]
  17
+    end
  18
+
  19
+    def status
  20
+      last_response.status
  21
+    end
  22
+
  23
+    def response_body
  24
+      last_response.body
  25
+    end
  26
+
  27
+    def content_type
  28
+      last_request.content_type
  29
+    end
  30
+
  31
+    protected
  32
+
  33
+    def do_request(method, path, params)
  34
+      self.last_response = access_token.send(method, "http://example.com#{path}", :body => params, :header => headers(method, path, params))
  35
+    end
  36
+
  37
+    class ProxyApp < Struct.new(:client, :app)
  38
+      def call(env)
  39
+        client.last_request = Struct.new(:env, :content_type).new(env, env["CONTENT_TYPE"])
  40
+        app.call(env.merge("SCRIPT_NAME" => ""))
  41
+      end
  42
+    end
  43
+
  44
+    def access_token
  45
+      @access_token ||= begin
  46
+                          app = ProxyApp.new(self, context.app)
  47
+                          stub_request(:any, %r{http://example\.com}).to_rack(app)
  48
+                          Rack::OAuth2::Client.new(options.merge(:host => "example.com", :scheme => "http")).access_token!
  49
+                        end
  50
+    end
  51
+  end
  52
+end
8  lib/rspec_api_documentation/rack_test_client.rb
@@ -47,13 +47,5 @@ def rack_test_session
47 47
         end
48 48
       end.new(app)
49 49
     end
50  
-
51  
-    def headers(method, path, params)
52  
-      if options && options[:headers]
53  
-        options[:headers]
54  
-      else
55  
-        {}
56  
-      end
57  
-    end
58 50
   end
59 51
 end
1  rspec_api_documentation.gemspec
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
27 27
   s.add_development_dependency "aruba"
28 28
   s.add_development_dependency "capybara"
29 29
   s.add_development_dependency "rake"
  30
+  s.add_development_dependency "rack-oauth2"
30 31
 
31 32
   s.files        = Dir.glob("lib/**/*") + Dir.glob("templates/**/*")
32 33
   s.require_path = 'lib'
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.