diff --git a/features/upload_file.feature b/features/upload_file.feature index 90d06032..cf0c942b 100644 --- a/features/upload_file.feature +++ b/features/upload_file.feature @@ -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' @@ -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 @@ -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 @@ -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: """ @@ -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 @@ -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 \ No newline at end of file diff --git a/lib/rspec_api_documentation/rack_test_client.rb b/lib/rspec_api_documentation/rack_test_client.rb index 96084d1f..3c0859e6 100644 --- a/lib/rspec_api_documentation/rack_test_client.rb +++ b/lib/rspec_api_documentation/rack_test_client.rb @@ -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