Skip to content

Commit 69b6222

Browse files
committed
Added a hack to fix OAuth2MacClient and nested/array params serialization
1 parent 40603f5 commit 69b6222

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

features/oauth2_mac_client.feature

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,38 @@ Feature: Use OAuth2 MAC client as a test client
3131
response.finish
3232
end
3333
34+
run app
35+
end
36+
37+
map "/multiple" do
38+
app = lambda do |env|
39+
if env["HTTP_AUTHORIZATION"].blank?
40+
return [401, {"Content-Type" => "text/plain"}, [""]]
41+
end
42+
43+
request = Rack::Request.new(env)
44+
response = Rack::Response.new
45+
response["Content-Type"] = "text/plain"
46+
response.write("hello #{request.params["targets"].join(", ")}")
47+
response.finish
48+
end
49+
50+
run app
51+
end
52+
53+
map "/multiple_nested" do
54+
app = lambda do |env|
55+
if env["HTTP_AUTHORIZATION"].blank?
56+
return [401, {"Content-Type" => "text/plain"}, [""]]
57+
end
58+
59+
request = Rack::Request.new(env)
60+
response = Rack::Response.new
61+
response["Content-Type"] = "text/plain"
62+
response.write("hello #{request.params["targets"].map {|company, products| company.to_s + ' with ' + products.join(' and ')}.join(", ")}")
63+
response.finish
64+
end
65+
3466
run app
3567
end
3668
end
@@ -50,6 +82,35 @@ Feature: Use OAuth2 MAC client as a test client
5082
response_body.should eq('hello rspec_api_documentation')
5183
end
5284
end
85+
86+
get "/multiple" do
87+
parameter :targets, "The people you want to greet"
88+
89+
let(:targets) { ["eric", "sam"] }
90+
91+
example "Greeting your favorite people" do
92+
do_request
93+
94+
response_headers["Content-Type"].should eq("text/plain")
95+
status.should eq(200)
96+
response_body.should eq("hello eric, sam")
97+
end
98+
end
99+
100+
get "/multiple_nested" do
101+
parameter :targets, "The companies you want to greet"
102+
103+
let(:targets) { { "apple" => ['mac', 'ios'], "google" => ['search', 'mail']} }
104+
105+
example "Greeting your favorite companies" do
106+
do_request
107+
108+
response_headers["Content-Type"].should eq("text/plain")
109+
status.should eq(200)
110+
response_body.should eq("hello apple with mac and ios, google with search and mail")
111+
end
112+
end
113+
53114
end
54115
"""
55116
When I run `rspec app_spec.rb --format RspecApiDocumentation::ApiFormatter`
@@ -61,6 +122,10 @@ Feature: Use OAuth2 MAC client as a test client
61122
Greetings
62123
GET /
63124
* Greeting your favorite gem
125+
GET /multiple
126+
* Greeting your favorite people
127+
GET /multiple_nested
128+
* Greeting your favorite companies
64129
"""
65-
And the output should contain "1 example, 0 failures"
130+
And the output should contain "3 examples, 0 failures"
66131
And the exit status should be 0

lib/rspec_api_documentation/oauth2_mac_client.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,15 @@ def do_request(method, path, params, request_headers)
4848

4949
class ProxyApp < Struct.new(:client, :app)
5050
def call(env)
51+
env["QUERY_STRING"] = query_string_hack(env)
5152
client.last_request = Struct.new(:env, :content_type).new(env, env["CONTENT_TYPE"])
5253
app.call(env.merge("SCRIPT_NAME" => ""))
5354
end
55+
56+
private
57+
def query_string_hack(env)
58+
env["QUERY_STRING"].gsub('%5B', '[').gsub('%5D', ']').gsub(/\[\d+/) { |s| "#{$1}[" }
59+
end
5460
end
5561

5662
def access_token

spec/dsl_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@
246246
trigger_callback do
247247
uri = URI.parse(callback_url)
248248
Net::HTTP.start(uri.host, uri.port) do |http|
249-
# debugger
250249
http.request Net::HTTP::Post.new(uri.path)
251250
end
252251
end

0 commit comments

Comments
 (0)