.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/plain
、application/zip
などに上書きしようとしましたが、役に立たないです。
提案がありますか?