Search code examples
ruby-on-railsrubyfile-uploaduploadcarrierwave

How to test binary post uploaded file in rails?


I am currently testing if uploaded file is the same as original file. The api requires upload body to be binary, not multipart/form-data.

This is the spec:

  it "uploads" do
    fixture_path = Rails.root.join("spec/fixtures/")
    fixture_file = fixture_path.join("landscape.jpg")
    file = Rack::Test::UploadedFile.new(fixture_file, "image/jpeg", :binary)
    post api_upload_upload_path, file, header
    assert_response :success
  end

When I run the spec, it fails:

Failures:

  1) Api::V1::UploadController uploads
     Failure/Error: post api_upload_upload_path, file, company_header
     ArgumentError:
       invalid %-encoding (These are bad characters

How to fix this? or, Is there any better way to test this kind of action?


Solution

  • I've figured it out. It's because browser is hard to post an image. AngularJS puts it into multipart/form-data.