Skip to content

Clean out uploaded data from arrays #310

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

Merged
merged 3 commits into from
Apr 26, 2017
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
51 changes: 49 additions & 2 deletions features/upload_file.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@ Feature: Uploading a file
[200, {}, [request.params["post"]["file"][:filename]]]
end
end
"""
"""
Given a file named "nested_param_in_array.rb" with:
"""
require 'rack'

class App
def self.call(env)
request = Rack::Request.new(env)
[200, {}, [request.params["post"]["files"][0][:filename]]]
end
end
"""

Scenario: Uploading a text file with nested parameters
Given a file named "file.txt" with:
Expand All @@ -40,7 +51,7 @@ Feature: Uploading a file

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

let(:post) do
{
Expand Down Expand Up @@ -161,6 +172,42 @@ Feature: Uploading a file

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

Scenario: Uploading an image file in params array
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,
files: [ Rack::Test::UploadedFile.new("file.png", "image/png") ]
}
end

example_request "Uploading a file" do
expect(response_body).to eq("file.png")
end
end
end
"""

When I run `rspec app_spec.rb --require ./nested_param_in_array.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
16 changes: 8 additions & 8 deletions lib/rspec_api_documentation/client_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ def record_response_body(response_content_type, response_body)
end

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
params.each do |value|
if [Hash, Array].member? value.class
request_body = if value.respond_to?(:has_key?) && value.has_key?(:tempfile)
data = value[:tempfile].read
request_body.gsub(data, "[uploaded data]")
else
clean_out_uploaded_data(value, request_body)
end
end
end
request_body
Expand Down