Skip to content

Commit

Permalink
Merge pull request #310 from PavelBezpalov/master
Browse files Browse the repository at this point in the history
Clean out uploaded data from arrays
  • Loading branch information
oestrich authored Apr 26, 2017
2 parents 0e8da74 + c9df040 commit 9b5fdcb
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
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

0 comments on commit 9b5fdcb

Please sign in to comment.