Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove nested uploaded data from request body #105

Merged
merged 2 commits into from
Jan 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 90 additions & 9 deletions features/upload_file.feature
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Feature: Uploading a file
Background:
Given a file named "app.rb" with:
Given a file named "nonestedparam.rb" with:
"""
require 'rack'

Expand All @@ -11,8 +11,57 @@ Feature: Uploading a file
end
end
"""
Given a file named "nestedparam.rb" with:
"""
require 'rack'

Scenario: Uploading a text file
class App
def self.call(env)
request = Rack::Request.new(env)
[200, {}, [request.params["post"]["file"][:filename]]]
end
end
"""

Scenario: Uploading a text file with nested parameters
Given a file named "file.txt" with:
"""
a file to upload
"""
And a file named "app_spec.rb" with:
"""
require "rspec_api_documentation"
require "rspec_api_documentation/dsl"
require "rack/test"

RspecApiDocumentation.configure do |config|
config.app = App
end

resource "FooBars" do
post "/foobar" do
parameter :post, "Post paramter"

let(:post) do
{
id: 1,
file: Rack::Test::UploadedFile.new("file.txt", "text/plain")
}
end

example_request "Uploading a file" do
response_body.should == "file.txt"
end
end
end
"""

When I run `rspec app_spec.rb --require ./nestedparam.rb --format RspecApiDocumentation::ApiFormatter`

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

Scenario: Uploading a text file, no nested parameters
Given a file named "file.txt" with:
"""
a file to upload
Expand All @@ -29,10 +78,8 @@ Feature: Uploading a file

resource "FooBars" do
post "/foobar" do
parameter :name, "Name of file"
parameter :file, "File to upload"

let(:name) { "my-new-file.txt" }
let(:file) do
Rack::Test::UploadedFile.new("file.txt", "text/plain")
end
Expand All @@ -44,12 +91,12 @@ Feature: Uploading a file
end
"""

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

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

Scenario: Uploading an image file
Scenario: Uploading an image file, no nested parameters
Given I move the sample image into the workspace
And a file named "app_spec.rb" with:
"""
Expand All @@ -63,10 +110,8 @@ Feature: Uploading a file

resource "FooBars" do
post "/foobar" do
parameter :name, "Name of file"
parameter :file, "File to upload"

let(:name) { "my-new-file.txt" }
let(:file) do
Rack::Test::UploadedFile.new("file.png", "image/png")
end
Expand All @@ -78,8 +123,44 @@ Feature: Uploading a file
end
"""

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

Then the output should contain "1 example, 0 failures"
And the exit status should be 0
And the generated documentation should be encoded correctly

Scenario: Uploading an image file, no nested parameters
Given I move the sample image into the workspace
And a file named "app_spec.rb" with:
"""
require "rspec_api_documentation"
require "rspec_api_documentation/dsl"
require "rack/test"

RspecApiDocumentation.configure do |config|
config.app = App
end

resource "FooBars" do
post "/foobar" do
parameter :post, "Post parameter"

let(:post) do
{
id: 10,
file: Rack::Test::UploadedFile.new("file.png", "image/png")
}
end

example_request "Uploading a file" do
response_body.should == "file.png"
end
end
end
"""

When I run `rspec app_spec.rb --require ./nestedparam.rb --format RspecApiDocumentation::ApiFormatter`

Then the output should contain "1 example, 0 failures"
And the exit status should be 0
And the generated documentation should be encoded correctly
21 changes: 14 additions & 7 deletions lib/rspec_api_documentation/rack_test_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,25 @@ def handle_multipart_body(request_headers, request_body)
"rack.input" => StringIO.new(request_body)
}).params

parsed_parameters.each do |_, value|
if value.is_a?(Hash) && value.has_key?(:tempfile)
data = value[:tempfile].read
request_body = request_body.gsub(data, "[uploaded data]")
clean_out_uploaded_data(parsed_parameters,request_body)
end

private

def clean_out_uploaded_data(params,request_body)
params.each do |_, value|
if value.is_a?(Hash)
if value.has_key?(:tempfile)
data = value[:tempfile].read
request_body = request_body.gsub(data, "[uploaded data]")
else
request_body = clean_out_uploaded_data(value,request_body)
end
end
end

request_body
end

private

def rack_test_session
@rack_test_session ||= Struct.new(:app) do
begin
Expand Down