Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added middleware to fix uploads from env.js

  • Loading branch information...
commit bc0832d903fd3b34639b2a2bf13bcc3f3912c71b 1 parent 1831e79
Joe Ferris jferris authored
1  lib/capybara_envjs_fixes/cucumber_fixes.rb
... ... @@ -1,2 +1,3 @@
1 1 require 'capybara_envjs_fixes/cucumber_fixes/javascript'
2 2 require 'capybara_envjs_fixes/cucumber_fixes/capybara_extensions'
  3 +require 'capybara_envjs_fixes/cucumber_fixes/base64_upload'
43 lib/capybara_envjs_fixes/cucumber_fixes/base64_upload.rb
... ... @@ -0,0 +1,43 @@
  1 +# Capybara-envjs attempts to upload files encoded in Base64, which Rack doesn't decode
  2 +# TODO: extract this to fixes gem
  3 +class Base64UploadDecoder
  4 + def initialize(app)
  5 + @app = app
  6 + end
  7 +
  8 + def call(env)
  9 + Rack::Request.new(env).POST
  10 + if env['rack.request.form_hash']
  11 + decode_base64_uploads(env['rack.request.form_hash'])
  12 + end
  13 + @app.call(env)
  14 + end
  15 +
  16 + private
  17 +
  18 + def decode_base64_uploads(hash)
  19 + hash.each do |param, value|
  20 + if Hash === value
  21 + if value[:tempfile]
  22 + if value[:tempfile].read(512).include?('Content-Transfer-Encoding: base64')
  23 + value[:tempfile].rewind
  24 + lines = value[:tempfile].readlines
  25 + while (line = lines.shift) && !line.strip.empty?
  26 + end
  27 + new_temp = Tempfile.new("base64_decode")
  28 + new_temp.write Base64.decode64(lines.join)
  29 + value[:tempfile] = new_temp
  30 + else
  31 + value[:tempfile].rewind
  32 + end
  33 + else
  34 + decode_base64_uploads(value)
  35 + end
  36 + end
  37 + end
  38 + end
  39 +end
  40 +
  41 +Capybara.app = Base64UploadDecoder.new(Capybara.app)
  42 +
  43 +

0 comments on commit bc0832d

Please sign in to comment.
Something went wrong with that request. Please try again.