Skip to content

Commit 28677ee

Browse files
committed
Merge pull request #105 from thefrontiergroup/uploaded_data
Remove nested uploaded data from request body
2 parents 9de7c78 + ebe29cd commit 28677ee

File tree

2 files changed

+104
-16
lines changed

2 files changed

+104
-16
lines changed

features/upload_file.feature

Lines changed: 90 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Feature: Uploading a file
22
Background:
3-
Given a file named "app.rb" with:
3+
Given a file named "nonestedparam.rb" with:
44
"""
55
require 'rack'
66
@@ -11,8 +11,57 @@ Feature: Uploading a file
1111
end
1212
end
1313
"""
14+
Given a file named "nestedparam.rb" with:
15+
"""
16+
require 'rack'
1417
15-
Scenario: Uploading a text file
18+
class App
19+
def self.call(env)
20+
request = Rack::Request.new(env)
21+
[200, {}, [request.params["post"]["file"][:filename]]]
22+
end
23+
end
24+
"""
25+
26+
Scenario: Uploading a text file with nested parameters
27+
Given a file named "file.txt" with:
28+
"""
29+
a file to upload
30+
"""
31+
And a file named "app_spec.rb" with:
32+
"""
33+
require "rspec_api_documentation"
34+
require "rspec_api_documentation/dsl"
35+
require "rack/test"
36+
37+
RspecApiDocumentation.configure do |config|
38+
config.app = App
39+
end
40+
41+
resource "FooBars" do
42+
post "/foobar" do
43+
parameter :post, "Post paramter"
44+
45+
let(:post) do
46+
{
47+
id: 1,
48+
file: Rack::Test::UploadedFile.new("file.txt", "text/plain")
49+
}
50+
end
51+
52+
example_request "Uploading a file" do
53+
response_body.should == "file.txt"
54+
end
55+
end
56+
end
57+
"""
58+
59+
When I run `rspec app_spec.rb --require ./nestedparam.rb --format RspecApiDocumentation::ApiFormatter`
60+
61+
Then the output should contain "1 example, 0 failures"
62+
And the exit status should be 0
63+
64+
Scenario: Uploading a text file, no nested parameters
1665
Given a file named "file.txt" with:
1766
"""
1867
a file to upload
@@ -29,10 +78,8 @@ Feature: Uploading a file
2978
3079
resource "FooBars" do
3180
post "/foobar" do
32-
parameter :name, "Name of file"
3381
parameter :file, "File to upload"
3482
35-
let(:name) { "my-new-file.txt" }
3683
let(:file) do
3784
Rack::Test::UploadedFile.new("file.txt", "text/plain")
3885
end
@@ -44,12 +91,12 @@ Feature: Uploading a file
4491
end
4592
"""
4693

47-
When I run `rspec app_spec.rb --require ./app.rb --format RspecApiDocumentation::ApiFormatter`
94+
When I run `rspec app_spec.rb --require ./nonestedparam.rb --format RspecApiDocumentation::ApiFormatter`
4895

4996
Then the output should contain "1 example, 0 failures"
5097
And the exit status should be 0
5198

52-
Scenario: Uploading an image file
99+
Scenario: Uploading an image file, no nested parameters
53100
Given I move the sample image into the workspace
54101
And a file named "app_spec.rb" with:
55102
"""
@@ -63,10 +110,8 @@ Feature: Uploading a file
63110
64111
resource "FooBars" do
65112
post "/foobar" do
66-
parameter :name, "Name of file"
67113
parameter :file, "File to upload"
68114
69-
let(:name) { "my-new-file.txt" }
70115
let(:file) do
71116
Rack::Test::UploadedFile.new("file.png", "image/png")
72117
end
@@ -78,8 +123,44 @@ Feature: Uploading a file
78123
end
79124
"""
80125

81-
When I run `rspec app_spec.rb --require ./app.rb --format RspecApiDocumentation::ApiFormatter`
126+
When I run `rspec app_spec.rb --require ./nonestedparam.rb --format RspecApiDocumentation::ApiFormatter`
82127

83128
Then the output should contain "1 example, 0 failures"
84129
And the exit status should be 0
85130
And the generated documentation should be encoded correctly
131+
132+
Scenario: Uploading an image file, no nested parameters
133+
Given I move the sample image into the workspace
134+
And a file named "app_spec.rb" with:
135+
"""
136+
require "rspec_api_documentation"
137+
require "rspec_api_documentation/dsl"
138+
require "rack/test"
139+
140+
RspecApiDocumentation.configure do |config|
141+
config.app = App
142+
end
143+
144+
resource "FooBars" do
145+
post "/foobar" do
146+
parameter :post, "Post parameter"
147+
148+
let(:post) do
149+
{
150+
id: 10,
151+
file: Rack::Test::UploadedFile.new("file.png", "image/png")
152+
}
153+
end
154+
155+
example_request "Uploading a file" do
156+
response_body.should == "file.png"
157+
end
158+
end
159+
end
160+
"""
161+
162+
When I run `rspec app_spec.rb --require ./nestedparam.rb --format RspecApiDocumentation::ApiFormatter`
163+
164+
Then the output should contain "1 example, 0 failures"
165+
And the exit status should be 0
166+
And the generated documentation should be encoded correctly

lib/rspec_api_documentation/rack_test_client.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,25 @@ def handle_multipart_body(request_headers, request_body)
4848
"rack.input" => StringIO.new(request_body)
4949
}).params
5050

51-
parsed_parameters.each do |_, value|
52-
if value.is_a?(Hash) && value.has_key?(:tempfile)
53-
data = value[:tempfile].read
54-
request_body = request_body.gsub(data, "[uploaded data]")
51+
clean_out_uploaded_data(parsed_parameters,request_body)
52+
end
53+
54+
private
55+
56+
def clean_out_uploaded_data(params,request_body)
57+
params.each do |_, value|
58+
if value.is_a?(Hash)
59+
if value.has_key?(:tempfile)
60+
data = value[:tempfile].read
61+
request_body = request_body.gsub(data, "[uploaded data]")
62+
else
63+
request_body = clean_out_uploaded_data(value,request_body)
64+
end
5565
end
5666
end
57-
5867
request_body
5968
end
6069

61-
private
62-
6370
def rack_test_session
6471
@rack_test_session ||= Struct.new(:app) do
6572
begin

0 commit comments

Comments
 (0)