2016-04-27 13 views
1

.zipファイルのダウンロード機能をテストするために、レールテスト(Capybara &ポルターガイストを使用)を作成しようとしています。バイナリデータから.zipファイルへの書き込み

XHRリクエストから返される.zipファイルのバイナリデータがあります。このデータをローカルの.zipファイルに書き込んで、そこからさらにテストを実行したいと考えています。私はここにファイルに応答を記述しようとしています

# Perform XHR 
def download_file(link) 
    page.execute_script("window.downloadFile = function(){ var url = window.location.protocol + '//' + window.location.host + '#{link}'; return getFile(url); }") 
    page.execute_script("window.getFile = function(url){ var xhr = new XMLHttpRequest(); xhr.open('GET', url, false); xhr.responseType = 'blob'; xhr.send(); return xhr.response; }") 

    begin 
    file = page.evaluate_script('downloadFile()') 
    rescue 
    raise "Error during XHR. Is url valid?" 
    end 
    file 
end 


次のメソッドは、アプリ内、選択されたすべてのファイルのzipファイルを返し、ボタンのクリックをエミュレートし

Archive: tmp/files/download.zip 

caution: zipfile comment truncated 
error [tmp/files/download.zip]: missing 3182550208 bytes in zipfile 
    (attempting to process anyway) 
error [tmp/files/download.zip]: start of central directory not found; 
    zipfile corrupt. 
    (please check that you have transferred or created the zipfile in the 
    appropriate BINARY mode and that you have compiled UnZip properly) 

I:

file = download_file(url) 
file_path = "#{Rails.root}/tmp/files/download.zip" 
File.open(file_path, 'wb'){ |f| f.write file } 

は、私は次の応答を与えてるunzip tmp/files/download.zipを使用したファイルを解凍しようとするとMIMEタイプをtext/plainapplication/zipなどに上書きしようとしましたが、役に立たないです。
提案がありますか?

答えて

0

私はこれはそれが可能に私のシステムは、zipファイルを解析し、使用して郵便番号:: ZIPファイルクラスを読み取るために作ら

class Tempfile 
    def openBinary 
     @tmpfile.close if @tmpfile 
     @tmpfile = File.open(@tmpname, 'wb') 
     @data[1] = @tmpfile 
     __setobj__(@tmpfile) 
    end 
end 

書き込みバイナリ機能を含めるために一時ファイルのクラスを拡張しました。

関連する問題