Skip to content

Commit e2038a4

Browse files
committed
Merge pull request zipmark#27 from oestrich/oauth2_client
Add OAuth2MACClient
2 parents 335dbe3 + dbac4b2 commit e2038a4

File tree

8 files changed

+139
-9
lines changed

8 files changed

+139
-9
lines changed

Gemfile.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ GEM
2222
cucumber (>= 1.1.1)
2323
ffi (>= 1.0.11)
2424
rspec (>= 2.7.0)
25+
attr_required (0.0.5)
2526
builder (3.0.0)
2627
capybara (1.1.2)
2728
mime-types (>= 1.16)
@@ -45,6 +46,7 @@ GEM
4546
ffi (1.0.11)
4647
gherkin (2.9.3)
4748
json (>= 1.4.6)
49+
httpclient (2.2.4)
4850
i18n (0.6.0)
4951
json (1.6.6)
5052
libwebsocket (0.1.3)
@@ -54,6 +56,13 @@ GEM
5456
mustache (0.99.4)
5557
nokogiri (1.5.2)
5658
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)
5766
rack-protection (1.2.0)
5867
rack
5968
rack-test (0.6.1)
@@ -93,6 +102,7 @@ DEPENDENCIES
93102
aruba
94103
capybara
95104
fakefs
105+
rack-oauth2
96106
rake
97107
rspec_api_documentation!
98108
sinatra

example/Gemfile.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ PATH
77
i18n (>= 0.1.0)
88
json (>= 1.4.0)
99
mustache (>= 0.99.0)
10-
rack-test (>= 0.5.5)
1110
rspec (>= 2.6.0)
1211
webmock (>= 1.7.0)
1312

features/oauth2_mac_client.feature

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/rspec_api_documentation.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module RspecApiDocumentation
2323

2424
autoload :DSL
2525
autoload :RackTestClient
26+
autoload :OAuth2MACClient, "rspec_api_documentation/oauth2_mac_client"
2627
autoload :TestServer
2728
autoload :HtmlWriter
2829
autoload :JsonWriter

lib/rspec_api_documentation/client_base.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,13 @@ def format_query_hash(query_hash)
6767
"#{key}: #{CGI.unescape(value)}"
6868
end.join("\n")
6969
end
70+
71+
def headers(method, path, params)
72+
if options && options[:headers]
73+
options[:headers]
74+
else
75+
{}
76+
end
77+
end
7078
end
7179
end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/rspec_api_documentation/rack_test_client.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,5 @@ def rack_test_session
4747
end
4848
end.new(app)
4949
end
50-
51-
def headers(method, path, params)
52-
if options && options[:headers]
53-
options[:headers]
54-
else
55-
{}
56-
end
57-
end
5850
end
5951
end

rspec_api_documentation.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
2727
s.add_development_dependency "aruba"
2828
s.add_development_dependency "capybara"
2929
s.add_development_dependency "rake"
30+
s.add_development_dependency "rack-oauth2"
3031

3132
s.files = Dir.glob("lib/**/*") + Dir.glob("templates/**/*")
3233
s.require_path = 'lib'

0 commit comments

Comments
 (0)